public void TestExpiration()
        {
            var cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;

            Assert.That(cache, Is.Not.Null, "pre-configured foo cache not found");
            Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(500)), "Unexpected expiration value for foo region");

            cache = (CoreDistributedCache)DefaultProvider.BuildCache("noExplicitExpiration", null);
            Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(300)),
                        "Unexpected expiration value for noExplicitExpiration region");
            Assert.That(cache.UseSlidingExpiration, Is.True, "Unexpected sliding value for noExplicitExpiration region");

            cache = (CoreDistributedCache)DefaultProvider
                    .BuildCache("noExplicitExpiration", new Dictionary <string, string> {
                { "expiration", "100" }
            });
            Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(100)),
                        "Unexpected expiration value for noExplicitExpiration region with default expiration");

            cache = (CoreDistributedCache)DefaultProvider
                    .BuildCache("noExplicitExpiration",
                                new Dictionary <string, string> {
                { Cfg.Environment.CacheDefaultExpiration, "50" }
            });
            Assert.That(cache.Expiration, Is.EqualTo(TimeSpan.FromSeconds(50)),
                        "Unexpected expiration value for noExplicitExpiration region with cache.default_expiration");
        }
Exemplo n.º 2
0
        protected CacheBase GetDefaultCache()
        {
            var cache = (CacheBase)DefaultProvider.BuildCache(DefaultRegion, GetDefaultProperties());

            Assert.That(cache, Is.Not.Null, "No default cache returned");
            return(cache);
        }
Exemplo n.º 3
0
        public void TestSlidingExpiration()
        {
            if (!SupportsSlidingExpiration)
            {
                Assert.Ignore("Provider does not support sliding expiration settings");
            }

            const int    expirySeconds = 3;
            const string key           = "keyTestSlidingExpiration";
            var          obj           = new SomeObject {
                Id = 2
            };

            var props = GetPropertiesForExpiration(Cfg.Environment.CacheDefaultExpiration, expirySeconds.ToString());

            props["cache.use_sliding_expiration"] = "true";
            var cache = DefaultProvider.BuildCache("TestObjectExpiration", props);

            cache.Put(key, obj);
            // Wait up to 1 sec before expiration
            Thread.Sleep(TimeSpan.FromSeconds(expirySeconds - 1));
            Assert.That(cache.Get(key), Is.Not.Null, "Missing entry for key");

            // Wait up to 1 sec before expiration again
            Thread.Sleep(TimeSpan.FromSeconds(expirySeconds - 1));
            Assert.That(cache.Get(key), Is.Not.Null, "Missing entry for key after get and wait less than expiration");

            // Wait expiration
            Thread.Sleep(TimeSpan.FromSeconds(expirySeconds + 1));

            // Check it expired
            Assert.That(cache.Get(key), Is.Null, "Unexpected entry for key after expiration");
        }
