Exemplo n.º 1
0
        public IBaseContainerBag Get(string key, bool isFullKey = false)
        {
            var value = base.Get(key, isFullKey);

            if (value == null)
            {
                return(null);
            }

            return(StackExchangeRedisExtensions.Deserialize <IBaseContainerBag>((RedisValue)value));
        }
Exemplo n.º 2
0
        public void Update(string key, object value, bool isFullKey = false)
        {
            //var cacheKey = GetFinalKey(key, isFullKey);
            var hashKeyAndField = this.GetHashKeyAndField(key);

            //value.Key = cacheKey;//储存最终的键

            //_cache.StringSet(cacheKey, value.Serialize());

            //_cache.HashSet(hashKeyAndField.Key, hashKeyAndField.Field, value.Serialize());
            _cache.HashSet(hashKeyAndField.Key, hashKeyAndField.Field, StackExchangeRedisExtensions.Serialize(value));
        }
Exemplo n.º 3
0
        public IContainerItemCollection Get(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }

            if (!CheckExisted(key))
            {
                return(null);
                //InsertToCache(key, new ContainerItemCollection());
            }

            var cacheKey = GetFinalKey(key);

            var value = _cache.StringGet(cacheKey);

            return(StackExchangeRedisExtensions.Deserialize <IContainerItemCollection>(value));
        }
Exemplo n.º 4
0
        public IDictionary <string, TBag> GetAll <TBag>() where TBag : IBaseContainerBag
        {
            #region 旧方法(没有使用Hash之前)

            //var itemCacheKey = ContainerHelper.GetItemCacheKey(typeof(TBag), "*");
            ////var keyPattern = string.Format("*{0}", itemCacheKey);
            //var keyPattern = GetFinalKey(itemCacheKey);

            //var keys = GetServer().Keys(pattern: keyPattern);
            //var dic = new Dictionary<string, TBag>();
            //foreach (var redisKey in keys)
            //{
            //    try
            //    {
            //        var bag = Get(redisKey, true);
            //        dic[redisKey] = (TBag)bag;
            //    }
            //    catch (Exception)
            //    {

            //    }

            //}

            #endregion

            var key = ContainerHelper.GetItemCacheKey(typeof(TBag), "");
            key = key.Substring(0, key.Length - 1); //去掉:号
            key = GetFinalKey(key);                 //获取带SenparcWeixin:DefaultCache:前缀的Key([DefaultCache]可配置)

            var list = _cache.HashGetAll(key);
            var dic  = new Dictionary <string, TBag>();

            foreach (var hashEntry in list)
            {
                var fullKey = key + ":" + hashEntry.Name;//最完整的finalKey(可用于LocalCache),还原完整Key,格式:[命名空间]:[Key]
                dic[fullKey] = StackExchangeRedisExtensions.Deserialize <TBag>(hashEntry.Value);
            }

            return(dic);
        }
Exemplo n.º 5
0
        public IBaseContainerBag Get(string key, bool isFullKey = false)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }

            if (!CheckExisted(key, isFullKey))
            {
                return(null);
                //InsertToCache(key, new ContainerItemCollection());
            }

            //var cacheKey = GetFinalKey(key, isFullKey);
            var hashKeyAndField = this.GetHashKeyAndField(key, isFullKey);

            //var value = _cache.StringGet(cacheKey);
            var value = _cache.HashGet(hashKeyAndField.Key, hashKeyAndField.Field);

            return(StackExchangeRedisExtensions.Deserialize <IBaseContainerBag>(value));
        }
Exemplo n.º 6
0
        public void InsertToCache(string key, object value)
        {
            if (string.IsNullOrEmpty(key) || value == null)
            {
                return;
            }

            //var cacheKey = GetFinalKey(key);
            var hashKeyAndField = this.GetHashKeyAndField(key);


            //if (value is IDictionary)
            //{
            //    //Dictionary类型
            //}

            //_cache.StringSet(cacheKey, value.Serialize());
            //_cache.HashSet(hashKeyAndField.Key, hashKeyAndField.Field, value.Serialize());
            _cache.HashSet(hashKeyAndField.Key, hashKeyAndField.Field, StackExchangeRedisExtensions.Serialize(value));
#if DEBUG
            var value1 = _cache.HashGet(hashKeyAndField.Key, hashKeyAndField.Field);//正常情况下可以得到 //_cache.GetValue(cacheKey);
#endif
        }