public void SlidingExpiryTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromSeconds(4) }; provider.Set("key8", "data8", policy, "testRegion"); // Wait for 500 seconds, get the data to reset the exiration System.Threading.Thread.Sleep(2000); object data = provider.Get("key8", "testRegion"); Assert.Equal("data8", data); // 1.1 sec after intial insert, but should still be there. System.Threading.Thread.Sleep(2400); data = provider.Get("key8", "testRegion"); Assert.Equal("data8", data); // Wait for 1.1 seconds so that data will expire System.Threading.Thread.Sleep(4400); data = provider.Get("key8", "testRegion"); Assert.Equal(null, data); } }
public void NameTest() { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); Assert.Equal("test", provider.Name); }
public void GetWithoutSetTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); Assert.Equal(null, provider.Get("key1")); } }
public void DoesNotContainsTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddMinutes(3); Assert.False(provider.Contains("key10", "testRegion")); } }
public void TryRemove() { var fake = A.Fake <IObjectCacheConnection>(); A.CallTo(() => fake.Remove("key1", null)); DateTime utcExpiry = DateTime.Now; RedisObjectCache cache = new RedisObjectCache("unitTest", new NameValueCollection()); cache.cache = fake; cache.Remove("key1"); A.CallTo(() => fake.Remove("key1", null)).MustHaveHappened(); }
public void TryGet() { var fake = A.Fake <IObjectCacheConnection>(); A.CallTo(() => fake.Get("key1", null)).Returns(new ArgumentException("foo")); RedisObjectCache cache = new RedisObjectCache("unitTest", new NameValueCollection()); cache.cache = fake; var obj = cache.Get("key1"); Assert.IsType <ArgumentException>(obj); }
public void ContainsTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddSeconds(1); provider.Set("key10", "data10", utxExpiry, "testRegion"); Assert.True(provider.Contains("key10", "testRegion")); } }
public void GetWithSlidingExpiration() { var fake = A.Fake <IObjectCacheConnection>(); A.CallTo(() => fake.Get("key1", null)).Returns(new SlidingExpiryCacheItem("foo", TimeSpan.FromMinutes(1))); RedisObjectCache cache = new RedisObjectCache("unitTest", new NameValueCollection()); cache.cache = fake; var obj = cache.Get("key1"); Assert.Equal("foo", obj); A.CallTo(() => fake.ResetExpiry("key1", A <DateTime> .Ignored, null)).MustHaveHappened(); }
public void RemoveWithoutSetTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); provider.Remove("key6", "testRegion"); object data = provider.Get("key6", "testRegion"); Assert.Equal(null, data); } }
public void TryAdd() { var fake = A.Fake <IObjectCacheConnection>(); DateTime utcExpiry = DateTime.Now; A.CallTo(() => fake.AddOrGetExisting("key1", "object", A <DateTime> .Ignored, null)).Returns(null); RedisObjectCache cache = new RedisObjectCache("unitTest", new NameValueCollection()); cache.cache = fake; var obj = cache.Add("key1", "object", utcExpiry); Assert.True(obj); }
public void SetGetIndexerTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddSeconds(3); provider["key2"] = "data2"; object data = provider["key2"]; Assert.Equal("data2", data); } }
public void AddWithoutSetTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddSeconds(3); Assert.True(provider.Add("key4", "data4", utxExpiry, "testRegion")); object data = provider.Get("key4", "testRegion"); Assert.Equal("data4", data); } }
public void RemoveTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddSeconds(3); provider.Set("key7", "data7", utxExpiry, "testRegion"); provider.Remove("key7", "testRegion"); object data = provider.Get("key7", "testRegion"); Assert.Equal(null, data); } }
public void AddWithSlidingExperation() { var fake = A.Fake <IObjectCacheConnection>(); A.CallTo(() => fake.AddOrGetExisting("key1", A <SlidingExpiryCacheItem> .Ignored, A <DateTime> .Ignored, null)).Returns(null); RedisObjectCache cache = new RedisObjectCache("unitTest", new NameValueCollection()); cache.cache = fake; var obj = cache.Add("key1", "object", new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(5) }); //Assert.IsType<bool>(obj); //Assert.True(obj); A.CallTo(() => fake.AddOrGetExisting("key1", A <SlidingExpiryCacheItem> .That.Matches(s => (string)s.Value == "object"), A <DateTime> .Ignored, null)).MustHaveHappened(); }
public void ExpiryTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddSeconds(1); provider.Set("key8", "data8", utxExpiry, "testRegion"); // Wait for 1.1 seconds so that data will expire System.Threading.Thread.Sleep(1100); object data = provider.Get("key8", "testRegion"); Assert.Equal(null, data); } }
public void RegionsTest() { using (new RedisServer()) { NameValueCollection config = new NameValueCollection(); config.Add("ssl", "false"); RedisObjectCache provider = new RedisObjectCache("test", config); DateTime utxExpiry = DateTime.UtcNow.AddMinutes(3); provider.Set("key11", "data11.1", utxExpiry, "region1"); provider.Set("key11", "data11.2", utxExpiry, "region2"); object region1Data = provider.Get("key11", "region1"); object region2Data = provider.Get("key11", "region2"); object regionlessData = provider.Get("key11"); Assert.Equal("data11.1", region1Data); Assert.Equal("data11.2", region2Data); Assert.Equal(null, regionlessData); } }
/// <summary> /// 添加缓存服务 /// </summary> /// <param name="services">The services.</param> public static IServiceCollection AddCacheService(this IServiceCollection services) { services.AddMemoryCache(); var cacheScheme = RuntimeContext.Current.WebsiteConfig.CacheScheme; // 使用内存作为缓存时 ICacheContext cacheContext = new MemoryCacheContext(); IObjectCache objectCache = new MemoryObjectCache(cacheContext); if (cacheScheme == "redis") { // 使用redis做为缓存时 ICacheConfiguration cacheConfiguration = new CacheConfiguration(RuntimeContext.Current.CacheConfigurationString); cacheContext = new RedisCacheContext(cacheConfiguration); objectCache = new RedisObjectCache(cacheContext); } services.AddSingleton(cacheContext); services.AddSingleton(objectCache); return(services); }