Exemplo n.º 1
0
        public void Create_Default()
        {
            var service = new CacheEntryOptionsFactory();
            var result  = service.Create <string>();

            Assert.Equal(14, result.SlidingExpiration.Value.Minutes);
        }
Exemplo n.º 2
0
        public void MemoryCache_Sliding_Expire()
        {
            var memoryCache = MemoryCacheProvider.Default;

            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(300));

            memoryCache.Set(key, "something", options);

            Thread.Sleep(250);
            Assert.Equal("something", memoryCache.Get <string>(key));

            Thread.Sleep(250);
            Assert.Equal("something", memoryCache.Get <string>(key));

            Thread.Sleep(300);
            Assert.Null(memoryCache.Get <string>(key));

            options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2));
            memoryCache.Set(key, "something", options);

            Thread.Sleep(1800);
            Assert.Equal("something", memoryCache.Get <string>(key));

            Thread.Sleep(1800);
            Assert.Equal("something", memoryCache.Get <string>(key));

            Thread.Sleep(2000);
            Assert.Null(memoryCache.Get <string>(key));
        }
Exemplo n.º 3
0
        public void Create_NotRemoveable()
        {
            var service = new CacheEntryOptionsFactory();
            var result  = service.Create(new CacheOptions <string>(CacheExpirationType.NotRemoveable));

            Assert.Equal(CacheItemPriority.NeverRemove, result.Priority);
        }
Exemplo n.º 4
0
        public void Create_Absolute()
        {
            var service = new CacheEntryOptionsFactory();
            var result  = service.Create(new CacheOptions <string>(CacheExpirationType.Absolute, 5));

            Assert.Equal(5, result.AbsoluteExpirationRelativeToNow.Value.Minutes);
        }
Exemplo n.º 5
0
        public void DistributedCache_Sliding_Expire()
        {
            var distributedCache = GetDistributedCache();

            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(500));

            distributedCache.Set(key, "something", options);

            Thread.Sleep(400);
            Assert.Equal("something", distributedCache.Get <string>(key));

            Thread.Sleep(400);
            Assert.Equal("something", distributedCache.Get <string>(key));

            Thread.Sleep(600);
            Assert.Null(distributedCache.Get <string>(key));

            options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2));
            distributedCache.Set(key, "something", options);

            Thread.Sleep(1800);
            Assert.Equal("something", distributedCache.Get <string>(key));

            Thread.Sleep(1800);
            Assert.Equal("something", distributedCache.Get <string>(key));

            Thread.Sleep(2200);
            Assert.Null(distributedCache.Get <string>(key));
        }
Exemplo n.º 6
0
        public async Task DistributedCache_Sliding_Expire_Async()
        {
            var distributedCache = GetDistributedCache();

            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromMilliseconds(1000));
            await distributedCache.SetAsync(key, "something", options).ConfigureAwait(false);

            Thread.Sleep(800);
            Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false));

            Thread.Sleep(800);
            Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false));

            Thread.Sleep(1200);
            Assert.Null(await distributedCache.GetAsync <string>(key).ConfigureAwait(false));

            options = CacheEntryOptionsFactory.Sliding().Timeout(TimeSpan.FromSeconds(2));
            await distributedCache.SetAsync(key, "something", options).ConfigureAwait(false);

            Thread.Sleep(1800);
            Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false));

            Thread.Sleep(1800);
            Assert.Equal("something", await distributedCache.GetAsync <string>(key).ConfigureAwait(false));

            Thread.Sleep(2200);
            Assert.Null(await distributedCache.GetAsync <string>(key).ConfigureAwait(false));
        }
Exemplo n.º 7
0
        public void Create_Callback()
        {
            var service = new CacheEntryOptionsFactory();
            var result  = service.Create(new CacheOptions <string>
            {
                DidRemove = (x) => { }
            });

            Assert.Equal(1, result.PostEvictionCallbacks.Count);
        }
Exemplo n.º 8
0
        public void Create_Sliding()
        {
            var service = new CacheEntryOptionsFactory();
            var result  = service.Create(new CacheOptions <string>
            {
                TimeoutMinutes = 5
            });

            Assert.Equal(5, result.SlidingExpiration.Value.Minutes);
        }
Exemplo n.º 9
0
        public void DistributedCache_Absolute_Expire()
        {
            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(300));

            var distributedCache = GetDistributedCache();

            distributedCache.Set(key, "something", options);

            Assert.Equal("something", distributedCache.Get <string>(key));

            Thread.Sleep(320);

            Assert.Null(distributedCache.Get <string>(key));
        }
