NHibernate和Memcached的 - 教程/示例 [英] NHibernate and Memcached - Tutorial/Example

查看:166
本文介绍了NHibernate和Memcached的 - 教程/示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇桶设置Membase的服务器上安装和我一直在寻找一个很好的教程如何使用这个作为与NHibernate的二级缓存或例子。

I have the Membase server installed with a couple buckets setup and I was looking for a good tutorial or example of how to use this as the 2nd level cache with NHibernate.

我感兴趣的一个示例配置是什么样子,如果有什么我需要做的code,或者如果我能处理这一切从我的NHibernate的映射。

I am interested in what a sample configuration would look like and if there is anything I need to do in code or if I can handle it all from my NHibernate mappings.

感谢您的任何帮助。

推荐答案

在你的映射文件,则需要包括属性:

In your mapping files, you will need to include the property:

<class name="ClassName" table="Table">
   <cache usage="read-write" />
   <!-- SNIP -->
</class>

选项是可读写的(读提交隔离),非严格读写(即很少写的对象,更好的性能,但是陈旧数据的机会增加),或只读(数据永远不会改变)。

Options are read-write (read committed isolation), nonstrict-read-write (objects that are rarely written, better performance but increased chance of stale data), or read-only (data that never changes).

然后,在你的网页(或应用程序)的配置,你需要一款配置memcached的:

Then, in your web (or app) config you need a section to configure memcached:

<configuration>
  <configSections>
    <!-- SNIP -->
    <section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
  </configSections>
  <memcache>
    <memcached host="127.0.0.1" port="11211" weight="2" />
  </memcache>
  <!-- SNIP -->
</configuration>

最后,在你的会话工厂的配置一定要使用:

Finally, in your session factory configuration be sure to use:

  <hibernate-configuration>
    <session-factory>
      <!-- SNIP -->

      <property name="expiration">300</property> <!--memcache uses seconds -->
      <property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property>
      <property name="cache.use_second_level_cache">true</property>
      <property name="cache.use_query_cache">false</property> <!-- true if you want to cache query results -->
    </session-factory>
  </hibernate-configuration>

当然,你需要下载和从 NHibernate.Caches 以获得正确的缓存提供者。 Memcached的1发生在ICSharp code.SharpZipLib和Memcached.ClientLibrary的依赖,以及(S / B包含在下载)

Of course you will need to download and reference a dll from the appropriate version of NHibernate.Caches to get the right cache provider. The memcached one takes a dependency on ICSharpCode.SharpZipLib and Memcached.ClientLibrary as well (s/b included in the download)

如果您使用的是一口流利的NHibernate的,有在设置链的会话工厂,你可以用一个.Cache方法,虽然有些属性需要手动设置通过调用.ExposeConfiguration。

If you're using fluent NHibernate, there is a .Cache method in the setup chain for a session factory that you can use, though some of the properties need to be set manually through a call to .ExposeConfiguration.

这篇关于NHibernate和Memcached的 - 教程/示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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