public override bool DeleteValue(RedisId key) { // get all the keys to be deleted IEnumerable <RedisId> keys = GetKey <object>(key); Dictionary <string, List <string> > hashsetDictionary = new Dictionary <string, List <string> >(); // sort the keys by hashset (there should only be 1, but this is more safe) foreach (RedisId currentKey in keys) { if (!hashsetDictionary.Keys.Contains(currentKey.HashSetIdentifier)) { hashsetDictionary.Add(currentKey.HashSetIdentifier, new List <string>()); } hashsetDictionary[currentKey.HashSetIdentifier].Add(currentKey.ObjectIdentifier); } long deleteCount = 0; // delete keys by hashset (still there should only be one) foreach (string hashsetDictionaryKey in hashsetDictionary.Keys) { deleteCount += RedisService.Database.HashDelete(hashsetDictionaryKey, hashsetDictionary[hashsetDictionaryKey].Select(x => (RedisValue)x).ToArray()); } return(deleteCount > 0); }
public override bool ContainsKey(RedisId key) { HashEntry value = RedisService.Database.HashScan(key.HashSetIdentifier, key.ObjectIdentifier , RedisService.CacheSettings.ServiceSettings.DefaultScanPageSize).FirstOrDefault(); return(!string.IsNullOrWhiteSpace(value.Name)); }
protected void HardAddToCache(RedisId key, RedisValue value, DateTime expiration) { RedisValue realValue = RedisService.CacheSettings.ServiceSettings.CompressValues ? (RedisValue)RedisService.CacheProvider.CompressValue(value) : value; ServiceAdd(key, realValue, expiration); }
public override bool ContainsKey(RedisId key) { if (key == null) { return(false); } return(RedisService.Database.KeyExists(key.FullKey)); }
protected RedisValue HardGetFromCache(RedisId key) { RedisValue rawValue = ServiceGet(key); RedisValue value = (rawValue != default(RedisValue)) && RedisService.CacheSettings.ServiceSettings.CompressValues ? RedisService.CacheProvider.DecompressValue(rawValue) : rawValue; return(value); }
public override bool DeleteValue(RedisId key) { if (string.IsNullOrWhiteSpace(key?.FullKey)) { return(false); } IEnumerable <RedisKey> keys = GetKey <object>(key).Select(x => (RedisKey)x.FullKey); return(RedisService.Database.KeyDelete(keys.ToArray()) > 0); }
/// <summary> /// Wraps an object for caching /// </summary> /// <typeparam name="T">The configuration type</typeparam> /// <param name="key">The key that identifies the object</param> /// <param name="value">The object to wrap</param> /// <param name="cachedTime">When the object was cached, default UTC now</param> /// <param name="expireTime">When the object should expire, default UTC now + DefaultCacheLifespan</param> /// <returns>The wrapped object</returns> protected virtual RedisCachedObject <T> CreateCachedObject <T>(RedisId key, T value, DateTime?cachedTime = null, DateTime?expireTime = null) { return(new RedisCachedObject <T>(key) { Value = value, CachedTime = cachedTime ?? DateTime.UtcNow, ExpireTime = expireTime ?? DateTime.UtcNow.Add(DefaultCacheLifespan), Metadata = _cacheInfo }); }
public virtual RedisCachedObject <T> CreateCachedValue <T>(RedisId redisKey = null) { return(new RedisCachedObject <T>(redisKey?.ObjectIdentifier) { RetrievedSuccesfully = false, Value = default(T), CachedTime = DateTime.MinValue, Status = CacheEntryStatus.Error, Metadata = CacheSettings.ServiceSettings.ProviderInfo, Id = redisKey, ExpireTime = DateTime.MinValue, }); }
// not too slow, but could be problematic public override Dictionary <RedisId, TimeSpan?> GetTimeToLive <T>(IEnumerable <RedisId> allKeys) { IEnumerable <RedisId> keys = allKeys.Where(x => !string.IsNullOrWhiteSpace(x?.FullKey)); // Lua uses 1-based arrays // Redis has the global "Table" (basically an array with generic indexing) KEYS for key arguments // https://www.lua.org/pil/2.5.html string luaScript = @" local keys = KEYS local retVal = '' local splitString = '|||||' local splitKvp = '^^^^^' local result = {} for i=1,#keys,1 do local ttlthing = redis.call('ttl', keys[i]) retVal = (retVal) .. (keys[i]) .. (splitKvp) .. ttlthing .. (splitString) end return retVal" ; // retVal = (retVal) .. (splitString) // .. (keys[i]) RedisKey[] redisValues = keys.Select(x => (RedisKey)x.FullKey).ToArray(); RedisResult result = RedisService.Database.ScriptEvaluate(luaScript, redisValues); string[] splitResult = result.ToString() .Split(new[] { "|||||" }, StringSplitOptions.RemoveEmptyEntries); Dictionary <RedisId, TimeSpan?> ttls = new Dictionary <RedisId, TimeSpan?>(); foreach (string s in splitResult) { if (string.IsNullOrWhiteSpace(s)) { continue; } string[] splitResult2 = s .Split(new[] { "^^^^^" }, StringSplitOptions.RemoveEmptyEntries); if (splitResult2.Length != 2) { continue; } RedisId key = keys.FirstOrDefault(k => k.FullKey.Contains(s)); int ttlSeconds; bool gotTtlSeconds = int.TryParse(splitResult2[1], out ttlSeconds); if (!gotTtlSeconds) { ttls.Add(key, null); } ttls.Add(key, TimeSpan.FromSeconds(ttlSeconds)); } return(ttls); }
public override IEnumerable <RedisId> GetKey <T>(RedisId key) { if (!(key?.HasFullKey ?? false)) { return(new List <RedisId>()); } IEnumerable <HashEntry> fieldIdentifiers = RedisService.Database.HashScan(key.HashSetIdentifier, key.ObjectIdentifier, RedisService.CacheSettings.ServiceSettings.DefaultScanPageSize); IEnumerable <RedisId> resultingKeys = // get the name, and add it to the hash name fieldIdentifiers.Select(fi => new RedisId { HashSetIdentifier = key.HashSetIdentifier, ObjectIdentifier = fi.Name.ToString(), ServiceType = this.ServiceType }); return(resultingKeys); }
/// <summary> /// Converts a string found in cache to an object /// </summary> /// <typeparam name="T">Object type to convert to</typeparam> /// <param name="stringToConvert">String to convert to an object</param> /// <param name="key">The key that was used to get the value</param> /// <param name="serializationSettings">The datacontract resolver to use for serialization /// (polymorphic dtos)</param> /// <returns>Object from string</returns> protected internal virtual T ConvertString <T>(string stringToConvert, RedisId key, ISerializationSettings serializationSettings = null) { T retVal = default(T); if (string.IsNullOrWhiteSpace(stringToConvert)) { return(retVal); } if (typeof(T) == typeof(string)) { retVal = (T)Convert.ChangeType(stringToConvert, typeof(T)); } else { ISerializationService serializationService = GetSerializationService <T>(); retVal = serializationService.DeserializeObject <T>(stringToConvert, _cacheSettings.SerializationSettings); } return(retVal); }
protected override void ServiceAdd(RedisId key, RedisValue value, DateTime expiration) { RedisService.Database.HashSet(key.HashSetIdentifier, key.ObjectIdentifier, value); }
public virtual IEnumerable <RedisCachedObject <T> > GetValue <T>(RedisId key) { return(GetValues <T>(new[] { key })); }
public virtual Dictionary <RedisId, TimeSpan?> GetTimeToLive <T>(RedisId key) { return(GetTimeToLive <T>(new[] { key })); }
public abstract IEnumerable <RedisId> GetKey <T>(RedisId key);
public abstract bool DeleteValue(RedisId key);
public bool ContainsKey(RedisId key) { return(GetRedisServiceImplementation().ContainsKey(key)); }
public override IEnumerable <RedisId> GetKey <T>(RedisId key) { return(GetKeys <T>(new[] { key })); }
protected abstract void ServiceAdd(RedisId key, RedisValue value, DateTime expiration);
public virtual IEnumerable <RedisId> GetKey <T>(RedisId key) { return(GetRedisServiceImplementation().GetKey <T>(key)); }
public virtual IEnumerable <RedisCachedObject <T> > GetValue <T>(RedisId key) { return(GetRedisServiceImplementation().GetValue <T>(key)); }
protected override RedisValue ServiceGet(RedisId key) { return(RedisService.Database.StringGet(key.FullKey)); }
public virtual RedisId GetRedisId(RedisId key) { return(key); }
public virtual bool DeleteValue(RedisId key) { return(GetRedisServiceImplementation().DeleteValue(key)); }
protected override RedisValue ServiceGet(RedisId key) { return(RedisService.Database.HashGet(key.HashSetIdentifier, key.ObjectIdentifier)); }
protected abstract RedisValue ServiceGet(RedisId key);
public abstract bool ContainsKey(RedisId key);
protected override void ServiceAdd(RedisId key, RedisValue value, DateTime expiration) { RedisService.Database.StringSet(key.FullKey, value, expiration - DateTime.UtcNow); }
public virtual Dictionary <RedisId, TimeSpan?> GetTimeToLive <T>(RedisId key) { return(GetRedisServiceImplementation().GetTimeToLive <T>(key)); }