一级缓存可以与ICriteria或其他API一起使用吗? [英] Can first level cache be used with ICriteria or other APIs?
问题描述
在NHibernate
中,使用Load
或Get
方法可以轻松地从一级缓存中受益.但是ICriteria
,HQL
,Linq-to-NHibernate
和QueryOver
呢?他们也使用一级缓存吗?
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屋!