コード例 #1
0
        public void Remove()
        {
            var cacheFactory = CacheFactory.Create()
                               .SetProperty("log-level", "none")
                               .SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

            var         cache       = (Cache)cacheFactory.CreateCache();
            PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
            var         pool        = poolFactory.CreatePool("myPool");

            var ssCache = new SessionStateCache(cache, _regionName);

            var            options       = new DistributedCacheEntryOptions();
            DateTime       localTime     = DateTime.Now.AddDays(1);
            DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
                                                              TimeZoneInfo.Local.GetUtcOffset(localTime));

            options.AbsoluteExpiration = dateAndOffset;
            var testValue = new byte[] { 1, 2, 3, 4, 5 };

            ssCache.Set("testKey", testValue, options);
            byte[] value = ssCache.Get("testKey");

            ssCache.Remove("testKey");
            value = ssCache.Get("testKey");
            Assert.Null(value);
        }
コード例 #2
0
        public void Refresh()
        {
            var cacheFactory = CacheFactory.Create()
                               .SetProperty("log-level", "none")
                               .SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

            var         cache       = (Cache)cacheFactory.CreateCache();
            PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
            var         pool        = poolFactory.CreatePool("myPool");

            var ssCache = new SessionStateCache(cache, _regionName);

            var options    = new DistributedCacheEntryOptions();
            int numSeconds = 20;

            options.SlidingExpiration = new TimeSpan(0, 0, numSeconds);
            var testValue = new byte[] { 1, 2, 3, 4, 5 };

            // Set a value
            ssCache.Set("testKey", testValue, options);

            // Wait half a timeout then refresh
            System.Threading.Thread.Sleep(numSeconds / 2 * 1000);
            ssCache.Refresh("testKey");

            // Wait beyond the original expiration
            System.Threading.Thread.Sleep(numSeconds / 2 * 1000 + 1);

            // Ensure it's not expired
            byte[] value = ssCache.Get("testKey");
            Assert.True(testValue.SequenceEqual(value));
        }
コード例 #3
0
        public void SetWithAbsoluteExpiration()
        {
            var cacheFactory = CacheFactory.Create()
                               .SetProperty("log-level", "none")
                               .SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

            var         cache       = (Cache)cacheFactory.CreateCache();
            PoolFactory poolFactory = cache.PoolFactory;

            var ssCache = new SessionStateCache(cache, _regionName);

            var options = new DistributedCacheEntryOptions();

            options.AbsoluteExpiration = DateTime.Now.AddSeconds(5);
            ssCache.Set("testKey", Encoding.UTF8.GetBytes("testValue"), options);
            System.Threading.Thread.Sleep(6000);
            byte[] value = ssCache.Get("testKey");
            Assert.Null(value);
        }