Exemplo n.º 4
0
        protected ICache GetCacheForExpiration(string cacheRegion, string expirationSetting, int expirySeconds)
        {
            var props = GetPropertiesForExpiration(expirationSetting, expirySeconds.ToString());
            var cache = DefaultProvider.BuildCache(cacheRegion, props);

            return(cache);
        }
        public void TestExpiration()
        {
            var cache = DefaultProvider.BuildCache("foo", null) as SysCacheRegion;

            Assert.That(cache, Is.Not.Null, "pre-configured foo cache not found");
            Assert.That(RelativeExpirationField.GetValue(cache), Is.EqualTo(TimeSpan.FromSeconds(500)), "Unexpected expiration value for foo region");

            // In the case of SysCache2, due to the SQL notification callbacks which may be set up for each configured region,
            // the build provider handles a global region cache and guarantees uniqueness of yielded cache region per region name
            // for the whole application.
            cache = DefaultProvider.BuildCache("noExplicitExpiration1", null) as SysCacheRegion;
            Assert.That(RelativeExpirationField.GetValue(cache), Is.EqualTo(TimeSpan.FromSeconds(300)),
                        "Unexpected expiration value for noExplicitExpiration1 region");
            Assert.That(UseSlidingExpirationField.GetValue(cache), Is.True, "Unexpected sliding value for noExplicitExpiration1 region");

            cache = DefaultProvider
                    .BuildCache("noExplicitExpiration2", new Dictionary <string, string> {
                { "expiration", "100" }
            }) as SysCacheRegion;
            Assert.That(RelativeExpirationField.GetValue(cache), Is.EqualTo(TimeSpan.FromSeconds(100)),
                        "Unexpected expiration value for noExplicitExpiration2 region with default expiration");

            cache = DefaultProvider
                    .BuildCache("noExplicitExpiration3", new Dictionary <string, string> {
                { Cfg.Environment.CacheDefaultExpiration, "50" }
            }) as SysCacheRegion;
            Assert.That(RelativeExpirationField.GetValue(cache), Is.EqualTo(TimeSpan.FromSeconds(50)),
                        "Unexpected expiration value for noExplicitExpiration3 region with cache.default_expiration");
        }
Exemplo n.º 6
0
        public void TestExpiration()
        {
            var cache = DefaultProvider.BuildCache("foo", null) as RedisCache;

            Assert.That(cache, Is.Not.Null, "pre-configured foo cache not found");

            var strategy = cache.RegionStrategy;

            Assert.That(strategy, Is.Not.Null, "strategy was not set for the pre-configured foo cache");
            Assert.That(strategy, Is.TypeOf <DefaultRegionStrategy>(), "unexpected strategy type for foo region");
            Assert.That(strategy.Expiration, Is.EqualTo(TimeSpan.FromSeconds(500)), "unexpected expiration value for foo region");

            cache = (RedisCache)DefaultProvider.BuildCache("noExplicitExpiration", null);
            Assert.That(cache.RegionStrategy.Expiration, Is.EqualTo(TimeSpan.FromSeconds(300)),
                        "unexpected expiration value for noExplicitExpiration region");
            Assert.That(cache.RegionStrategy.UseSlidingExpiration, Is.True, "unexpected sliding value for noExplicitExpiration region");

            cache = (RedisCache)DefaultProvider
                    .BuildCache("noExplicitExpiration", new Dictionary <string, string> {
                { "expiration", "100" }
            });
            Assert.That(cache.RegionStrategy.Expiration, Is.EqualTo(TimeSpan.FromSeconds(100)),
                        "unexpected expiration value for noExplicitExpiration region with default expiration");

            cache = (RedisCache)DefaultProvider
                    .BuildCache("noExplicitExpiration", new Dictionary <string, string> {
                { Cfg.Environment.CacheDefaultExpiration, "50" }
            });
            Assert.That(cache.RegionStrategy.Expiration, Is.EqualTo(TimeSpan.FromSeconds(50)),
                        "unexpected expiration value for noExplicitExpiration region with cache.default_expiration");
        }
        public void TestNoExpiration()
        {
            var props = GetDefaultProperties();

            props["expiration"] = "0";
            Assert.Throws <CacheException>(() => DefaultProvider.BuildCache(DefaultRegion, props),
                                           "default region strategy should not allow to have no expiration");
        }
        public void TestPriorityOutOfRange()
        {
            var props = GetDefaultProperties();

            props["priority"] = 7.ToString();
            Assert.That(() => DefaultProvider.BuildCache("TestPriorityOutOfRange", props),
                        Throws.TypeOf <IndexOutOfRangeException>());
        }
        private RedisCache GetDefaultRedisCache(Dictionary <string, string> properties = null)
        {
            var props = GetDefaultProperties();

            foreach (var property in properties ?? new Dictionary <string, string>())
            {
                props[property.Key] = property.Value;
            }
            return((RedisCache)DefaultProvider.BuildCache(DefaultRegion, props));
        }
        private RedisCache GetFastRedisCache(Dictionary <string, string> properties = null)
        {
            var props = GetDefaultProperties();

            foreach (var property in properties ?? new Dictionary <string, string>())
            {
                props[property.Key] = property.Value;
            }
            props["strategy"] = typeof(FastRegionStrategy).AssemblyQualifiedName;
            return((RedisCache)DefaultProvider.BuildCache(DefaultRegion, props));
        }
