简体   繁体   English

NHibernate和Memcached - 教程/示例

[英]NHibernate and Memcached - Tutorial/Example

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. 我安装了几个桶设置的Membase服务器,我正在寻找一个很好的教程或如何使用它作为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. 我感兴趣的是示例配置的样子,以及我是否需要在代码中执行任何操作,或者是否可以从我的NHibernate映射中处理所有内容。

Thanks for any assistance. 谢谢你的帮助。

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). 选项是读写(读提交隔离),非严格读写(很少写入的对象,性能更好但过时数据的可能性增加)或只读(永不改变的数据)。

Then, in your web (or app) config you need a section to configure memcached: 然后,在您的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>

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>

Of course you will need to download and reference a dll from the appropriate version of NHibernate.Caches to get the right cache provider. 当然,您需要从适当版本的NHibernate.Caches下载并引用dll才能获得正确的缓存提供程序。 The memcached one takes a dependency on ICSharpCode.SharpZipLib and Memcached.ClientLibrary as well (s/b included in the download) memcached也依赖于ICSharpCode.SharpZipLib和Memcached.ClientLibrary(包含在下载中的s / b)

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,则可以使用会话工厂的设置链中的.Cache方法,但需要通过调用.ExposeConfiguration手动设置某些属性。

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

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