예제 #1
0
        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);
        }
예제 #2
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));
        }
예제 #3
0
        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);
        }
예제 #4
0
 public override bool ContainsKey(RedisId key)
 {
     if (key == null)
     {
         return(false);
     }
     return(RedisService.Database.KeyExists(key.FullKey));
 }
예제 #5
0
        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);
        }
예제 #6
0
        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,
     });
 }
예제 #9
0
        // 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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        /// <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);
        }
예제 #12
0
 protected override void ServiceAdd(RedisId key, RedisValue value, DateTime expiration)
 {
     RedisService.Database.HashSet(key.HashSetIdentifier, key.ObjectIdentifier, value);
 }
예제 #13
0
 public virtual IEnumerable <RedisCachedObject <T> > GetValue <T>(RedisId key)
 {
     return(GetValues <T>(new[] { key }));
 }
예제 #14
0
 public virtual Dictionary <RedisId, TimeSpan?> GetTimeToLive <T>(RedisId key)
 {
     return(GetTimeToLive <T>(new[] { key }));
 }
예제 #15
0
 public abstract IEnumerable <RedisId> GetKey <T>(RedisId key);
예제 #16
0
 public abstract bool DeleteValue(RedisId key);
예제 #17
0
 public bool ContainsKey(RedisId key)
 {
     return(GetRedisServiceImplementation().ContainsKey(key));
 }
예제 #18
0
 public override IEnumerable <RedisId> GetKey <T>(RedisId key)
 {
     return(GetKeys <T>(new[] { key }));
 }
예제 #19
0
 protected abstract void ServiceAdd(RedisId key, RedisValue value, DateTime expiration);
예제 #20
0
 public virtual IEnumerable <RedisId> GetKey <T>(RedisId key)
 {
     return(GetRedisServiceImplementation().GetKey <T>(key));
 }
예제 #21
0
 public virtual IEnumerable <RedisCachedObject <T> > GetValue <T>(RedisId key)
 {
     return(GetRedisServiceImplementation().GetValue <T>(key));
 }
예제 #22
0
 protected override RedisValue ServiceGet(RedisId key)
 {
     return(RedisService.Database.StringGet(key.FullKey));
 }
 public virtual RedisId GetRedisId(RedisId key)
 {
     return(key);
 }
예제 #24
0
 public virtual bool DeleteValue(RedisId key)
 {
     return(GetRedisServiceImplementation().DeleteValue(key));
 }
예제 #25
0
 protected override RedisValue ServiceGet(RedisId key)
 {
     return(RedisService.Database.HashGet(key.HashSetIdentifier, key.ObjectIdentifier));
 }
예제 #26
0
 protected abstract RedisValue ServiceGet(RedisId key);
예제 #27
0
 public abstract bool ContainsKey(RedisId key);
예제 #28
0
 protected override void ServiceAdd(RedisId key, RedisValue value, DateTime expiration)
 {
     RedisService.Database.StringSet(key.FullKey, value, expiration - DateTime.UtcNow);
 }
예제 #29
0
 public virtual Dictionary <RedisId, TimeSpan?> GetTimeToLive <T>(RedisId key)
 {
     return(GetRedisServiceImplementation().GetTimeToLive <T>(key));
 }