一级缓存可以与ICriteria或其他API一起使用吗? [英] Can first level cache be used with ICriteria or other APIs?

查看:99
本文介绍了一级缓存可以与ICriteria或其他API一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NHibernate中,使用LoadGet方法可以轻松地从一级缓存中受益.但是ICriteriaHQLLinq-to-NHibernateQueryOver呢?他们也使用一级缓存吗?

In NHibernate you can easily benefit from first level cache when using Load or Get methods. But what about ICriteria, HQL, Linq-to-NHibernate and QueryOver? Do they use first level cache too?

推荐答案

他们将其用于返回实体,但是查询将直接进入db,除非您使用二级缓存.

They use it for returning entities, but the queries go straight to the db unless you use the second level cache.

考虑一下:

var fooUsingGet = session.Get<Foo>(fooId);
var fooQueryById = session.Query<Foo>().Single(f => f.Id == fooId);

执行两个查询(一个用于Get,一个用于Query),但是两个变量都包含相同的对象引用.

Two queries are executed (one for the Get, one for the Query), but both variables contain the same object reference.

现在,如果启用二级缓存,请查询缓存并为查询指定缓存:

Now, if you enable the 2nd level cache, query caching, and specify caching for the query:

var fooQueryById = session.Query<Foo>().Cacheable()
                          .Single(f => f.Id == fooId);
var fooQueryByIdAgain = session.Query<Foo>().Cacheable()
                               .Single(f => f.Id == fooId);

将仅执行一个查询.

这篇关于一级缓存可以与ICriteria或其他API一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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