///======================================================================== /// Method : Store /// /// <summary> /// Allows callers to explictly store data in the cache if they desire /// </summary> ///======================================================================== public void Store(TKey Key, TValue Value) { DateTime lTimeOut = mCacheExpiry == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.UtcNow + mCacheExpiry; var lItem = new RedisCacheItem(Key, Value, lTimeOut); StoreInCache(lItem); }
public void Deserialize_SerializedString_ReturnsRedisCacheItem() { var value = new RedisCacheItem <string> { Expires = new DateTime(2015, 01, 02), Key = "JsonSerializationProviderTests.Key", Value = "Test", Tags = new List <string> { "tag1", "tag2", "tag3" } }; var serializer = new FastJsonSerializationProvider(); var serialized = serializer.Serialize(value); var result = serializer.Deserialize <RedisCacheItem <string> >(serialized); Assert.IsNotNull(result); Assert.AreEqual(value.Expires, result.Expires); Assert.AreEqual(value.Key, result.Key); Assert.AreEqual(value.Value, result.Value); Assert.IsTrue((value.Tags.Count() == result.Tags.Count()) && !value.Tags.Except(result.Tags).Any()); }
protected override void StoreInCache(RedisCacheItem cacheItem) { var lDatabase = base.RedisConnection.GetDatabase(); //FetchPrimaryKeyHashSetDetails var PrimaryhashSetDetails = hashSets.Where(h => h.isPrimaryHashSet == true).FirstOrDefault(); PrimaryhashSetDetails.Value = cacheItem.Keys.ToList().Where(p => p.Key.ToString().ToLower().Equals(PrimaryhashSetDetails.key.ToLower())).FirstOrDefault().Value.ToString(); foreach (var key in cacheItem.Keys) { var hashset = hashSets.Where(h => h.key.ToLower().Equals(key.Key.ToLower())).FirstOrDefault(); if (hashSets != null) { if (hashset.isPrimaryHashSet) { //Store Actual Object Against only in primary HashSet lDatabase.HashSet(hashset.HashSetName, new HashEntry[] { new HashEntry(key.Value.ToString(), ConvertToRedisValue(cacheItem)) }); } else { //Check if there is already lookup stored if yes no need to update lookup if (!lDatabase.HashExists(hashset.HashSetName, PrimaryhashSetDetails.Value)) { //Store Look up value of primary HashSet in other hashSets lDatabase.HashSet(hashset.HashSetName, new HashEntry[] { new HashEntry(key.Value.ToString(), ConvertToRedisValue(PrimaryhashSetDetails.Value)) }); } } } } }
protected override void StoreInCache(RedisCacheItem cacheItem) { var lDatabase = base.RedisConnection.GetDatabase(); var hashEntries = new HashEntry[1]; if (!lDatabase.HashExists(this.cacheName, Convert.ToString(cacheItem.Key))) { hashEntries[0] = new HashEntry(Convert.ToString(cacheItem.Key), ConvertToRedisValue(cacheItem)); lDatabase.HashSet(this.cacheName, hashEntries); } }
protected RedisValue ConvertToRedisValue(RedisCacheItem xiResults) { using (var lMemoryStream = new MemoryStream()) { using (var lCompressionStream = new DeflateStream(lMemoryStream, CompressionLevel.Fastest)) { SerializationUtils.SerializeToStream(lCompressionStream, xiResults); } return(lMemoryStream.ToArray()); } }
public void Serialize_RedisCacheItem_ReturnsString() { var value = new RedisCacheItem<string> { Expires = new DateTime(2014, 01, 02), Key = "JsonSerializationProviderTests.Key", Value = "Test", Tags = new List<string>{ "tag1", "tag2", "tag3" } }; var serializer = new FastJsonSerializationProvider(); var result = serializer.Serialize(value); Assert.IsNotNull(result); }
public void Serialize_RedisCacheItem_ReturnsString() { var value = new RedisCacheItem <string> { Expires = new DateTime(2014, 01, 02), Key = "JsonSerializationProviderTests.Key", Value = "Test", Tags = new List <string> { "tag1", "tag2", "tag3" } }; var serializer = new FastJsonSerializationProvider(); var result = serializer.Serialize(value); Assert.IsNotNull(result); }
///======================================================================== /// Method : Retrieve /// /// <summary> /// Retrieves an item from the cache, if it's not cached or has expired /// then the delegate is used to read the value. Optionally allows /// caller to force the item to be refreshed /// </summary> ///======================================================================== public virtual TValue Retrieve(TKey Key, bool ForceRefresh) { //======================================================================== // If we do have a cached item then note as much here - we may later need // to remove it from the cache if it is expired and we can't retrieve an // up to date version //======================================================================== RedisCacheItem lItem = RetrieveFromCache(Key); bool lMayRemoveItem = (lItem != null); if (lItem == null || lItem.TimeOut < DateTime.UtcNow || ForceRefresh) { lItem = null; if (mRetriever != null) { if (lMayRemoveItem) { RemoveFromCache(Key); } DateTime lStart = DateTime.UtcNow; TimeSpan lTimeOut = GetTimeout() - lStart; TValue lValue = mRetriever != null?mRetriever(Key) : default(TValue); TimeSpan lRetrievalTime = DateTime.UtcNow - lStart; lItem = new RedisCacheItem(Key, lValue, lStart.Add(lTimeOut)) { FetchTimeInMillisecs = (int)lRetrievalTime.TotalMilliseconds }; StoreInCache(lItem); } else if (lMayRemoveItem) { //======================================================================== // The item is expired. Should remove it now since it's no use any more //======================================================================== RemoveFromCache(Key); } } return((lItem == null) ? default(TValue) : lItem.Value); }
protected override void StoreInCache(RedisCacheItem cacheItem) { var lDatabase = base.RedisConnection.GetDatabase(); var hashEntries = new HashEntry[associatedKeys.Count]; string primaryKeyFormat = string.Empty; int i = 0; foreach (var key in cacheItem.Keys) { if (associatedKeys[0].ToLower().Equals(key.Key.ToLower())) { primaryKeyFormat = GestringFormat(key.Key, key.Value.ToString()); hashEntries[i++] = new HashEntry(primaryKeyFormat, ConvertToRedisValue(cacheItem)); } else { hashEntries[i++] = new HashEntry(GestringFormat(key.Key, key.Value.ToString()), ConvertToRedisValue(primaryKeyFormat)); } } //hashEntries[0] = new HashEntry(Convert.ToString(cacheItem.Key), ConvertToRedisValue(cacheItem)); lDatabase.HashSet(this.cacheName, hashEntries); }
public void Deserialize_SerializedString_ReturnsRedisCacheItem() { var value = new RedisCacheItem<string> { Expires = new DateTime(2015, 01, 02), Key = "JsonSerializationProviderTests.Key", Value = "Test", Tags = new List<string> { "tag1", "tag2", "tag3" } }; var serializer = new FastJsonSerializationProvider(); var serialized = serializer.Serialize(value); var result = serializer.Deserialize<RedisCacheItem<string>>(serialized); Assert.IsNotNull(result); Assert.AreEqual(value.Expires, result.Expires); Assert.AreEqual(value.Key, result.Key); Assert.AreEqual(value.Value, result.Value); Assert.IsTrue((value.Tags.Count() == result.Tags.Count()) && !value.Tags.Except(result.Tags).Any()); }
protected abstract void StoreInCache(RedisCacheItem Item);