在下第一次使用NHibernate,看中了它的二级缓存,不过发现不开启缓存的话,数据是正常的,一旦开启就发现获取了很多重复数据,我试了NHibernate.Caches.MemCache和NHibernate.Caches.EnyimMemcached这两个缓存实现,发现bug是一样的,而使用NHibernate自带的NHibernate.Cache.HashtableCacheProvider缓存实现发现是正常的,所以我猜测是生成的key重复了的问题,因为memcache(我用windows版本)的key最长不能超过250,所以肯定会对超长的key进行散列,我用反编译工具把NHibernate.Caches.MemCache的代码抠出来调试了一下果然很多key散列成了一样的,比如下面这两个key:
key1:
Edu.DB:[email protected]: /* criteria query */ SELECT this_.NewsClassify_ID as NewsClas1_11_0_, this_.NewsClassify_Name as NewsClas2_11_0_, this_.NewsClassify_Path as NewsClas3_11_0_, this_.NewsClassify_State as NewsClas4_11_0_, this_.NewsClassify_Icon as NewsClas5_11_0_, this_.NewsClassify_Parent as NewsClas6_11_0_ FROM NewsClassify this_ WHERE this_.NewsClassify_Name = ? and this_.NewsClassify_State = ?; parameters: []; named parameters: {‘cp0‘=‘专升本留学申请‘, ‘cp1‘=‘1‘}; first row: 0
key2:
Edu.DB:[email protected]: /* criteria query */ SELECT this_.NewsClassify_ID as NewsClas1_11_0_, this_.NewsClassify_Name as NewsClas2_11_0_, this_.NewsClassify_Path as NewsClas3_11_0_, this_.NewsClassify_State as NewsClas4_11_0_, this_.NewsClassify_Icon as NewsClas5_11_0_, this_.NewsClassify_Parent as NewsClas6_11_0_ FROM NewsClassify this_ WHERE this_.NewsClassify_Name = ? and this_.NewsClassify_State = ?; parameters: []; named parameters: {‘cp0‘=‘博士研究生申请‘, ‘cp1‘=‘1‘}; first row: 0
以上两个key只有结尾处的几个中文稍微有些区别,结果都散列成了bMRPNkL2ZgFqs6v1butPO2rZR+4=,这重复性太高了(虽然散列有重复是必然的)
我准备代码扣出来,把散列key的地方用md5加密重新实现一下,应该就可以了,具体代码就不发了,因为没几个代码,我发这条随笔就是把这个现象反应一下。