Пример #1
0
        protected async Task <string> Get(Guid projectId, string key, TimeSpan renewTimeout)
        {
            object transientTemp;
            var    cacheKey = CacheExtenstions.ToCacheKey(projectId, key);

            if (_transientCache.Count > 0)
            {
                var value = _transientCache.TryGetValue(cacheKey, out transientTemp) ? (string)transientTemp : (string)null;
                return(value);
            }

            try
            {
#if RunSynchronous
                var redisValue = _retryStrategy.ExecuteWithRetry(() => Cache.StringGet(cacheKey));
                if (redisValue.HasValue)
                {
                    if (renewTimeout != NoTimeout)
                    {
                        Cache.KeyExpire(cacheKey, renewTimeout);
                    }
#if TrackStats
                    UpdateStats(cacheKey, StatType.Hit);
#endif //TrackStats
                }
#else
                var redisValue = _retryStrategy.ExecuteWithRetry(() => Cache.StringGetAsync(cacheKey)).Result;
                if (redisValue.HasValue)
                {
                    UpdateStats(cacheKey, StatType.Hit);
                    if (renewTimeout != NoTimeout)
                    {
                        var isSuccessful = _retryStrategy.ExecuteWithRetry(() => Cache.KeyExpireAsync(cacheKey, renewTimeout)).Result;
                    }
                }
#endif
                else
                {
#if TrackStats
                    UpdateStats(cacheKey, StatType.Miss);
#endif //TrackStats
                }
                return(redisValue);
            }
            catch (Exception ex)
            {
#if TrackStats
                UpdateStats(cacheKey, StatType.GetException, ex);
#endif //TrackStats

                if (ex.GetType() == typeof(StackExchange.Redis.RedisConnectionException) || ex.GetType() == typeof(System.NullReferenceException))
                {
                    return(_transientCache.TryGetValue(cacheKey, out transientTemp) ? (string)transientTemp : (string)null);
                }
                return((string)null);
            }
        }
Пример #2
0
        protected async Task <bool> Set(Guid projectId, string key, string value, TimeSpan timeout)
        {
            var cacheKey = CacheExtenstions.ToCacheKey(projectId, key);

            if (_transientCache.Count > 0)
            {
                _transientCache[cacheKey] = value;
                return(true);
            }
            try
            {
                bool isSuccesful = false;
#if RunSynchronous
                if (timeout == NoTimeout)
                {
                    isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.StringSet((RedisKey)cacheKey, value) && Cache.KeyPersist((RedisKey)cacheKey));
                }
                else
                {
                    isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.StringSet((RedisKey)cacheKey, value, timeout));
                }
#else
                if (timeout == NoTimeout)
                {
                    isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.StringSetAsync(cacheKey, value).Result&& Cache.KeyPersistAsync(cacheKey).Result);
                    else
                    {
                        isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.StringSetAsync(cacheKey, value, timeout).Result);
#endif
#if TrackStats
                UpdateStats(cacheKey, isSuccesful ? StatType.Set : StatType.SetFail);
#endif //TrackStats
                return(isSuccesful);
            }
            catch (Exception ex)
            {
#if TrackStats
                UpdateStats(cacheKey, StatType.SetException, ex);
#endif //TrackStats
                if (ex.GetType() == typeof(StackExchange.Redis.RedisConnectionException) || ex.GetType() == typeof(System.NullReferenceException))
                {
                    _transientCache[cacheKey] = value;
                    return(true);
                }
                return(false);
            }
        }
Пример #3
0
        protected void DeleteAllKeys(Guid projectId, string keyPrefix, Action <RedisKey> onDelete = null)
        {
            if (_transientCache.Count > 0)
            {
                _transientCache.Clear();
                return;
            }
            var endpoints = Connection.GetEndPoints(true);

            foreach (var endpoint in endpoints)
            {
                var server         = Connection.GetServer(endpoint);
                var cacheKeyPrefix = CacheExtenstions.ToCacheKey(projectId, keyPrefix);
                var cacheKeys      = server.Keys(0, cacheKeyPrefix + "*");
                foreach (var cacheKey in cacheKeys)
                {
                    _retryStrategy.ExecuteWithRetry(() => Cache.KeyDelete(cacheKey));
                    onDelete?.Invoke(cacheKey);
                }
            }
        }
Пример #4
0
        protected async void Delete(Guid projectId, string key)
        {
            var cacheKey = CacheExtenstions.ToCacheKey(projectId, key);

            if (_transientCache.Count > 0)
            {
                _transientCache.Remove(cacheKey);
                return;
            }
            try
            {
#if RunSynchronous
                _retryStrategy.ExecuteWithRetry(() => Cache.KeyDelete(cacheKey));
#else
                var isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.KeyDeleteAsync(cacheKey)).Result;
#endif
            }
            catch (Exception ex)
            {
            }
        }
Пример #5
0
        protected async Task <bool> KeyExists(Guid projectId, string key, TimeSpan renewTimeout)
        {
            object transientTemp;
            var    cacheKey = CacheExtenstions.ToCacheKey(projectId, key);
            bool   exists   = false;

            try
            {
#if RunSynchronous
                if (_transientCache.Count > 0)
                {
                    exists = _transientCache.TryGetValue(cacheKey, out transientTemp);
                }
                else
                {
                    exists = _retryStrategy.ExecuteWithRetry(() => Cache.KeyExists(cacheKey),
                                                             (ex, numberOfRetries, remainingRetries) =>
                                                             ex.GetType() == typeof(StackExchange.Redis.RedisConnectionException)
                            ? new RetryResponse <bool> {
                        Action = RetryAction.ReturnResult, Result = false
                    }
                            : new RetryResponse <bool> {
                        Action = RetryAction.ContinueRetrying
                    });

                    if (exists)
                    {
                        if (renewTimeout != NoTimeout)
                        {
                            Cache.KeyExpire(cacheKey, renewTimeout);
                        }
                    }
                }
#else
                exists = _retryStrategy.ExecuteWithRetry(() => Cache.KeyExistsAsync(cacheKey)).Result;
                if (exists)
                {
                    if (renewTimeout != NoTimeout)
                    {
                        var isSuccesful = _retryStrategy.ExecuteWithRetry(() => Cache.KeyExpireAsync(cacheKey, renewTimeout)).Result;
                    }
                }
#endif
#if TrackStats
                UpdateStats(cacheKey, exists ? StatType.ExistHit : StatType.ExistMiss);
#endif //TrackStats
                return(exists);
            }
            catch (Exception ex)
            {
#if TrackStats
                UpdateStats(cacheKey, StatType.ExistException, ex);
#endif //TrackStats

                if (ex.GetType() == typeof(StackExchange.Redis.RedisConnectionException))
                {
                    return(_transientCache.TryGetValue(cacheKey, out transientTemp));
                }
                return(false);
            }
        }