public void GetBasketWithExecuteFunctionWithRedirectTest() { const string cacheName = "AccessClient"; const int basketId = 1; var basket = new ObjectWithAnId { Id = basketId }; var cacheManager = new CacheManager(); var cache = new InMemoryTestCache(cacheName); cacheManager.AddCache(cache); var cacheKeyBasket = cacheManager.GetKey("GetBasket", basketId); //Act var cached = cacheManager.ExecuteFunction(cacheName, cacheKeyBasket, () => basket); // Assert Assert.AreSame(cached, basket); Assert.IsTrue(cacheManager.TryGet(cacheName, cacheKeyBasket, out cached)); Assert.IsTrue(cache.TryGet("Basket1", out cached)); Assert.AreSame(cached, basket); }
public void FlushItemsByTagTest() { // Arrange const string cacheName = "AccessClient"; var cacheManager = new CacheManager(); var cache = new InMemoryTestCache(cacheName); cacheManager.AddCache(cache); ObjectWithAnId ignore; int i; // Act var key = cacheManager.GetKey("GetSomethingWithFlush", 1); var somethingIn = new ObjectWithAnId { Id = 1 }; var somethingOut = cacheManager.ExecuteFunction(cacheName, key, "Tag" + 1, () => somethingIn); Assert.IsTrue(cacheManager.TryGet(cacheName, key, out ignore)); key = cacheManager.GetKey("GetWhateverWithFlush", 1); const int whateverIn = 1; var whateverOut = cacheManager.ExecuteFunction(cacheName, key, "Tag" + 1, () => whateverIn); Assert.IsTrue(cacheManager.TryGet(cacheName, key, out i)); key = cacheManager.GetKey("GetSomethingWithFlush", 2); var somethingIn2 = new ObjectWithAnId { Id = 2 }; var somethingOut2 = cacheManager.ExecuteFunction(cacheName, key, "Tag" + 2, () => somethingIn2); Assert.IsTrue(cacheManager.TryGet(cacheName, key, out ignore)); // Assert 1 Assert.AreSame(somethingIn, somethingOut); Assert.AreEqual(whateverIn, whateverOut); Assert.AreSame(somethingIn2, somethingOut2); // Act 2 var updated = new ObjectWithAnId { Id = 1 }; cacheManager.Flush(cacheName, "Tag" + updated.Id); var updateKey = cacheManager.GetKey("GetSomethingWithFlush", updated.Id); cacheManager.Add(cacheName, updateKey, "Tag" + 1, updated); // Assert 2 key = cacheManager.GetKey("GetWhateverWithFlush", 1); Assert.IsFalse(cacheManager.TryGet(cacheName, key, out i)); key = cacheManager.GetKey("GetSomethingWithFlush", 1); Assert.IsTrue(cacheManager.TryGet(cacheName, key, out ignore)); key = cacheManager.GetKey("GetSomethingWithFlush", 2); Assert.IsTrue(cacheManager.TryGet(cacheName, key, out ignore)); }
public void GetBasketThenInsertWithCacheRefresh() { // Arrange const string cacheName = "AccessClient"; const int basketId = 1; var basket = new ObjectWithAnId { Id = basketId }; var checkout = new ObjectWithAnId { Id = basketId }; var cacheManager = new CacheManager(); var cache = new InMemoryTestCache(cacheName); cacheManager.AddCache(cache); var cacheKeyBasket = cacheManager.GetKey("GetBasket", basketId); var cacheKeyCheckout = cacheManager.GetKey("GetCheckout", basketId); //Act if (!cacheManager.HasConfiguration(cacheName)) { Assert.Fail("Should have a cache config file"); } var success = cacheManager.Add(cacheName, cacheKeyBasket, basket); Assert.IsTrue(success, "basket should be added."); success = cacheManager.Add(cacheName, cacheKeyCheckout, checkout); Assert.IsTrue(success, "Checkout should be added."); object cached; var cacheKeyInsertBasket = cacheManager.GetKey("InsertBasketItem", basket); success = cacheManager.TryGet(cacheName, cacheKeyInsertBasket, out cached); Assert.IsFalse(success, "Should not be there on that key."); var newBasket = new ObjectWithAnId { Id = basketId }; success = cacheManager.Add(cacheName, cacheKeyInsertBasket, newBasket); Assert.IsTrue(success, "New basket should now be added on original key."); cacheManager.TryGet(cacheName, cacheKeyBasket, out cached); Assert.AreSame(newBasket, cached, "Now it should be cached."); cacheManager.TryGet(cacheName, cacheKeyCheckout, out cached); Assert.IsNull(cached, "Checkout should have been flushed"); }
public void GetBasketWithExecuteFunctionWithRedirectAndClearTest() { const string cacheName = "AccessClient"; const int basketId = 1; var basket = new ObjectWithAnId { Id = basketId }; var checkout = new ObjectWithAnId { Id = basketId }; var cacheManager = new CacheManager(); var cache = new InMemoryTestCache(cacheName); cacheManager.AddCache(cache); var cacheKeyBasket = cacheManager.GetKey("GetBasket", basketId); var cacheKeyCheckout = cacheManager.GetKey("GetCheckout", basketId); //Act var cachedCheckout = cacheManager.ExecuteFunction(cacheName, cacheKeyCheckout, () => checkout); var cachedBasket = cacheManager.ExecuteFunction(cacheName, cacheKeyBasket, () => basket); // Assert Assert.AreSame(cachedBasket, basket); Assert.IsTrue(cacheManager.TryGet(cacheName, cacheKeyBasket, out cachedBasket)); Assert.AreSame(cachedCheckout, checkout); Assert.IsTrue(cacheManager.TryGet(cacheName, cacheKeyCheckout, out cachedCheckout)); // Act 2 var modifiedBasket = new ObjectWithAnId { Id = basketId }; var cacheKeyInsert = cacheManager.GetKey("InsertBasketItem", modifiedBasket.Id); cacheManager.Add(cacheName, cacheKeyInsert, modifiedBasket); // Assert 2 Assert.IsFalse(cacheManager.TryGet(cacheName, cacheKeyCheckout, out cachedCheckout)); Assert.IsTrue(cacheManager.TryGet(cacheName, cacheKeyBasket, out cachedBasket)); Assert.AreSame(cachedBasket, modifiedBasket); }
public void GetNullDataAndThenFlushItToCreateNew() { // Arrange const string cacheName = "AccessClient"; const string email = "*****@*****.**"; var newObject = new ObjectWithAnId { Id = 1 }; var cacheManager = new CacheManager(); var cache = new InMemoryTestCache(cacheName); cacheManager.AddCache(cache); var cacheKey = cacheManager.GetKey("GetCustomerByEmail", email); //Act if (!cacheManager.HasConfiguration(cacheName)) { Assert.Fail("Should have a cache config file"); } var added = cacheManager.Add(cacheName, cacheKey, (ObjectWithAnId)null); Assert.IsFalse(added, "Should not be added."); object cached; cacheKey = cacheManager.GetKey("CreateCustomer", newObject); cacheManager.TryGet(cacheName, cacheKey, out cached); Assert.IsNull(cached, "Should not have value in cache"); added = cacheManager.Add(cacheName, cacheKey, newObject); Assert.IsFalse(added, "Should not add creates."); cacheKey = cacheManager.GetKey("GetCustomerByEmail", email); cacheManager.Add(cacheName, cacheKey, newObject); cacheManager.TryGet(cacheName, cacheKey, out cached); Assert.AreSame(newObject, cached, "Now it should be cached."); }