public void SlidingExpirationRenewedByAccessUntilAbsoluteExpiration() { var cache = MongoDbCacheConfig.CreateCacheInstance(); const string key = "myKey4"; var value = new byte[1]; cache.Set(key, value, new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromSeconds(1)) .SetAbsoluteExpiration(TimeSpan.FromSeconds(3))); var result = cache.Get(key); Assert.Equal(value, result); for (var i = 0; i < 5; i++) { Thread.Sleep(TimeSpan.FromSeconds(0.5)); result = cache.Get(key); Assert.Equal(value, result); } Thread.Sleep(TimeSpan.FromSeconds(1)); result = cache.Get(key); Assert.Null(result); }
public void AddMongoDbCache_ReplaceUserRegisteredServices() { // Arrange var services = new ServiceCollection(); services.AddSingleton <IDistributedCache, TestDistributedCache>(); var defaultOptions = MongoDbCacheConfig.CreateOptions(); // Act services.AddMongoDbCache(options => { options.ConnectionString = defaultOptions.ConnectionString; options.DatabaseName = defaultOptions.DatabaseName; options.CollectionName = defaultOptions.CollectionName; options.ExpiredScanInterval = defaultOptions.ExpiredScanInterval; }); // Assert var serviceProvider = services.BuildServiceProvider(); var distributedCache = services.FirstOrDefault(desc => desc.ServiceType == typeof(IDistributedCache)); Assert.NotNull(distributedCache); Assert.Equal(ServiceLifetime.Singleton, distributedCache.Lifetime); Assert.IsType <MongoDbCache>(serviceProvider.GetRequiredService <IDistributedCache>()); }
public void GetMissingKeyReturnsNull() { var cache = MongoDbCacheConfig.CreateCacheInstance(); const string key = "non-existent-key"; var result = cache.Get(key); Assert.Null(result); }
public void RefreshRefreshes() { var cache = MongoDbCacheConfig.CreateCacheInstance(); var value = new byte[1]; const string key = "myKeyToBeRefreshed"; cache.Set(key, value); cache.Refresh(key); var result = cache.Get(key); Assert.Equal(value, result); }
public void SetAndGetReturnsObject() { var cache = MongoDbCacheConfig.CreateCacheInstance(); var value = new byte[1]; const string key = "myKey"; cache.Set(key, value); var result = cache.Get(key); Assert.Equal(value, result); }
public void SetAndGetWorksWithCaseSensitiveKeys() { var cache = MongoDbCacheConfig.CreateCacheInstance(); var value = new byte[1]; const string key1 = "myKey"; const string key2 = "Mykey"; cache.Set(key1, value); var result = cache.Get(key1); Assert.Equal(value, result); result = cache.Get(key2); Assert.Null(result); }
public void AddMongoDbCache_RegistersDistributedCacheAsSingleton() { // Arrange var services = new ServiceCollection(); // Act services.AddMongoDbCache(options => { options = MongoDbCacheConfig.CreateOptions(); }); // Assert var distributedCache = services.FirstOrDefault(desc => desc.ServiceType == typeof(IDistributedCache)); Assert.NotNull(distributedCache); Assert.Equal(ServiceLifetime.Singleton, distributedCache.Lifetime); }
public void SetAlwaysOverwrites() { var cache = MongoDbCacheConfig.CreateCacheInstance(); var value1 = new byte[] { 1 }; const string key = "myKey"; cache.Set(key, value1); var result = cache.Get(key); Assert.Equal(value1, result); var value2 = new byte[] { 2 }; cache.Set(key, value2); result = cache.Get(key); Assert.Equal(value2, result); }
public void SlidingExpirationExpiresIfNotAccessed() { var cache = MongoDbCacheConfig.CreateCacheInstance(); const string key = "myKey2"; var value = new byte[1]; cache.Set(key, value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(1))); var result = cache.Get(key); Assert.Equal(value, result); Thread.Sleep(TimeSpan.FromSeconds(3)); result = cache.Get(key); Assert.Null(result); }
public void AbsoluteExpirationExpires() { var cache = MongoDbCacheConfig.CreateCacheInstance(); const string key = "myKey1"; var value = new byte[1]; cache.Set(key, value, new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1))); var result = cache.Get(key); Assert.Equal(value, result); for (var i = 0; i < 4 && (result != null); i++) { Thread.Sleep(TimeSpan.FromSeconds(0.5)); result = cache.Get(key); } Assert.Null(result); }