public void DoubleRetrieve() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(existsRetrievedWrapper); Assert.NotNull(retrievedWrapper); Assert.Equal(cachable, existsRetrievedWrapper.Value); Assert.Equal(retrievedWrapper.Value, existsRetrievedWrapper.Value); Assert.Single(service); }
public void AutomatedExpiration() { TimeSpan cacheLifeTime = TimeSpan.FromMilliseconds(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); Thread.Sleep(6); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(existsRetrievedWrapper); Assert.Null(retrievedWrapper); }
public void ManualExpiration() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); service.Invalidate(cachableKey); ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.Empty(service); Assert.NotNull(existsRetrievedWrapper); Assert.Null(retrievedWrapper); }
public void StaticUsage() { TimeSpan cacheLifeTime = TimeSpan.FromMilliseconds(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime, true); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); MemoryCachingService service2 = new MemoryCachingService(cacheLifeTime, true); ICachedObject <Foobar> existsRetrievedWrapper2 = service2.Retrieve <Foobar>(cachableKey); Assert.Equal(cachable, existsRetrievedWrapper.Value); Assert.Equal(existsRetrievedWrapper.Value, existsRetrievedWrapper2.Value); service2.Clear(); Assert.Null(service.Retrieve <Foobar>(cachableKey)); }
public void AddKvp() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; KeyValuePair <string, ICachedObjectBasic> item = GetCachableKvp(DateTime.UtcNow, cacheLifeTime, cachable, cachableKey); service.Add(item); ICachedObject <Foobar> result = service.Retrieve <Foobar>(cachableKey); Assert.Equal(cachable, result.Value); }
public void Clear() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; Foobar cachable = GetCachableObject(); service.Cache <Foobar>(cachableKey, cachable); Assert.Single(service);; Assert.Equal(cachable, service.Retrieve <Foobar>(cachableKey).Value); service.Clear(); Assert.Empty(service); }
public void Add() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); string cachableKey = this.cachableKey; Foobar cachable = GetCachableObject(); DefaultCachedObject <object> dto = new DefaultCachedObject <object> { Value = cachable, CachedTime = DateTime.UtcNow, ExpireTime = DateTime.UtcNow.Add(cacheLifeTime) }; service.Add(cachableKey, dto); Assert.Single(service);; Assert.Equal(dto.Value, service.Retrieve <Foobar>(cachableKey).Value); }
public void CopyTo() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; service.Cache(cachableKey, cachable); ICachedObject <Foobar> existsRetrievedWrapper = service.Retrieve <Foobar>(cachableKey); KeyValuePair <string, ICachedObjectBasic>[] cache = new KeyValuePair <string, ICachedObjectBasic> [1]; service.CopyTo(cache, 0); Assert.NotNull(cache); Assert.NotEmpty(cache); Assert.Single(cache); Assert.Equal(existsRetrievedWrapper.Value, cache[0].Value.UntypedValue); Assert.Equal(cachableKey, cache[0].Key); }
public void Retrieval() { TimeSpan cacheLifeTime = TimeSpan.FromMinutes(5); MemoryCachingService service = new MemoryCachingService(cacheLifeTime); Foobar cachable = GetCachableObject(); string cachableKey = this.cachableKey; DateTime startedCaching = DateTime.UtcNow; service.Cache(cachableKey, cachable); DateTime endedCaching = DateTime.UtcNow; ICachedObject <Foobar> retrievedWrapper = service.Retrieve <Foobar>(cachableKey); Assert.NotNull(retrievedWrapper); Assert.Equal(cachable, retrievedWrapper.Value); Assert.True(retrievedWrapper.CachedTime > startedCaching && retrievedWrapper.CachedTime < endedCaching); Assert.True(retrievedWrapper.ExpireTime > startedCaching.Add(cacheLifeTime) && retrievedWrapper.ExpireTime < endedCaching.Add(cacheLifeTime)); }