PommaLabs.KVLite.Database 11.0.7

Prefix Reserved
This package has a SemVer 2.0.0 package version: 11.0.7+0a479187.
There is a newer version of this package available.
See the version list below for details.
dotnet add package PommaLabs.KVLite.Database --version 11.0.7                
NuGet\Install-Package PommaLabs.KVLite.Database -Version 11.0.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PommaLabs.KVLite.Database" Version="11.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PommaLabs.KVLite.Database --version 11.0.7                
#r "nuget: PommaLabs.KVLite.Database, 11.0.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PommaLabs.KVLite.Database as a Cake Addin
#addin nuget:?package=PommaLabs.KVLite.Database&version=11.0.7

// Install PommaLabs.KVLite.Database as a Cake Tool
#tool nuget:?package=PommaLabs.KVLite.Database&version=11.0.7                

KVLite

License: MIT Donate Docs NuGet version NuGet downloads

standard-readme compliant GitLab pipeline status Quality gate Code coverage Renovate enabled

KVLite is a partition-based key-value cache built for SQL RDBMSs.

KVLite entries can be stored either in persistent or volatile fashion, and each key/value pair can have its own lifetime and refresh mode.

Entries are grouped by partition, where each key must be unique only inside the partition it belongs to.

Following RDBMSs are currently supported by KVLite:

  • MySQL and MariaDB (.NET and .NET Core, with integration tests for MySQL only)
  • Oracle (.NET and .NET Core)
  • PostgreSQL (.NET and .NET Core, with integration tests)
  • SQL Server (.NET and .NET Core, with integration tests)
  • SQLite (.NET and .NET Core, with integrations tests)

KVLite implements different caching adapters for various libraries and frameworks:

An in-memory driver is also provided for unit testing.

Table of Contents

Install

KVLite requires a driver for your RDBMS, packages are linked above. For example, if you use MySQL, the you should install:

dotnet add package PommaLabs.KVLite.MySql

After that, you could install specific adapters (like the one for ASP.NET Core) and you should set your DB up according to the documentation below.

Storage layout

KVLite stores cache entries in a dedicated table, whose schema is as much tuned as possible for each RDBMS. The logical schema for cache entries table is the following:

Column name Data type Content
kvle_id guid or long Automatically generated ID. This is the primary key.
kvle_hash long Hash of partition and key. This is the unique key.
kvle_expiry long When the entry will expire, expressed as seconds after UNIX epoch.
kvle_interval long How many seconds should be used to extend expiry time when the entry is retrieved.
kvle_value byte[] Serialized and optionally compressed content of this entry.
kvle_compressed bool Whether the entry content was compressed or not.
kvle_partition string A partition holds a group of related keys.
kvle_key string A key uniquely identifies an entry inside a partition.
kvle_creation long When the entry was created, expressed as seconds after UNIX epoch.
kvle_parent_hash0 long Optional parent entry hash, used to link entries in a hierarchical way.
kvle_parent_key0 string Optional parent entry key, used to link entries in a hierarchical way.
kvle_parent_hash1 long Optional parent entry hash, used to link entries in a hierarchical way.
kvle_parent_key1 string Optional parent entry key, used to link entries in a hierarchical way.
kvle_parent_hash2 long Optional parent entry hash, used to link entries in a hierarchical way.
kvle_parent_key2 string Optional parent entry key, used to link entries in a hierarchical way.

If SQL user chosen for KVLite has enough privileges, then cache entries table will be automatically created. Anyway, specialized schemas for supported RDBMS systems are available inside this project repository, at following links:

Each script might have a few comments suggesting how to further optimize cache entries table storage depending on the actual version of the specific RDBMS system.

Customizing SQL layout

Default name for cache entries table is kvle_cache_entries and default SQL schema is kvlite. However, those values can be easily changed at runtime, as we do in the following snippet:

// Change cache entries table name for Oracle cache.
OracleCache.DefaultInstance.Settings.CacheEntriesTableName = "my_custom_name";

// Change SQL schema name for MySQL cache.
MySqlCache.DefaultInstance.Settings.CacheSchemaName = "my_schema_name";

// Change both table ans schema name for SQL Server cache.
SqlServerCache.DefaultInstance.Settings.CacheEntriesTableName = "my_custom_name";
SqlServerCache.DefaultInstance.Settings.CacheSchemaName = "my_schema_name";

Please perform those customizations as early as your application starts; for example, these are good places where to put the lines:

  • Program.cs for console and Windows applications.
  • Global.asax.cs for classic web applications.
  • Startup.cs for Owin-based web applications or ASP.NET Core.

Usage

Let's start with a simple example of what you can do with KVLite:

/// <summary>
///   Learn how to use KVLite by examples.
/// </summary>
internal static class Program
{
    /// <summary>
    ///   Learn how to use KVLite by examples.
    /// </summary>
    public static void Main()
    {
        // Some variables used in the examples.
        var examplePartition1 = "example partition 1";
        var examplePartition2 = "example partition 2";
        var exampleKey1 = "example key 1";
        var exampleKey2 = "example key 2";
        var simpleValue = Math.PI;
        var complexValue = new ComplexValue
        {
            Integer = 21,
            NullableBoolean = null,
            String = "Learning KVLite",
            Dictionary = new Dictionary<short, ComplexValue>
            {
                [1] = new ComplexValue { NullableBoolean = true },
                [2] = new ComplexValue { String = "Nested..." }
            }
        };

        /*
         * KVLite stores its values inside a given partition and each value is linked to a key.
         * KVLite can contain more than one partition and each partition can contain more than one key.
         *
         * Therefore, values are stored according to this logical layout:
         *
         * [partition1] --> key1/value1
         *              --> key2/value2
         * [partition2] --> key1/value1
         *              --> key2/value2
         *              --> key3/value3
         *
         * A key is unique inside a partition, not inside all cache.
         * A partition, instead, is unique inside all cache.
         */

        // You can start using the default caches immediately. Let's try to store some values in
        // a way similar to the figure above, using the default persistent cache.
        ICache persistentCache = PersistentCache.DefaultInstance;
        persistentCache.AddTimed(examplePartition1, exampleKey1, simpleValue, persistentCache.Clock.UtcNow + TimeSpan.FromMinutes(5));
        persistentCache.AddTimed(examplePartition1, exampleKey2, simpleValue, persistentCache.Clock.UtcNow + TimeSpan.FromMinutes(10));
        persistentCache.AddTimed(examplePartition2, exampleKey1, complexValue, persistentCache.Clock.UtcNow + TimeSpan.FromMinutes(10));
        persistentCache.AddTimed(examplePartition2, exampleKey2, complexValue, persistentCache.Clock.UtcNow + TimeSpan.FromMinutes(5));
        PrettyPrint(persistentCache);

        // Otherwise, you can customize you own cache... Let's see how we can use a volatile
        // cache. Let's define the settings that we will use in new volatile caches.
        var volatileCacheSettings = new VolatileCacheSettings
        {
            CacheName = "My In-Memory Cache", // The backend.
        };

        // Then the settings that we will use in new persistent caches.
        var persistentCacheSettings = new PersistentCacheSettings
        {
            CacheFile = "CustomCache.sqlite", // The SQLite DB used as the backend for the cache.
            ChancesOfAutoCleanup = 0.5, // Chance of an automatic a cache cleanup being issued.
        };

        // We create both a volatile and a persistent cache.
        var volatileCache = new VolatileCache(volatileCacheSettings);
        persistentCache = new PersistentCache(persistentCacheSettings);

        // Use the new volatile cache!
        volatileCache.AddTimed(examplePartition1, exampleKey1, Tuple.Create("Volatile!", 123), TimeSpan.FromMinutes(60));
        PrettyPrint(volatileCache);

        // Use the new persistent cache!
        persistentCache.AddTimed(examplePartition2, exampleKey2, Tuple.Create("Persistent!", 123), TimeSpan.FromMinutes(60));
        PrettyPrint(persistentCache);

        /*
         * An entry can be added to the cache in three different ways.
         *
         * "Timed" values last until the specified date and time, or for a specified timespan.
         * Reading them will not extend their lifetime.
         *
         * "Sliding" values last for the specified lifetime, but, if read,
         * their lifetime will be extended by the timespan specified initially.
         */

        // Let's clear the volatile cache and let's a value for each type.
        volatileCache.Clear();
        volatileCache.AddTimed(examplePartition1, exampleKey1, simpleValue, volatileCache.Clock.UtcNow + TimeSpan.FromMinutes(10));
        volatileCache.AddTimed(examplePartition1, exampleKey2, complexValue, TimeSpan.FromMinutes(15));
        volatileCache.AddSliding(examplePartition2, exampleKey2, complexValue, TimeSpan.FromMinutes(15));
        PrettyPrint(volatileCache);
    }

    private static void PrettyPrint(ICache cache)
    {
        Console.WriteLine($"Printing the contents of a {cache.GetType().Name}");

        // When we use "Peek*" methods, the expiration time of entries is left untouched.
        var cacheEntries = cache.PeekEntries<object>();
        foreach (var cacheEntry in cacheEntries.OrderBy(ci => ci.Partition).ThenBy(ci => ci.Key))
        {
            Console.WriteLine($"{cacheEntry.Partition} --> {cacheEntry.Key} --> {cacheEntry.Value}");
        }

        Console.WriteLine();
    }

