/// <summary> /// Add the object into MemoryCache /// </summary> /// <param name="persistenceCacheStackEntity"></param> /// <returns></returns> protected virtual bool AddItem(PersistenceCacheStackEntity persistenceCacheStackEntity) { try { var cacheItem = new CacheItem(persistenceCacheStackEntity.Key, persistenceCacheStackEntity); lock (padlock) { if (persistenceCacheStackEntity.Expiration == null) { cache.Set(cacheItem, new CacheItemPolicy()); } else { // if it is required a cache expiration cache.Set(cacheItem, new CacheItemPolicy() { AbsoluteExpiration = persistenceCacheStackEntity.Expiration.Value }); } } return(true); } catch (Exception) { return(false); } }
public void TestAddItem() { var persistenceCacheStackEntity = new PersistenceCacheStackEntity("test", new object(), null); var addResult = new RedisWrapper().Push(persistenceCacheStackEntity); Assert.IsTrue(addResult); }
public void TestSynchFromRedis() { var testClass = new UnitTestClass() { TestDescription = "TestSynchFromRedis", TestId = 4 }; var persistenceCacheStackEntity = new PersistenceCacheStackEntity("TestSynchFromRedis", testClass, null); var redisWrapper = new RedisWrapper(); // add fake object into Redis var addResult = redisWrapper.Push(persistenceCacheStackEntity); Assert.IsTrue(addResult); var objectCached = redisWrapper.Get(persistenceCacheStackEntity.Key); Assert.IsNotNull(objectCached); // synch from Redis var persistenceCacheStack = new PersistenceCacheStackClient <UnitTestClass>(true); var obj = persistenceCacheStack.GetItem("TestSynchFromRedis"); Assert.IsNotNull(obj); Assert.IsTrue(obj.TestId == 4); }
/// <summary> /// Add item into MemoryCache and Redis for persistence /// </summary> /// <param name="PersistenceCacheStackEntity"></param> /// <returns></returns> public bool AddItem(PersistenceCacheStackEntity persistenceCacheStackEntity) { var resultInMemory = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); if (true == resultInMemory) { this.taskFactory.StartNew(() => this.redisWrapper.Push(persistenceCacheStackEntity)); return(true); } return(false); }
public void TestGetItem() { var persistenceCacheStackEntity = new PersistenceCacheStackEntity("test", new object(), null); var redisWrapper = new RedisWrapper(); var addResult = redisWrapper.Push(persistenceCacheStackEntity); Assert.IsTrue(addResult); var objectCached = redisWrapper.Get(persistenceCacheStackEntity.Key); Assert.IsNotNull(objectCached); }
public void TestAddItem() { var testClass = new UnitTestClass() { TestDescription = "test1", TestId = 1 }; var persistenceCacheStackEntity = new PersistenceCacheStackEntity("test1", testClass, null); var addResult = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); Assert.IsTrue(addResult); }
/// <summary> /// Update the PersistenceCacheStackEntity object into MemoryCache and Redis /// </summary> /// <param name="PersistenceCacheStackEntity"></param> /// <returns></returns> public bool UpdateItem(PersistenceCacheStackEntity persistenceCacheStackEntity) { var objCached = GlobalCachingProvider.Instance.GetItem(persistenceCacheStackEntity.Key, true); if (objCached != null) { var resultInMemory = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); if (true == resultInMemory) { this.taskFactory.StartNew(() => this.redisWrapper.Update(persistenceCacheStackEntity)); } return(resultInMemory); } return(false); }
public void TestGetItem() { var testClass = new UnitTestClass() { TestDescription = "test2", TestId = 2 }; var persistenceCacheStackEntity = new PersistenceCacheStackEntity("test2", testClass, null); var addResult = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); Assert.IsTrue(addResult); var objectCached = GlobalCachingProvider.Instance.GetItem(persistenceCacheStackEntity.Key, false); Assert.IsNotNull(objectCached); Assert.AreEqual(objectCached.Key, "test2"); }
/// <summary> /// Check the current state of the object into Redis and eventually sync the memory cache /// </summary> private void CheckExternalsUpdates(string key, PersistenceCacheStackEntity persistenceCacheStackEntity) { var objRedis = redisWrapper.Get(key); if (objRedis != null) { if (persistenceCacheStackEntity == null) { // another node has added the object with key into redis, we must therefore add it into the MemoryCache GlobalCachingProvider.Instance.AddItem(objRedis); } else if (!objRedis.Equals(persistenceCacheStackEntity)) { // another node has updated the object, we must therefore update it into the MemoryCache GlobalCachingProvider.Instance.GetItem(key, true); GlobalCachingProvider.Instance.AddItem(objRedis); } } else { // another node has deleted the key from Redis, we must therefore remove it from the MemoryCache GlobalCachingProvider.Instance.GetItem(key, true); } }
public void TestUpdateItem() { var testClass = new UnitTestClass() { TestDescription = "test4", TestId = 4 }; var persistenceCacheStackEntity = new PersistenceCacheStackEntity("test4", testClass, null); var addResult = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); Assert.IsTrue(addResult); var objectCached = GlobalCachingProvider.Instance.GetItem(persistenceCacheStackEntity.Key, false); Assert.AreEqual(objectCached.Key, "test4"); testClass.TestDescription = "update_test4"; persistenceCacheStackEntity = new PersistenceCacheStackEntity("test4", testClass, null); var updateResult = GlobalCachingProvider.Instance.AddItem(persistenceCacheStackEntity); Assert.IsTrue(updateResult); var updatedObject = GlobalCachingProvider.Instance.GetItem(persistenceCacheStackEntity.Key, false); Assert.IsNotNull(updatedObject); Assert.IsNotNull(updatedObject.Object); var updateTestClass = (UnitTestClass)updatedObject.Object; Assert.IsNotNull(updateTestClass); Assert.IsTrue(updateTestClass.TestDescription == "update_test4"); }
/// <summary> /// Update the PersistenceCacheStackEntity object /// </summary> /// <param name="pCacheStackEntity"></param> /// <returns></returns> public bool Update(PersistenceCacheStackEntity pCacheStackEntity) { return(pCacheStackEntity != null && (pCacheStackEntity?.Expiration != null ? RedisAccess.Instance.RedisCacheClient.Replace(pCacheStackEntity.Key, pCacheStackEntity, pCacheStackEntity.Expiration.Value) : RedisAccess.Instance.RedisCacheClient.Replace(pCacheStackEntity.Key, pCacheStackEntity))); }
/// <summary> /// Add the object into MemoryCache /// </summary> /// <param name="persistenceCacheStackEntity"></param> /// <returns></returns> public new bool AddItem(PersistenceCacheStackEntity persistenceCacheStackEntity) { return(base.AddItem(persistenceCacheStackEntity)); }