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); } } }
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); }
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, ""))); } }