    private sealed class ComplexValue
    {
        public int Integer { get; set; }
        public bool? NullableBoolean { get; set; }
        public string String { get; set; }
        public IDictionary<short, ComplexValue> Dictionary { get; set; }

        public override string ToString() => nameof(ComplexValue);
    }
}

Polly cache provider

Moreover, KVLite can be used as Polly cache provider:

// Every KVLite cache can be interfaced with Polly: Memory, MySQL, Oracle, ...
var options = new KVLiteCacheProviderOptions();
var cacheProvider = new KVLiteSyncCacheProvider<string>(PersistentCache.DefaultInstance, options);
var cachePolicy = Policy.Cache(cacheProvider, TimeSpan.FromMinutes(10));

var myGuid1 = cachePolicy.Execute(() => Guid.NewGuid().ToString(), new Context("MyGuid"));
var myGuid2 = cachePolicy.Execute(() => Guid.NewGuid().ToString(), new Context("MyGuid"));

// Two GUIDs are equal because they share the same key.
Debug.Assert(myGuid1 == myGuid2);

myGuid1 = cachePolicy.Execute(() => Guid.NewGuid().ToString(), new KVLiteContext("My", "Complex", "Key", 1));
myGuid2 = cachePolicy.Execute(() => Guid.NewGuid().ToString(), new KVLiteContext("My", "Complex", "Key", 2));

// Two GUIDs are not equal because they do not share the same key.
Debug.Assert(myGuid1 != myGuid2);

Examples

Further examples can be found in the following projects:

  • ASP.NET Core: It shows how to register KVLite services and how to use it as a proper distributed cache implementation. Moreover, it shows how to use KVLite session extensions.

Maintainers

@pomma89.

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2014-2021 Alessio Parma

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on PommaLabs.KVLite.Database:

Package Downloads
PommaLabs.KVLite.SqlServer

KVLite is a partition-based key-value cache built for SQL RDBMSs. This package contains default SQL Server driver.

PommaLabs.KVLite.MySql

KVLite is a partition-based key-value cache built for SQL RDBMSs. This package contains default MySQL driver.

PommaLabs.KVLite.SQLite

KVLite is a partition-based key-value cache built for SQL RDBMSs. This package contains default SQLite driver.

PommaLabs.KVLite.PostgreSql

KVLite is a partition-based key-value cache built for SQL RDBMSs. This package contains default PostgreSQL driver.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
12.3.0 2,117 11/15/2023
12.2.1 3,000 12/18/2022
12.2.0 146 12/17/2022
12.1.0 144 12/16/2022
12.0.0 4,971 1/23/2022
11.1.0 2,499 11/28/2021
11.0.7 893 9/24/2021
11.0.5 2,299 4/25/2021
11.0.4 1,361 3/17/2021
11.0.3 1,237 3/14/2021
11.0.2 1,752 1/16/2021
11.0.1 1,184 12/27/2020
11.0.0 1,343 11/1/2020
10.1.1 1,284 10/26/2020
10.1.0 1,320 8/12/2020
10.0.5 1,361 6/19/2020
10.0.4 1,360 6/14/2020
10.0.3 1,349 6/3/2020
10.0.2 1,308 6/1/2020
10.0.0 1,588 3/9/2020
9.3.3 1,394 3/2/2020
9.2.7 1,451 2/7/2020
9.2.6 1,400 2/6/2020
9.2.5 1,330 2/5/2020
9.2.4 1,732 1/4/2020
9.2.3 1,424 1/2/2020
9.1.2 2,035 8/31/2019
9.0.6 3,731 5/5/2019
9.0.5 2,122 2/26/2019
9.0.4 1,495 2/25/2019
9.0.3 2,313 12/28/2018
8.1.3 3,371 8/17/2018
8.1.2 1,688 8/16/2018
8.1.0 1,725 8/14/2018
8.0.2 1,833 7/7/2018
8.0.1 1,868 6/12/2018
7.2.3 1,775 4/5/2018
7.2.2 1,774 4/1/2018
7.2.1 1,891 4/1/2018
7.1.1 1,807 3/24/2018
7.1.0 1,725 3/18/2018
7.0.3 1,621 3/6/2018
7.0.2 1,816 3/6/2018
6.4.4 2,382 11/1/2017
6.4.3 2,259 10/29/2017
6.4.2 2,301 10/28/2017
6.4.0 2,111 8/16/2017
6.3.3 1,046 10/28/2017