Exemplo n.º 11
0
        public void TestBadRelativeExpiration([ValueSource(nameof(ExpirationSettingNames))] string expirationSetting)
        {
            if (!SupportsDefaultExpiration)
            {
                Assert.Ignore("Provider does not support default expiration settings");
            }

            var props = GetPropertiesForExpiration(expirationSetting, "foobar");

            Assert.That(
                () => DefaultProvider.BuildCache("TestBadRelativeExpiration", props),
                Throws.ArgumentException.Or.TypeOf <FormatException>(),
                expirationSetting);
        }
        public void TestMaxAllowedVersion()
        {
            var cache    = (RedisCache)GetDefaultCache();
            var strategy = (DefaultRegionStrategy)cache.RegionStrategy;
            var version  = strategy.CurrentVersion;

            var props = GetDefaultProperties();

            props.Add("cache.region_strategy.default.max_allowed_version", version.ToString());
            cache    = (RedisCache)DefaultProvider.BuildCache(DefaultRegion, props);
            strategy = (DefaultRegionStrategy)cache.RegionStrategy;

            cache.Clear();

            Assert.That(strategy.CurrentVersion, Is.EqualTo(1L), "the version was not reset to 1");
        }
Exemplo n.º 13
0
        public async Task TestRegionsAsync()
        {
            const string key    = "keyTestRegions";
            var          props  = GetDefaultProperties();
            var          cache1 = DefaultProvider.BuildCache("TestRegions1", props);
            var          cache2 = DefaultProvider.BuildCache("TestRegions2", props);
            const string s1     = "test1";
            const string s2     = "test2";

            await(cache1.PutAsync(key, s1, CancellationToken.None));
            await(cache2.PutAsync(key, s2, CancellationToken.None));
            var get1 = await(cache1.GetAsync(key, CancellationToken.None));
            var get2 = await(cache2.GetAsync(key, CancellationToken.None));

            Assert.That(get1, Is.EqualTo(s1), "Unexpected value in cache1");
            Assert.That(get2, Is.EqualTo(s2), "Unexpected value in cache2");
        }
Exemplo n.º 14
0
        public void TestRegions()
        {
            const string key    = "keyTestRegions";
            var          props  = GetDefaultProperties();
            var          cache1 = DefaultProvider.BuildCache("TestRegions1", props);
            var          cache2 = DefaultProvider.BuildCache("TestRegions2", props);
            const string s1     = "test1";
            const string s2     = "test2";

            cache1.Put(key, s1);
            cache2.Put(key, s2);
            var get1 = cache1.Get(key);
            var get2 = cache2.Get(key);

            Assert.That(get1, Is.EqualTo(s1), "Unexpected value in cache1");
            Assert.That(get2, Is.EqualTo(s2), "Unexpected value in cache2");
        }
        public void TestAppendHashcodeToKey()
        {
            Assert.That(CoreDistributedCacheProvider.AppendHashcodeToKey, Is.True, "Default is not true");

            var cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;

            Assert.That(cache.AppendHashcodeToKey, Is.True, "First built cache not correctly set");

            CoreDistributedCacheProvider.AppendHashcodeToKey = false;
            try
            {
                cache = DefaultProvider.BuildCache("foo", null) as CoreDistributedCache;
                Assert.That(cache.AppendHashcodeToKey, Is.False, "Second built cache not correctly set");
            }
            finally
            {
                CoreDistributedCacheProvider.AppendHashcodeToKey = true;
            }
        }
