Пример #1
0
        private async Task <bool> SetValueAsync <T>(string key, T value, CacheOptions cacheOption)
        {
            bool result = false;

            try
            {
                var valueStr        = CheckKeyAndValue(key, value);
                var redisExpireTime = cacheOption.Expiry.AddMinutes(_redisExpireAddtionalTime);

                // 存储协议:第一位(1 代表滑动过期) 第二位(逻辑过期时间 时间戳)第三位 数据位 第四位 逻辑过期标志(1代表过期)

                await Cache.KeyDeleteAsync(key);

                var times = cacheOption.Expiry.Ticks;
                if (cacheOption.ExpireType == ExpireType.Silibing)
                {
                    times = times - DateTime.UtcNow.Ticks;
                }

                await Cache.ListRightPushAsync(key, new RedisValue[4] {
                    (int)cacheOption.ExpireType, times, valueStr, 0
                });

                result = await Cache.KeyExpireAsync(key, redisExpireTime);
            }
            catch (Exception ex)
            {
                _logger.Error($"Redis cache write operation error!key={key},value={_jsonSerializer.Serialize(value)}, expire type:{cacheOption.ExpireType}", ex);
            }
            return(result);
        }
Пример #2
0
        public async Task <bool> TrySetExpireAsync <T>(string key, T value, long silidingTime)
        {
            var cacheOption = new CacheOptions
            {
                Expiry     = DateTime.Now.AddMinutes(silidingTime),
                ExpireType = ExpireType.Silibing
            };

            return(await SetValueAsync(key, value, cacheOption));
        }
Пример #3
0
        public async Task <bool> TrySetExpireAsync <T>(string key, T value, TimeSpan?releativeTimeSpan = default(TimeSpan?))
        {
            var cacheOption = new CacheOptions
            {
                Expiry     = releativeTimeSpan.HasValue ? DateTime.Now.Add(releativeTimeSpan.Value) : DateTime.Now.AddMinutes(_defaultExpireTime),
                ExpireType = ExpireType.Relative
            };

            return(await SetValueAsync(key, value, cacheOption));
        }
Пример #4
0
        public async Task <bool> TrySetExpireAsync <T>(string key, T value, DateTime absoluteExpiredTime)
        {
            var cacheOption = new CacheOptions
            {
                Expiry     = absoluteExpiredTime,
                ExpireType = ExpireType.Relative
            };

            return(await SetValueAsync(key, value, cacheOption));
        }