NHibernate 和 Memcached - 教程/示例

2024-03-22

我安装了带有几个存储桶设置的 Membase 服务器,并且我正在寻找一个很好的教程或示例来说明如何将其用作 NHibernate 的二级缓存。

我对示例配置的外观感兴趣,以及是否需要在代码中执行任何操作,或者我是否可以通过 NHibernate 映射来处理所有这些内容。

感谢您的任何帮助。


在您的映射文件中,您需要包含以下属性:

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

选项包括读写(读提交隔离)、非严格读写(很少写入的对象,性能更好,但增加了过时数据的可能性)或只读(永不更改的数据)。

然后,在您的 Web(或应用程序)配置中,您需要一个部分来配置 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>

最后,在会话工厂配置中请务必使用:

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

当然,您需要从适当的版本下载并引用 dllNHibernate 缓存 http://sourceforge.net/projects/nhcontrib/files/NHibernate.Caches/获得正确的缓存提供者。 memcached 也依赖 ICSharpCode.SharpZipLib 和 Memcached.ClientLibrary(s/b 包含在下载中)

如果您使用的是流畅的 NHibernate,则可以使用会话工厂的设置链中的 .Cache 方法,尽管某些属性需要通过调用 .ExposeConfiguration 手动设置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NHibernate 和 Memcached - 教程/示例 的相关文章

随机推荐