NHibernate二级缓存性能问题 [英] NHibernate second level cache performance issue

查看:73
本文介绍了NHibernate二级缓存性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NHibernate开发每个会话的请求模式的MVC应用程序.大多数时候,用户只是读取数据,因此,我试图通过以下方式使用NHibernate的二级缓存:我已经设置SysCache并使所有持久性实体都可缓存(缓存用法="nonstrict-read-write" ),然后在应用程序启动时进行调用,以从数据库中加载所有常用实体,如下所示:

I am developing a MVC application with NHibernate using the session per request pattern. Most of the time users just read data and therefore i am trying to make use of NHibernate's second level cache in the following manner: I've setup SysCache and have made all my persistant entities cacheable (cache usage="nonstrict-read-write") and on application start I make calls to load all commonly used entities from the database like so:

var all = Session.QueryOver<T>().Cacheable().List<T>()

出于评估目的,我跟踪了上述调用的执行时间,大约有50000个结果,这是第一次〜5秒,而对缓存查询的任何后续调用都是〜2.5秒....NHibernate Profiler对数据库花费不到100毫秒,那么花费那么多时间呢?我尝试了切换提供程序,但使用Velocity和Memcached获得了类似的结果(即使不是更糟)……我读到了几乎所有有关NHibernate及其对二级缓存的使用的信息,尽管我可能并不完全正确,但我认为在上面的语句中,发生的事情是:构造了50000个对象,并将它们的数据存储在实体和查询缓存中,并将会话时间戳保存在时间戳缓存中.在i5机器上怎么可能需要5秒钟?即使那很正常,在随后的调用中,读取缓存的数据又如何花费2.5秒而之间没有任何变化呢?由于我是NHibernate的新手,所以你们中的任何人都可以帮助我弄清楚我做错了什么吗?任何帮助将不胜感激...我已经把头撞在墙上一个星期了...

For evaluation purposes I trace the execution time for the above call and for approximately 50000 results it's ~5 seconds the first time and ~2.5 seconds for any subsequent calls to the cached query.... NHibernate Profiler says the query to the database takes less than 100ms, what takes so much time then? I tried switching providers but got similar (if not worse) results with Velocity and Memcached... I read virtually all I could find regarding NHibernate and its use of the second level cache and I think, though I may not be entirely correct, that in the above statement what happens is: 50000 objects are constructed and their data is stored in the entity and query caches and the session timestamp saved in the timestamp cache. How could that take 5 seconds on an i5 machine? Even if that is normal how just reading the cached data take 2.5 seconds in the subsequent calls with no changes inbetween? Since I am relatively new to NHibernate, can any of you help me figure out what I am doing wrong? Any help would be greatly appreciated...I've been banging my head against the wall for a week now...

推荐答案

您不应在缓存中放入50000个实体,这否定了拥有数据库的意义,尤其是当您的数据来自SELECT * FROM TABLE时.如果来自真正昂贵的查询,则缓存成本低于查询成本,应将其放入缓存中.

You shouldn't put 50000 entities in your cache, that negates the point of having a database, especially if your data comes from a SELECT * FROM TABLE. If it comes from a REALLY expensive query, then caching cost is less than query cost, and it should be put in the cache.

使用查询获取特定数据.然后,查找进行查询最多的页面,并在必要时使用缓存对其进行优化.

Use queries to get specific data. Then, look for the pages that make most queries, and optimize them, using a cache if necessary.

这篇关于NHibernate二级缓存性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