简体   繁体   English

Nhibernate LINQ-缓存问题

[英]Nhibernate LINQ - caching problems

I have the following code 我有以下代码

factory = new Configuration().Configure().BuildSessionFactory();
session = factory.OpenSession();
var query = session.Linq<Root>();
var data = query.ToList<Root>();
var data2 = query.ToList<Root>();

This one generate 2 SQL queries, so i understand that first level cache isn't working. 这个生成2个SQL查询,所以我知道一级缓存不起作用。 Does LINQ for nhibernate manage first level cache ? LINQ for nhibernate是否管理一级缓存? If yes, how to configure it ? 如果是,该如何配置?

Thank's by advance. 提前谢谢。

It is a misconception that the first level cache avoids execution of sql in general. 一级缓存通常会避免执行sql,这是一个误解。

Queries are always executed on the database. 查询总是在数据库上执行。 This makes 100% sure that the result is exactly the same regardless if the instances are already in memory or not. 这可以100%确保结果完全相同,无论实例是否已经在内存中。 This could be important for instance when using joins or conditions. 例如,在使用联接或条件时,这可能很重要。 There is no difference if the query actually contains joins or a where clause. 如果查询实际上包含联接或where子句,则没有区别。

The first level cache make sure that the same instance is returned if it represents the same instance in the database. 如果一级缓存表示数据库中的同一实例,则确保返回相同的实例。 NHibernate executes the sql, when it finds the same object in memory as returned by the query, then it returns the one in the cache. NHibernate执行sql,当它在内存中找到与查询返回的对象相同的对象时,它将在缓存中返回该对象。 But it can't know before executing the sql. 但是在执行sql之前它不知道。

Only when you use session.Get or session.Load , NH does a cache lookup and avoids a query when it finds the instance. 仅当您使用session.Getsession.Load ,NH才会进行缓存查找,并在找到实例时避免查询。

AFAIK, Linq for NHibernate does nothing special with the cache. AFAIK,用于NHibernate的Linq对缓存没有什么特别的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM