- http://www.tuicool.com/articles/bURJRj [Reids各类数据类型的应用场景]
- https://github.com/antirez/redis [Github Reids]
- https://github.com/StackExchange/StackExchange.Redis [Github StackExchangeReids]
为了实现上述目标,针对如下几种类型进行了思考:git
使用字符串类型来存储集合对象。这种方式存在如下几个问题:github
使用集合类型(LIST/SET)来存储集合类型对象。相对于字符串而言,有以下改进:redis
可是仍然存在如下问题:express
使用HashSet来存储一个对象的每一个FIELD,使用一个对应的KEY来访问对象。这种方式解决了如下问题:缓存
可是没法提供集合支持。服务器
使用一个SortedSet来记录数据集合的全部的KEY,使用不一样的KEY指向的HashSet存储集合元素数据。这个方案知足了上述全部的需求,是目前采起的方式。可是仍然有如下问题:app
为了保证存储在Redis的键值对逻辑上的惟一性,在实现上述方案的时候使用了较长的KEY。一个KEY由如下几个部分组成:性能
在Redis中,一个KEY应该形如:[WellKnownReidsKeys][TypeSpecifiedKey][CustomizedKey]。其中,CustomizedKey能够将同类型的数据集合拆分红不一样的区块,独立管理。单元测试
使用了运行时构造表达式目录树进行编译的方式来减小反射开销,代码以下:测试
public Func<T, IDictionary<string, string>> Compile(string key) { var outType = typeof (Dictionary<string, string>); var func = ConcurrentDic.GetOrAdd(key, k => { var tType = typeof (T); var properties = tType.GetProperties(); var expressions = new List<Expression>(); //public T xxx(IDataReader reader){ var param = Expression.Parameter(typeof (T)); //var instance = new T(); var newExp = Expression.New(outType); var varExp = Expression.Variable(outType, "instance");v var varAssExp = Expression.Assign(varExp, newExp); expressions.Add(varAssExp); var indexProp = typeof (IDictionary<string, string>).GetProperties().Last(p => p.Name == "Item"); var strConvertMethod = typeof (object).GetMethod("ToString"); foreach (var property in properties) { var propExp = Expression.PropertyOrField(param, property.Name); Expression indexAccessExp = Expression.MakeIndex(varExp, indexProp, new Expression[] {Expression.Constant(property.Name)}); var strConvertExp = Expression.Condition(Expression.Equal(Expression.Constant(null), Expression.Convert(propExp,typeof(object))), Expression.Constant(string.Empty), Expression.Call(propExp, strConvertMethod)); var valueAssignExp = Expression.Assign(indexAccessExp, strConvertExp); expressions.Add(valueAssignExp); } //return instance; var retarget = Expression.Label(outType); var returnExp = Expression.Return(retarget, varExp); expressions.Add(returnExp); //} var relabel = Expression.Label(retarget, Expression.Default(outType)); expressions.Add(relabel); var blockExp = Expression.Block(new[] {varExp}, expressions); var expression = Expression.Lambda<Func<T, IDictionary<string, string>>>(blockExp, param); return expression.Compile(); }); return func; }
对于单次转换,表达式的编译结果根据类型信息和字典的KEY信息作了缓存,从而提高性能。对于集合转换,对于每一个集合的操做,每次使用的委托都是同一个从而减小了字典索引的开销。如下是一个以硬编码代码为了测试基准的性能比对:
public void ModelStringDicTransfer() { var customer = new ExpressionFuncTest.Customer { Id = Guid.NewGuid(), Name = "TestMap", Age = 25, Nick = "Test", Sex = 1, Address = "Hello World Street", Tel = "15968131264" }; const int RunCount = 10000000; GetDicByExpression(customer); var time = StopwatchHelper.Timing(() => { int count = RunCount; while (count-- > 0) { GetDicByExpression(customer); } }); var baseTime = StopwatchHelper.Timing(() => { int count = RunCount; while (count-- > 0) { GetDicByHardCode(customer); } }); Console.WriteLine("time:{0}\tbasetime:{1}", time, baseTime); Assert.IsTrue(baseTime * 3 >= time); } private Func<ExpressionFuncTest.Customer, IDictionary<string, string>> _dicMapper; private IDictionary<string, string> GetDicByExpression(ExpressionFuncTest.Customer customer) { _dicMapper = _dicMapper ?? ModelStringDicTransfer<ExpressionFuncTest.Customer>.Instance.Compile( typeof(ExpressionFuncTest.Customer).FullName); return _dicMapper(customer); } private Dictionary<string, string> GetDicByHardCode(ExpressionFuncTest.Customer customer) { var dic = new Dictionary<string, string>(); dic.Add("Name", customer.Name); dic.Add("Address", customer.Address); dic.Add("Nick", customer.Nick); dic.Add("Tel", customer.Tel); dic.Add("Id", customer.Id.ToString()); dic.Add("Age", customer.Age.ToString()); dic.Add("Sex", customer.Sex.ToString()); return dic; }
对于10M的转换量,硬编码耗时6s左右,动态转换耗时10s左右。
如下是一个针对已经完成的实现的测试:
public void PerformanceTest() { var amount = 1000000; var key = "PerformanceTest"; Fill(amount, key); PageGetFirst(1, key); int i = 1; while (i <= 100000) { var count = i; var fTime = StopwatchHelper.Timing(() => PageGetFirst(count, key)); var lTime = StopwatchHelper.Timing(() => PageGetLast(count, key)); Console.WriteLine("{0}:第一页耗时:{1}\t最后一页耗时:{2}", count, fTime, lTime); i = i*10; } } private void Fill(int count,string partKey) { var codes = Enumerable.Range(1000, count).Select(i => i.ToString()); codes.Foreach(i => { var customer = new Customer { Id = i == "1000" ? Guid.Empty : Guid.NewGuid(), Name = "Customer" + i, Code = i, Address = string.Format("XX街{0}号", DateTime.Now.Millisecond), Tel = "15968131264" }; _pagableHashStore.UpdateOrInsertAsync(customer, customer.Code + "", partKey).Wait(); }); } private void PageGetFirst(int count,string partKey) { var pageInfo = new PageInfo(count, 1); _pagableHashStore.PageAsync(pageInfo, partKey).Result .Foreach(i => i.Wait()); } private void PageGetLast(int count, string partKey) { var pageInfo = new PageInfo(count, (100000 - 1)/count + 1); _pagableHashStore.PageAsync(pageInfo, partKey).Result .Foreach(i => i.Wait()); }
对于10M数据的分页测试(默认的插入时间排序,不一样的页长)的结果(时间单位:毫秒):