示例#1
0
        /// <summary>
        ///     使用 <see cref="RedisCacheValue" /> 包装 缓存值,并返回序列化后的结果。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        /// <returns></returns>
        protected byte[] ParseValue(string key, object value, DateTime?absoluteExpiration,
                                    TimeSpan?slidingExpiration)
        {
            var cacheValue = new RedisCacheValue
            {
                Key = key,
                AbsoluteExpiration = absoluteExpiration,
                SlidingExpiration  = slidingExpiration,
                Value = value
            };

            return(Serialize(cacheValue));
        }
示例#2
0
        protected bool CheckCacheValue(RedisCacheValue value)
        {
            if (value.SlidingExpiration.HasValue)
            {
                // 通常情况下在 滑动时间到期时 RedisCache 会自动移除缓存项。
                if (value.SlidingExpiration.Value < DateTime.Now - value.InsertTime)
                {
                    Remove(value.Key);
                    return(false);
                }

                Set(value.Key, value.Value, value.SlidingExpiration.Value);
            }

            return(true);
        }
示例#3
0
        protected bool TryDeserialize(byte[] data, out RedisCacheValue @object)
        {
            @object = default(RedisCacheValue);
            if (data == null)
            {
                return(false);
            }
            try
            {
                var result = _serializableService.Deserialize <RedisCacheValue>(data);
                @object = result;
                if (!CheckCacheValue(@object))
                {
                    @object = null;
                    return(false);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }