Nhibernate LINQ-缓存问题 [英] Nhibernate LINQ - caching problems

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

问题描述

我有以下代码

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

这个生成2条SQL查询,所以我知道一级缓存不起作用. LINQ for nhibernate是否管理一级缓存?如果是,该如何配置?

This one generate 2 SQL queries, so i understand that first level cache isn't working. Does LINQ for nhibernate manage first level cache ? If yes, how to configure it ?

先谢谢了.

推荐答案

一个误解是,一级缓存通常避免执行sql.

It is a misconception that the first level cache avoids execution of sql in general.

查询始终在数据库上执行.这样可以100%确保结果完全相同,无论实例是否已经在内存中.例如,在使用联接或条件时,这可能很重要.如果查询实际上包含联接或where子句,则没有区别.

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. 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.

如果一级缓存代表数据库中的同一实例,请确保返回相同的实例. NHibernate执行sql,当它在内存中找到与查询返回的对象相同的对象时,则在缓存中返回该对象.但是在执行sql之前是不知道的.

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. But it can't know before executing the sql.

仅当使用session.Getsession.Load时,NH才会进行缓存查找,并在找到实例时避免查询.

Only when you use session.Get or session.Load, NH does a cache lookup and avoids a query when it finds the instance.

AFAIK,适用于NHibernate的Linq在缓存方面没有什么特别之处.

AFAIK, Linq for NHibernate does nothing special with the cache.

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

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