/// <summary>
        ///   Registers <see cref="VolatileCache"/> as singleton implementation for
        ///   <see cref="ICache"/>, <see cref="IAsyncCache"/>, <see cref="IDistributedCache"/>.
        /// </summary>
        /// <param name="services">Services collection.</param>
        /// <param name="changeSettings">Can be used to customize settings.</param>
        /// <returns>Modified services collection.</returns>
        public static IServiceCollection AddVolatileSQLiteKVLiteCache(this IServiceCollection services, Action <VolatileCacheSettings> changeSettings)
        {
            var settings = new VolatileCacheSettings();

            changeSettings?.Invoke(settings);

            var ext = services.GetKVLiteExtensionServices();

#pragma warning disable CC0022 // Should dispose object
            return(services.AddKVLiteCache(new VolatileCache(settings, ext.Serializer, ext.Compressor, ext.Clock, ext.Random)));

#pragma warning restore CC0022 // Should dispose object
        }
Exemple #2
0
        /// <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.GetCurrentInstant() + Duration.FromMinutes(5));
            persistentCache.AddTimed(examplePartition1, exampleKey2, simpleValue, persistentCache.Clock.GetCurrentInstant() + Duration.FromMinutes(10));
            persistentCache.AddTimed(examplePartition2, exampleKey1, complexValue, persistentCache.Clock.GetCurrentInstant() + Duration.FromMinutes(10));
            persistentCache.AddTimed(examplePartition2, exampleKey2, complexValue, persistentCache.Clock.GetCurrentInstant() + Duration.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.
                StaticInterval = Duration.FromDays(10) // How long static values will last.
            };

            // 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.
                StaticInterval       = Duration.FromDays(10) // How long static values will last.
            };

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

            persistentCache = new PersistentCache(persistentCacheSettings);

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

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

            /*
             * An item 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.
             *
             * "Static" values are a special form of "sliding" values.
             * They use a very long timespan, 30 days by default, and they can be used for seldom changed data.
             */

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

            Console.Read();
        }
Exemple #3
0
        /// <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.AddMinutes(5));
            persistentCache.AddTimed(examplePartition1, exampleKey2, simpleValue, persistentCache.Clock.UtcNow.AddMinutes(10));
            persistentCache.AddTimed(examplePartition2, exampleKey1, complexValue, persistentCache.Clock.UtcNow.AddMinutes(10));
            persistentCache.AddTimed(examplePartition2, exampleKey2, complexValue, persistentCache.Clock.UtcNow.AddMinutes(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.
                StaticIntervalInDays = 10 // How many days static values will last.
            };

            // 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.
                InsertionCountBeforeAutoClean = 10, // Number of inserts before a cache cleanup is issued.
                MaxCacheSizeInMB = 64, // Max size in megabytes for the cache.
                MaxJournalSizeInMB = 16, // Max size in megabytes for the SQLite journal log.
                StaticIntervalInDays = 10 // How many days static values will last.
            };

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

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

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

            /*
             * An item 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.
             *
             * "Static" values are a special form of "sliding" values.
             * They use a very long timespan, 30 days by default, and they can be used for seldom changed data.
             */

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

            Console.Read();
        }