Exemplo n.º 16
0
        public void TestRemove144()
        {
            string key   = "keyTestRemove144";
            string value = "value";

            //memcached 1.4+ drops support for expiration time specified for Delete operations
            //therefore if you install memcached 1.4.4 this test will fail unless corresponding fix is implemented in MemCacheClient.cs
            //the test will fail because Remove won't actually delete the item from the cache!
            //the error you will see in the log is: "Error deleting key: nunit@key1.  Server response: CLIENT_ERROR bad command line format.  Usage: delete <key> [noreply]"

            //Now, Memcached.ClientLibrary incorrectly divides expiration time for Delete operation by 1000
            //(for Add and Set operations the expiration time is calculated correctly)
            //that's why we need to set expiration to 20000, otherwise it will be treated as 20ms which is too small to be sent to server (the minimum value is 1 second)
            var props = GetDefaultProperties();

            props["expiration"] = "20000";

            //disabling lingering delete will cause the item to get immediately deleted
            //this parameter is NEW and the code to make it work is part of the proposed fix
            props.Add("lingering_delete_disabled", "true");

            var cache = DefaultProvider.BuildCache("TestRemove144", props);

            Assert.That(cache, Is.Not.Null, "no cache returned");

            // add the item
            cache.Put(key, value);
            Thread.Sleep(1000);

            // make sure it's there
            var item = cache.Get(key);

            Assert.That(item, Is.Not.Null, "item just added is not there");

            // remove it
            cache.Remove(key);

            // make sure it's not there
            item = cache.Get(key);
            Assert.That(item, Is.Null, "item still exists in cache");
        }
        public void TestClearWithMultipleClientsAndNoPubSub()
        {
            const string key   = "keyTestClear";
            const string value = "valueClear";

            var props = GetDefaultProperties();

            props.Add("cache.region_strategy.default.use_pubsub", "false");

            var cache     = (RedisCache)DefaultProvider.BuildCache(DefaultRegion, props);
            var strategy  = (DefaultRegionStrategy)cache.RegionStrategy;
            var cache2    = (RedisCache)DefaultProvider.BuildCache(DefaultRegion, props);
            var strategy2 = (DefaultRegionStrategy)cache2.RegionStrategy;

            // add the item
            cache.Put(key, value);

            // make sure it's there
            var item = cache.Get(key);

            Assert.That(item, Is.Not.Null, "couldn't find item in cache");

            item = cache2.Get(key);
            Assert.That(item, Is.Not.Null, "couldn't find item in second cache");

            var version = strategy.CurrentVersion;

            // clear the cache
            cache.Clear();

            Assert.That(strategy.CurrentVersion, Is.EqualTo(version + 1), "the version has not been updated");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Assert.That(strategy2.CurrentVersion, Is.EqualTo(version), "the version should not be updated");

            // make sure we don't get an item
            item = cache.Get(key);
            Assert.That(item, Is.Null, "item still exists in cache after clear");

            item = cache2.Get(key);
            Assert.That(item, Is.Null, "item still exists in the second cache after clear");
        }
Exemplo n.º 18
0
        public virtual void TestEmptyProperties()
        {
            var cache = DefaultProvider.BuildCache("TestEmptyProperties", new Dictionary <string, string>());

            Assert.That(cache, Is.Not.Null);
        }
Exemplo n.º 19
0
        public void TestBuildCacheNoRegionNoProperties()
        {
            var cache = DefaultProvider.BuildCache(null, null);

            Assert.That(cache, Is.Not.Null, "no cache returned");
        }
Exemplo n.º 20
0
        public void TestBuildCacheRegionWithProperties()
        {
            var cache = DefaultProvider.BuildCache("TestBuildCacheWithProperties", GetDefaultProperties());

            Assert.That(cache, Is.Not.Null, "no cache returned");
        }
        public void TestBuildCacheFromConfig()
        {
            var cache = DefaultProvider.BuildCache("foo", null);

            Assert.That(cache, Is.Not.Null, "pre-configured cache not found");
        }