public object Get(string key)
        {
            byte[] result;
            result = _distributedCache.Get(key);
            var deserializeResult = CacheSerializer.Deserialize <object>(result);

            return(deserializeResult);
        }
예제 #2
0
        public void ShouldDeserializeJsonToCache()
        {
            string json                     = CreateJsonString();
            var    cacheSerializer          = new CacheSerializer();
            Dictionary <string, int> result = cacheSerializer.Deserialize(json);
            int id = result[SerieName];

            Assert.That(id, Is.EqualTo(SerieId));
        }
예제 #3
0
        public async Task <IReadOnlyList <IWallet> > GetAllAsync(string walletId)
        {
            // todo: refactor most of code below into RedisCacheExtensions method 'TryHashGetAllAsync'
            try
            {
                var balances = await _redisDatabase.HashGetAllAsync(GetCacheKey(walletId));

                if (balances != null && balances.Length > 0)
                {
                    return(balances.Select(x => CacheSerializer.Deserialize <CachedWalletModel>(x.Value)).ToList());
                }
            }
            catch (RedisConnectionException ex)
            {
                _log.Warning("Redis cache is not available", ex);
            }

            return(await ReloadAllAsync(walletId));
        }
        public T Get(string cacheKeyName, int cacheTimeOutSeconds, Func <T> func)
        {
            //var redisManager = new PooledRedisClientManager("localhost:6379");
            var redisCacheServer =
                ConfigurationManager.AppSettings["RedisCacheServer"];
            var redisCacheServerPort =
                ConfigurationManager.AppSettings["RedisCacheServerPort"];

            if (string.IsNullOrEmpty(redisCacheServer))
            {
                throw new ApplicationException("RedisCacheServer must be defined in appsettings");
            }
            if (string.IsNullOrEmpty(redisCacheServerPort))
            {
                throw new ApplicationException("RedisCacheServerPort must be defined in appsettings");
            }

            var    redisKey = ConfigurationManager.AppSettings["RedisCacheServerKey"];
            string connectionString;

            if (!string.IsNullOrEmpty(redisKey))
            {
                connectionString =
                    $"{redisCacheServer}:{redisCacheServerPort},abortConnect=false,ssl=false,password={redisKey}";
            }
            else
            {
                connectionString =
                    $"{redisCacheServer}:{redisCacheServerPort}";
            }

            using (var connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString))
            {
                lock (Locker)
                {
                    redis = connectionMultiplexer.GetDatabase();
                }

                var o = CacheSerializer.Deserialize <T>(redis.StringGet(cacheKeyName));
                if (o != null)
                {
                    return(o);
                }
                lock (Locker)
                {
                    // get lock but release if it takes more than 60 seconds to complete to avoid deadlock if this app crashes before release
                    //using (redis.AcquireLock(cacheKeyName + "-lock", TimeSpan.FromSeconds(60)))

                    var lockKey = cacheKeyName + "-lock";
                    if (redis.LockTake(lockKey, Environment.MachineName, TimeSpan.FromSeconds(10)))
                    {
                        try
                        {
                            o = CacheSerializer.Deserialize <T>(redis.StringGet(cacheKeyName));
                            if (o == null)
                            {
                                o = func();
                                redis.StringSet(cacheKeyName, CacheSerializer.Serialize(o),
                                                TimeSpan.FromSeconds(cacheTimeOutSeconds));
                            }
                            redis.LockRelease(lockKey, Environment.MachineName);
                            return(o);
                        }
                        finally
                        {
                            redis.LockRelease(lockKey, Environment.MachineName);
                        }
                    }
                    return(o);
                }
            }
        }