Exemplo n.º 1
0
        protected override object GetInner(Type type, string key)
        {
            ValidParams(key);
            Projects.Tool.Accumlator.Accumlator.GetInstance().Increment("RedisHash" + type.FullName);

            using (var client = CreateRedisClient())
            {
                var keyPrefix = GetKeyPrefix(key);
                var keys      = ObjectHashMappingUtil.GetKeys(type, keyPrefix);
                var values    = client.GetValuesFromHash(CacheName, keys).ToArray();
                if (values.All(o => String.IsNullOrEmpty(o)))
                {
                    return(null);
                }

                try
                {
                    return(ObjectHashMappingUtil.ToObject(type, values));
                }
                catch (Exception ex)
                {
                    log.WarnFormat("反序列化数据 {0} 发生错误 {1}", key, ex.ToString());
                    return(null);
                }
            }
        }
Exemplo n.º 2
0
        public override IEnumerable <CacheItemResult> GetBatchResult(Type type, IEnumerable <string> keys)
        {
            //产生 field keys
            List <BatchItem> items = new List <BatchItem>();

            foreach (var key in keys)
            {
                ValidParams(key);
                var item = new BatchItem()
                {
                    SourceKey = key,
                    FieldKeys = ObjectHashMappingUtil.GetKeys(type, GetKeyPrefix(key))
                };
                items.Add(item);
            }

            //批量获取数据
            string[] allValues;
            using (var client = CreateRedisClient())
            {
                var allKeys = items.SelectMany(o => o.FieldKeys).ToArray();
                allValues = client.GetValuesFromHash(CacheName, allKeys).ToArray();
            }

            //转换数据为对象
            List <CacheItemResult> outputs = keys.Select(o => new CacheItemResult()).ToList();
            int index = 0;

            foreach (var item in items)
            {
                var output = outputs[index];
                output.Key = item.SourceKey;

                var values = new string[item.FieldKeys.Length];
                Array.Copy(allValues, index * item.FieldKeys.Length, values, 0, values.Length);

                //判断是否命中
                if (values.All(o => String.IsNullOrEmpty(o)))
                {
                    output.IsMissing = true;
                }
                else
                {
                    try
                    {
                        output.Value = ObjectHashMappingUtil.ToObject(type, values);;
                    }
                    catch (Exception ex)
                    {
                        log.WarnFormat("反序列化数据 {0} 发生错误 {1}", output.Key, ex.ToString());
                        output.IsMissing = true;
                    }
                }
                index++;
            }
            TraceCache(keys, outputs.GetMissingKeys().Count());
            return(outputs);
        }
Exemplo n.º 3
0
        protected override void RemoveInner(Type type, string key)
        {
            ValidParams(key);

            using (var client = CreateRedisClient())
            {
                var keyPrefix = GetKeyPrefix(key);
                var keys      = ObjectHashMappingUtil.GetKeys(type, keyPrefix);
                client.SetRangeInHash(CacheName, keys.Select(o => new KeyValuePair <string, string>(o, "")));
            }
        }
Exemplo n.º 4
0
        protected override void SetInner <T>(string key, T value, DateTime expiredTime)
        {
            ValidParams(key);

            using (var client = CreateRedisClient())
            {
                var keyPrefix = GetKeyPrefix(key);
                var kvs       = ObjectHashMappingUtil.ToHash(value, keyPrefix);
                client.SetRangeInHash(CacheName, kvs);
            }
        }
Exemplo n.º 5
0
        public override void SetBatch <T>(IEnumerable <CacheItem <T> > items, DateTime expiredTime)
        {
            List <KeyValuePair <string, string> > datas = new List <KeyValuePair <string, string> >();

            foreach (var item in items)
            {
                var keyPrefix = GetKeyPrefix(item.Key);
                var kvs       = ObjectHashMappingUtil.ToHash(item.Value, keyPrefix);
                datas.AddRange(kvs);
            }

            using (var client = CreateRedisClient())
            {
                client.SetRangeInHash(CacheName, datas);
            }
        }