在MVC应用程序配置NHibernate的二级缓存 [英] Configuring NHibernate second level caching in an MVC app

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

问题描述

我有一个的使用NHibernate的MVC3应用程序。一切都顺利,直到我开始尝试并添加第二级缓存。浏览网页了几个小时后,我终于找到了我认为是正确的DLL(NHibernate.Caches.SysCache2.dll),并把它添加到我的项目。

I have an MVC3 app that's using NHibernate. All was going well until I started to try and add second level caching. After browsing the web for a few hours I finally found what I think is the right dll (NHibernate.Caches.SysCache2.dll) and have added it to my project.

不过,我无法找到与MVC应用程序配置它的任何帮助。所有的例子指的是具有一个App.config文件(I presume这是Web窗体)。我是一个Java开发谁是学习的.Net,所以我不那么熟悉如何操纵一切。

However, I can't find any help for configuring it with an MVC app. All the examples refer to having an App.config file (I presume this is for Web Forms). I'm a Java developer who's learning .Net so I'm not so familiar with how to rig everything up.

有人能指出我在正确的方向。我使用XML休眠(HBM)的文件。

Could someone point me in the right direction. I'm using xml hibernate (hbm) files.

感谢。

推荐答案

我使用的是MVC3和NHibernate.Caches.SysCache.dll这样的二级缓存...

I'm using 2nd level caching with MVC3 and NHibernate.Caches.SysCache.dll like this...

1,添加配置节这样

<configSections>
    <section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache, Version=3.0.0.4000, Culture=neutral, PublicKeyToken=6876f2ea66c9f443"/>
</configSections>

添加一个syscache部分你的web.config在你的配置部分是这样的:

add a syscache section to your web.config under your configuration section like this:

<syscache>
    <cache region="SomeCustomNameRegion" expiration="86400" priority="5" />
</syscache>

在我的hibernate.cfg.xml文件中,我有以下属性补充说:

in my hibernate.cfg.xml file i have the following properties added:

<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>

我使用FluentNhibernate创建我的映射,并添加此支持缓存:

I'm using FluentNhibernate to create my mappings and add this to support caching:

public CustomerClassMap()
{
    Cache.NonStrictReadWrite();
    Id(x => x.Id);
    Map(x => x.Name);
    //... more properties
}

然后在我的数据访问code,我有类似这样的,我想缓存查询的内容:

And then in my data access code, I have something similar to this for queries that I want cached:

public IEnumerable<Customer> GetAllCached()
{
    return Session.CreateCriteria(typeof(Customer))
            .SetCacheable(true)
            .SetCacheRegion("SomeCustomNameRegion")
            .SetCacheMode(CacheMode.Normal)
            .AddOrder(Order.Desc("CreateDate"))
            .List<Customer>();
}

下面是一些有用的链接进入一个更详细一点。没有什么具体的关于MVC3,你需要担心的,最是不变的从早期版本的NHibernate的,据我可以告诉。

Here are some helpful links to get into a little more detail. There's nothing specific about MVC3 that you need to worry about, and most is unchanged from earlier versions of NHibernate as far as i can tell.

<一个href=\"http://www.klopfenstein.net/lorenz.aspx/using-syscache-as-secondary-cache-in-nhibernate\">http://www.klopfenstein.net/lorenz.aspx/using-syscache-as-secondary-cache-in-nhibernate

<一个href=\"http://blog.symbiotic-development.com/2008/02/27/more-configuring-nhibernate-caches/\">http://blog.symbiotic-development.com/2008/02/27/more-configuring-nhibernate-caches/

这篇关于在MVC应用程序配置NHibernate的二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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