Ejemplo n.º 1
0
        public void AddStatic_TwoCaches_NoMix()
        {
            const string key = "key";

            using (var another = new VolatileCache(new VolatileCacheSettings {
                CacheName = "another"
            }, clock: Kernel.Get <IClock>()))
            {
                try
                {
                    Cache.AddStatic(key, 1);
                    another.AddStatic(key, 2);
                    Assert.True(Cache.Contains(key));
                    Assert.True(another.Contains(key));
                    Assert.AreEqual(1, ((VolatileCache)Cache)[Cache.Settings.DefaultPartition, key].Value);
                    Assert.AreEqual(2, another[Cache.Settings.DefaultPartition, key].Value);

                    another.AddStatic(key + key, 3);
                    Assert.False(Cache.Contains(key + key));
                    Assert.True(another.Contains(key + key));
                    Assert.AreEqual(3, another[Cache.Settings.DefaultPartition, key + key].Value);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"{ex.Message} - {ex.GetType().Name} - {ex.StackTrace}");
                    Console.Error.WriteLine(Cache.LastError?.Message ?? "First cache has no errors");
                    Console.Error.WriteLine(another.LastError?.Message ?? "Second cache has no errors");
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        public void NewCache_GoodName(string name)
        {
            ICache cache = new VolatileCache(new VolatileCacheSettings {
                CacheName = name
            }, clock: Kernel.Get <IClock>());

            Assert.That(cache, Is.Not.Null);
            cache.Dispose();
            Assert.That(cache.Disposed, Is.True);
        }
Ejemplo n.º 3
0
        public void NewCache_WrongName(string name)
        {
            ICache cache;

            try
            {
#pragma warning disable CC0022 // Should dispose object
                cache = new VolatileCache(new VolatileCacheSettings {
                    CacheName = name
                }, clock: Kernel.Get <IClock>());
#pragma warning restore CC0022 // Should dispose object
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOf <ArgumentException>(ex);
                Assert.True(ex.Message.Contains(ErrorMessages.InvalidCacheName));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 ///   Initializes the provider using the specified cache.
 /// </summary>
 /// <param name="cache">The cache that will be used by the provider.</param>
 public VolatileOutputCacheProvider(VolatileCache cache)
     : base(cache)
 {
 }
Ejemplo n.º 5
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="VolatileViewStatePersister"/> class.
 /// </summary>
 /// <param name="page">The page.</param>
 /// <param name="cache">The volatile cache.</param>
 public VolatileViewStatePersister(Page page, VolatileCache cache)
     : base(page, cache)
 {
 }
Ejemplo n.º 6
0
 public void Dispose_ObjectDisposedExceptionAfterDispose()
 {
     Cache = new VolatileCache(new VolatileCacheSettings());
     Cache.Dispose();
     Assert.Throws <ObjectDisposedException>(() => { Cache.Count(); });
 }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 9
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="VolatileViewStatePersister"/> class.
 /// </summary>
 /// <param name="page">The page.</param>
 /// <param name="cache">The volatile cache.</param>
 public VolatileViewStatePersister(Page page, VolatileCache cache)
     : base(page, cache)
 {
 }
Ejemplo n.º 10
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();
        }