static void Main(string[] args) { Person p1 = new Person { Id = 1, Age = 10, Name = "" }; Person p2 = new Person { Id = 2, Age = 11, Name = "" }; Person p3 = new Person { Id = 3, Age = 12, Name = "" }; ICachingService <int, Person> cache = new CachingService <int, Person>(); //ICachingService<int, Person> cache = new CachingService<int, Person>(new LRUAlgorithm<int, Person>()); //ICachingService<int, Person> cache = new CachingService<int, Person>(new MRUAlgorithm<int, Person>()); cache.Add(1, p1); cache.Add(2, p2); cache.Add(3, p3); var rez = cache.Get(2); ICachingService <int, Person2> cache2 = new CachingService <int, Person2>(); Person2 p4 = new Person2 { Id = 1, Age = 13, Name = "" }; cache2.Add(1, p4); var rez2 = cache.Get(1); var rez3 = cache2.Get(1); }
public ComplexObject Init_CRUD() { var cache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions()))) as IAppCache; cache.Add(CacheKey, ComplexObject1); var obj = cache.Get <ComplexObject>(CacheKey); obj.Int = 256; cache.Add(CacheKey, obj); cache.Remove(CacheKey); return(obj); }
public void AddNullKeyThrowsExceptionWithSliding() { var sut = new CachingService(); Action act = () => sut.Add(null, new object(), new TimeSpan(1000)); act.ShouldThrow <ArgumentNullException>(); }
public void AddEmptyKeyThrowsExceptionWithExpiration() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), DateTimeOffset.Now.AddHours(1)); act.ShouldThrow <ArgumentOutOfRangeException>(); }
public void AddEmptyKeyThrowsExceptionWithPolicy() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), new CacheItemPolicy()); act.ShouldThrow <ArgumentOutOfRangeException>(); }
public void AddEmptyKeyThrowsExceptionWithSliding() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), new TimeSpan(1000)); act.ShouldThrow <ArgumentOutOfRangeException>(); }
public void AddNullKeyThrowsException() { var sut = new CachingService(); Action act = () => sut.Add(null, new object()); act.ShouldThrow <ArgumentNullException>(); }
public void AddNullKeyThrowsExceptionWithExpiration() { var sut = new CachingService(); Action act = () => sut.Add(null, new object(), DateTimeOffset.Now.AddHours(1)); act.ShouldThrow <ArgumentNullException>(); }
public void AddWithOffsetReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1)); Assert.AreEqual("testObject", sut.Get <string>(TestKey)); }
public void AddNullThrowsException() { var sut = new CachingService(); Action act = () => sut.Add <object>(TestKey, null); act.ShouldThrow <ArgumentNullException>(); }
public void AddWithPolicyReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new CacheItemPolicy()); Assert.AreEqual("testObject", sut.Get <string>(TestKey)); }
public void AddThenGetReturnsCachedObject() { var sut = new CachingService(); sut.Add(TestKey, "testObject"); Assert.AreEqual("testObject", sut.Get <string>(TestKey)); }
public void AddWithSlidingReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new TimeSpan(5000)); Assert.AreEqual("testObject", sut.Get <string>(TestKey)); }
public void AddWithOffsetThatExpiresReturnsNull() { var sut = new CachingService(); sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1)); Thread.Sleep(1500); Assert.IsNull(sut.Get <string>(TestKey)); }
public void AddWithSlidingThatExpiresReturnsNull() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new TimeSpan(750)); Thread.Sleep(1500); Assert.IsNull(sut.Get <string>(TestKey)); }
public void GetCachedNullableStructTypeParamReturnsType() { var sut = new CachingService(); DateTime?cached = new DateTime(); sut.Add(TestKey, cached); Assert.AreEqual(cached.Value, sut.Get <DateTime>(TestKey)); }
public void GetWithClassTypeParamReturnsType() { var sut = new CachingService(); var cached = new EventArgs(); sut.Add(TestKey, cached); Assert.AreEqual(cached, sut.Get <EventArgs>(TestKey)); }
public void GetWithWrongStructTypeParamReturnsNull() { var sut = new CachingService(); var cached = new DateTime(); sut.Add(TestKey, cached); Assert.AreEqual(new TimeSpan(), sut.Get <TimeSpan>(TestKey)); }
public void GetWithStructTypeParamReturnsType() { var sut = new CachingService(); var cached = new DateTime(2000, 1, 1); sut.Add(TestKey, cached); Assert.AreEqual(cached, sut.Get <DateTime>(TestKey)); }
public void GetWithValueTypeParamReturnsType() { var sut = new CachingService(); const int cached = 3; sut.Add(TestKey, cached); Assert.AreEqual(3, sut.Get <int>(TestKey)); }
public void GetWithWrongClassTypeParamReturnsNull() { var sut = new CachingService(); var cached = new EventArgs(); sut.Add(TestKey, cached); Assert.IsNull(sut.Get <ArgumentNullException>(TestKey)); }
public void GetWithNullableIntRetunsCachedNonNullableInt() { var sut = new CachingService(); const int expected = 123; sut.Add(TestKey, expected); Assert.AreEqual(expected, sut.Get <int?>(TestKey)); }
public void RemovedItemCannotBeRetrievedFromCache() { var sut = new CachingService(); sut.Add(TestKey, new object()); Assert.NotNull(sut.Get <object>(TestKey)); sut.Remove(TestKey); Assert.Null(sut.Get <object>(TestKey)); }
public void AddComplexObjectThenGetReturnsCachedObject() { var sut = new CachingService(); sut.Add(TestKey, new ComplexTestObject()); var actual = sut.Get<ComplexTestObject>(TestKey); var expected = new ComplexTestObject(); Assert.AreEqual(ComplexTestObject.SomeMessage, ComplexTestObject.SomeMessage); Assert.AreEqual(expected.SomeItems, actual.SomeItems); }
public virtual void AddThenGet_TestObject_AddAndGetEachInvokedOnce() { var cacheEntryKey = Fixture.Create <string>(); var expectedResult = Fixture.Create <TestObject>(); CachingService.Add(cacheEntryKey, expectedResult); var actualResult = CachingService.Get <TestObject>(cacheEntryKey); CachingService.Received(1).Add(cacheEntryKey, expectedResult, Arg.Any <MemoryCacheEntryOptions>()); CachingService.Received(1).Get <TestObject>(cacheEntryKey); }
public virtual void AddThenGet_TestObject_AddAndGetEachInvokedOnce() { var cacheEntryKey = Fixture.Create <string>(); var expectedResult = Fixture.Create <TestObject>(); CachingService.Add(cacheEntryKey, expectedResult); var actualResult = CachingService.Get <TestObject>(cacheEntryKey); Mock.Get(CachingService).Verify(m => m.Add(cacheEntryKey, expectedResult, It.IsAny <MemoryCacheEntryOptions>()), Times.Once); Mock.Get(CachingService).Verify(m => m.Get <TestObject>(cacheEntryKey), Times.Once); }
public async Task LazyCache_MemoryProviderAsync() { var lazyCache = new CachingService(); await LoopActionAsync(Iterations, async() => { lazyCache.Add("TestKey", 123, TimeSpan.FromDays(1)); await lazyCache.GetAsync <int>("TestKey"); await lazyCache.GetOrAddAsync("TestKey", () => Task.FromResult("Hello World"), TimeSpan.FromDays(1)); }); }
public void LazyCache_MemoryProvider() { var lazyCache = new CachingService(); LoopAction(Iterations, () => { lazyCache.Add("TestKey", 123, TimeSpan.FromDays(1)); lazyCache.Get <int>("TestKey"); lazyCache.GetOrAdd("TestKey", () => "Hello World", TimeSpan.FromDays(1)); }); }
public void AddComplexObjectThenGetReturnsCachedObject() { var sut = new CachingService(); sut.Add(TestKey, new ComplexTestObject()); var actual = sut.Get <ComplexTestObject>(TestKey); var expected = new ComplexTestObject(); Assert.AreEqual(ComplexTestObject.SomeMessage, ComplexTestObject.SomeMessage); Assert.AreEqual(expected.SomeItems, actual.SomeItems); }
public void GetOrAddWillNotAddIfExistingData() { var times = 0; var sut = new CachingService(); var cached = new DateTime(1999, 01, 01); sut.Add(TestKey, cached); var expected = sut.GetOrAdd(TestKey, () => { times++; return(new DateTime(2001, 01, 01)); }); Assert.AreEqual(1999, expected.Year); Assert.AreEqual(0, times); }
public void CachingService_MemoryCacheProvider_ShouldCache() { var key = nameof(CachingService_MemoryCacheProvider_ShouldCache); Assert.IsNull(_cacheService.Get <TestObject>(key)); _cacheService.Add(key, new TestObject { Name = "Test", Value = 1024 }); var val = _cacheService.Get <TestObject>(key); Assert.NotNull(val); Assert.AreEqual("Test", val.Name); Assert.AreEqual(1024, val.Value); // cacheService.Remove(key); }
public void GetCachedNullableStructTypeParamReturnsType() { var sut = new CachingService(); DateTime? cached = new DateTime(); sut.Add(TestKey, cached); Assert.AreEqual(cached.Value, sut.Get<DateTime>(TestKey)); }
public void GetOrAddWillNotAddIfExistingData() { var times = 0; var sut = new CachingService(); var cached = new DateTime(1999, 01, 01); sut.Add(TestKey, cached); var expected = sut.GetOrAdd(TestKey, () => { times++; return new DateTime(2001, 01, 01); }); Assert.AreEqual(1999, expected.Year); Assert.AreEqual(0, times); }
public void GetWithClassTypeParamReturnsType() { var sut = new CachingService(); var cached = new EventArgs(); sut.Add(TestKey, cached); Assert.AreEqual(cached, sut.Get<EventArgs>(TestKey)); }
public void GetWithNullableIntRetunsCachedNonNullableInt() { var sut = new CachingService(); const int expected = 123; sut.Add(TestKey, expected); Assert.AreEqual(expected, sut.Get<int?>(TestKey)); }
public void AddNullKeyThrowsExceptionWithPolicy() { var sut = new CachingService(); Action act = () => sut.Add(null, new object(), new CacheItemPolicy()); act.ShouldThrow<ArgumentNullException>(); }
public void AddWithPolicyReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new CacheItemPolicy()); Assert.AreEqual("testObject", sut.Get<string>(TestKey)); }
public void AddEmptyKeyThrowsExceptionWithSliding() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), new TimeSpan(1000)); act.ShouldThrow<ArgumentOutOfRangeException>(); }
public void GetWithWrongStructTypeParamReturnsNull() { var sut = new CachingService(); var cached = new DateTime(); sut.Add(TestKey, cached); Assert.AreEqual(new TimeSpan(), sut.Get<TimeSpan>(TestKey)); }
public void GetWithWrongClassTypeParamReturnsNull() { var sut = new CachingService(); var cached = new EventArgs(); sut.Add(TestKey, cached); Assert.IsNull(sut.Get<ArgumentNullException>(TestKey)); }
public void RemovedItemCannotBeRetrievedFromCache() { var sut = new CachingService(); sut.Add(TestKey, new object()); Assert.NotNull(sut.Get<object>(TestKey)); sut.Remove(TestKey); Assert.Null(sut.Get<object>(TestKey)); }
public void AddEmptyKeyThrowsExceptionWithExpiration() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), DateTimeOffset.Now.AddHours(1)); act.ShouldThrow<ArgumentOutOfRangeException>(); }
public void AddEmptyKeyThrowsExceptionWithPolicy() { var sut = new CachingService(); Action act = () => sut.Add("", new object(), new CacheItemPolicy()); act.ShouldThrow<ArgumentOutOfRangeException>(); }
public void AddWithSlidingThatExpiresReturnsNull() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new TimeSpan(750)); Thread.Sleep(1500); Assert.IsNull(sut.Get<string>(TestKey)); }
public void AddWithOffsetReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1)); Assert.AreEqual("testObject", sut.Get<string>(TestKey)); }
public void AddWithSlidingReturnsCachedItem() { var sut = new CachingService(); sut.Add(TestKey, "testObject", new TimeSpan(5000)); Assert.AreEqual("testObject", sut.Get<string>(TestKey)); }
public void AddNullKeyThrowsExceptionWithExpiration() { var sut = new CachingService(); Action act = () => sut.Add(null, new object(), DateTimeOffset.Now.AddHours(1)); act.ShouldThrow<ArgumentNullException>(); }
public void AddWithOffsetThatExpiresReturnsNull() { var sut = new CachingService(); sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1)); Thread.Sleep(1500); Assert.IsNull(sut.Get<string>(TestKey)); }
public void GetWithValueTypeParamReturnsType() { var sut = new CachingService(); const int cached = 3; sut.Add(TestKey, cached); Assert.AreEqual(3, sut.Get<int>(TestKey)); }
public void AddThenGetReturnsCachedObject() { var sut = new CachingService(); sut.Add(TestKey, "testObject"); Assert.AreEqual("testObject", sut.Get<string>(TestKey)); }
public void GetWithStructTypeParamReturnsType() { var sut = new CachingService(); var cached = new DateTime(2000, 1, 1); sut.Add(TestKey, cached); Assert.AreEqual(cached, sut.Get<DateTime>(TestKey)); }
public void AddNullKeyThrowsExceptionWithSliding() { var sut = new CachingService(); Action act = () => sut.Add(null, new object(), new TimeSpan(1000)); act.ShouldThrow<ArgumentNullException>(); }
public void AddNullThrowsException() { var sut = new CachingService(); Action act = () => sut.Add<object>(TestKey, null); act.ShouldThrow<ArgumentNullException>(); }