public virtual IEnumerable <RedisCachedObject <T> > SetValue <T>(RedisCachedObject <T> value) { if ((!(value?.Id?.HasFullKey ?? false)) || value.Value == null || value.Value.Equals(default(T))) { return(null); } string serializedWrapper; string serializedObject; ISerializationService sz = RedisService.GetSerializationService <T>(); serializedObject = sz.SerializeObject <T>(value.Value, RedisService.CacheSettings.SerializationSettings); RedisCachedObject <string> wrapper = new RedisCachedObject <string> { RetrievedSuccesfully = value.RetrievedSuccesfully, Value = serializedObject, CachedTime = value.CachedTime, Status = value.Status, Metadata = value.Metadata, Id = value.Id, ExpireTime = value.ExpireTime, }; sz = RedisService.GetSerializationService <RedisCachedObject <string> >(); serializedWrapper = sz.SerializeObject <RedisCachedObject <string> >(wrapper, RedisService.CacheSettings.SerializationSettings); HardAddToCache(value.Id, serializedWrapper, value.ExpireTime); return(new[] { value }); }
// Abstract implementation public virtual RedisValue GetValueToCache <T>(RedisCachedObject <T> cachedObject) { RedisValue redisValue = CacheSettings.SerializationService.SerializeObject(cachedObject , CacheSettings.SerializationSettings); if (CacheSettings.ServiceSettings.CompressValues) { redisValue = CompressValue(redisValue); } return(redisValue); }
public void Properties() { RedisCachedObject <string> cachedObject = new RedisCachedObject <string>(); cachedObject.Id.ObjectIdentifier = "test"; const string value = "a value"; DateTime time = DateTime.UtcNow; DateTime expireTime = DateTime.UtcNow.AddMinutes(5); cachedObject.ExpireTime = expireTime; cachedObject.CachedTime = time; cachedObject.Value = value; Assert.Equal(expireTime, cachedObject.ExpireTime); Assert.Equal(time, cachedObject.CachedTime); Assert.Equal(value, cachedObject.Value); }
public virtual RedisCachedObject <T> ChangeType <T, TK>(RedisCachedObject <TK> source) { if (source == null) { return(CreateCachedValue <T>()); } RedisCachedObject <T> wrapper = new RedisCachedObject <T> { RetrievedSuccesfully = source.RetrievedSuccesfully, Value = (T)source.UntypedValue, CachedTime = source.CachedTime, Status = source.Status, Metadata = source.Metadata, Id = source.Id, ExpireTime = source.ExpireTime, }; return(wrapper); }
public IEnumerable <RedisCachedObject <T> > GetValues <T>(IEnumerable <RedisId> keys) { if (!(keys?.Any() ?? false)) { return(new RedisCachedObject <T> [0]); } RedisId[] keysToUse = keys.ToArray(); for (int i = 0; i < keysToUse.Count(); i++) { if (keysToUse[i].ObjectIdentifier.EndsWith("*")) { continue; } keysToUse[i].ObjectIdentifier = keysToUse[i].ObjectIdentifier + "*"; } IEnumerable <RedisId> redisKeys = GetKeys <T>(keys); if (!(redisKeys?.Any() ?? false)) { return(new RedisCachedObject <T> [0]); } RedisValue[] results = ServiceGetValues <T>(redisKeys).ToArray(); List <RedisCachedObject <T> > values = new List <RedisCachedObject <T> >(results.Length); foreach (RedisValue result in results) { RedisCachedObject <T> current; RedisCachedObject <string> stringValue = RedisService.CacheProvider.GetCachedValue <string>(result, this); if (typeof(T) == typeof(object) || typeof(T) == typeof(string)) { current = RedisService.CacheProvider.ChangeType <T, string>(stringValue); } else if (typeof(T).IsPrimitive) { current = RedisService.CacheProvider.ChangeType <T, string>(stringValue); } else { ISerializationService sz = RedisService.GetSerializationService <T>(); current = new RedisCachedObject <T> { RetrievedSuccesfully = stringValue.RetrievedSuccesfully, Value = sz.DeserializeObject <T>(stringValue.Value, RedisService.CacheSettings.SerializationSettings), CachedTime = stringValue.CachedTime, Status = stringValue.Status, Metadata = stringValue.Metadata, Id = stringValue.Id, ExpireTime = stringValue.ExpireTime, }; } if (current != null) { values.Add(current); } } return(ServiceValuePostProcess(values)); }
protected virtual RedisCachedObject <T> GetCachedValue <T>(RedisValue redisValue, IRedisService service, bool first) { string decompressed = null; if (default(RedisValue) == redisValue) { return(CreateCachedValue <T>()); } if (CacheSettings.ServiceSettings.CompressValues) { decompressed = DecompressValue(redisValue); } if (default(RedisValue) == redisValue) { return(CreateCachedValue <T>()); } decompressed = decompressed ?? redisValue; RedisCachedObject <T> value; if (!first) { ISerializationService sz = CacheSettings.SerializationService; try { return(sz.DeserializeObject <RedisCachedObject <T> >(decompressed, CacheSettings.SerializationSettings)); } catch (SerializationException) { return(CreateCachedValue <T>()); } } RedisCachedObject <string> stringValue = GetCachedValue <string>(redisValue, service, false); if (typeof(T) == typeof(object) || typeof(T) == typeof(string)) { value = ChangeType <T, string>(stringValue); } else if (typeof(T).IsPrimitive) { value = ChangeType <T, string>(stringValue); } else { ISerializationService sz = CacheSettings.SerializationService; value = new RedisCachedObject <T> { RetrievedSuccesfully = stringValue.RetrievedSuccesfully, Value = sz.DeserializeObject <T>(stringValue.Value, CacheSettings.SerializationSettings), CachedTime = stringValue.CachedTime, Status = stringValue.Status, Metadata = stringValue.Metadata, Id = stringValue.Id, ExpireTime = stringValue.ExpireTime, }; } if (value != null && value.ExpireTime < DateTime.UtcNow) { if (value.Id != null) { service.DeleteValue(value.Id); } return(CreateCachedValue <T>()); } return(value); }
public virtual IEnumerable <RedisCachedObject <T> > SetValue <T>(RedisCachedObject <T> value) { return(GetRedisServiceImplementation().SetValue <T>(value)); }