Exemplo n.º 10
0
        public void CacheService_GetOrAdd()
        {
            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(1000));

            var cacheService = GetCacheService();

            cacheService.GetOrAdd(key, (k) => { return("something"); }, options);
            Assert.Equal("something", cacheService.Get <string>(key));
            Thread.Sleep(1300);
            Assert.Null(cacheService.Get <string>(key));

            var region = "test";

            cacheService.GetOrAdd(key, (k) => { return("something"); }, options, region);
            Assert.Equal("something", cacheService.Get <string>(key, region));
            Thread.Sleep(1300);
            Assert.Null(cacheService.Get <string>(key, region));
        }
Exemplo n.º 11
0
        public void CacheService_GetOrAdd_Async_Concurrent()
        {
            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromHours(1));

            var cacheService = GetCacheService();

            var tasks = new List <Task>();

            _value = null;

            //concurrent write
            for (int i = 0; i < 1000; i++)
            {
                Func <int, Task> taskFunc = async(j) =>
                {
                    await Task.Yield();

                    var value = await cacheService.GetOrAddAsync(key, ValueAsyncFactory, options).ConfigureAwait(false);

                    Assert.Equal(_value, value);
                };

                tasks.Add(taskFunc(i));
            }
            Task.WaitAll(tasks.ToArray());

            //concurrent read
            for (int i = 0; i < 1000; i++)
            {
                Func <int, Task> taskFunc = async(j) =>
                {
                    await Task.Yield();

                    var value = await cacheService.GetOrAddAsync(key, ValueAsyncFactory, options).ConfigureAwait(false);

                    Assert.Equal(_value, value);
                };

                tasks.Add(taskFunc(i));
            }
            Task.WaitAll(tasks.ToArray());
        }
Exemplo n.º 12
0
        protected static void RedisCache_Absolute_Expire(RedisCacheProvider redisCache)
        {
            Console.WriteLine("Sync test absolute expire start...");
            Console.Write(Environment.NewLine);

            var key     = Guid.NewGuid().ToString("N");
            var value   = "something";
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(300));

            redisCache.Set(key, value, options);
            Console.WriteLine(string.Format("Set key:{0} with value:{1} by expire:{2}",
                                            key, value, options.ExpirationTimeout.TotalMilliseconds));

            Console.WriteLine(string.Format("Get key:{0} with value:{1}", key, redisCache.Get <string>(key)));

            Thread.Sleep(300);
            Console.WriteLine(string.Format("After expire:{0} get key:{1} with value:{2}",
                                            options.ExpirationTimeout.TotalMilliseconds, key, redisCache.Get <string>(key)));

            Console.Write(Environment.NewLine);
            Console.WriteLine("Sync test absolute expire end...");
        }
Exemplo n.º 13
0
        protected static async Task RedisCache_Absolute_Expire_Async(RedisCacheProvider redisCache)
        {
            Console.WriteLine("Async test absolute expire start...");
            Console.Write(Environment.NewLine);

            var key     = Guid.NewGuid().ToString("N");
            var value   = "something";
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(300));

            await redisCache.SetAsync(key, value, options).ConfigureAwait(false);

            Console.WriteLine(string.Format("Async set key:{0} with value:{1} by expire:{2}",
                                            key, value, options.ExpirationTimeout.TotalMilliseconds));

            Console.WriteLine(string.Format("Async get key:{0} with value:{1}", key, await redisCache.GetAsync <string>(key).ConfigureAwait(false)));

            Thread.Sleep(350);
            Console.WriteLine(string.Format("After expire:{0} async get key:{1} with value:{2}",
                                            options.ExpirationTimeout.TotalMilliseconds, key, await redisCache.GetAsync <string>(key).ConfigureAwait(false)));

            Console.Write(Environment.NewLine);
            Console.WriteLine("Async test absolute expire end...");
        }
Exemplo n.º 14
0
        public async Task CacheService_GetOrAdd_Async()
        {
            var key     = Guid.NewGuid().ToString("N");
            var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(1000));

            var cacheService = GetCacheService();

            await cacheService.GetOrAddAsync(key,
                                             async (k) => { await Task.Yield(); return("something"); },
                                             options).ConfigureAwait(false);

            Assert.Equal("something", await cacheService.GetAsync <string>(key).ConfigureAwait(false));
            Thread.Sleep(1300);
            Assert.Null(await cacheService.GetAsync <string>(key).ConfigureAwait(false));

            var region = "test";
            await cacheService.GetOrAddAsync(key,
                                             async (k) => { await Task.Yield(); return("something"); },
                                             options, region).ConfigureAwait(false);

            Assert.Equal("something", await cacheService.GetAsync <string>(key, region).ConfigureAwait(false));
            Thread.Sleep(1300);
            Assert.Null(await cacheService.GetAsync <string>(key, region).ConfigureAwait(false));
        }