【问题标题】:NHibernate and Memcached - Tutorial/ExampleNHibernate 和 Memcached - 教程/示例
【发布时间】:2011-08-08 23:33:44
【问题描述】:

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

我对示例配置的外观以及我是否需要在代码中做任何事情或是否可以从我的 NHibernate 映射中处理这一切感兴趣。

感谢您的帮助。

【问题讨论】:

    标签: c# .net nhibernate memcached membase


    【解决方案1】:

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

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

    选项有读写(读取提交隔离)、非严格读写(很少写入的对象、更好的性能但增加过时数据的机会)或只读(永远不会更改的数据)。

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

    当然,您需要从NHibernate.Caches 的适当版本下载并引用一个dll 以获得正确的缓存提供程序。 memcached 也依赖于 ICSharpCode.SharpZipLib 和 Memcached.ClientLibrary(下载中包含 s/b)

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

    【讨论】:

    • 我发现这个例子不完整。首先,您必须在某处安装 Memcached 服务器(如果您想在 Windows 机器上作为服务安装,请参阅xrigher.info/php/… 上的部分说明)。添加 Memcached 客户端二进制文件时,我发现 3.2 的二进制文件依赖于 log4net.dll,因此我必须将其与 Memcached 客户端二进制文件一起复制。此外,添加到期密钥会在我的项目中产生错误
    • 如何为 Windows Azure VM 安装 memcache
    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2011-03-08
    • 2017-10-16
    • 2013-09-18
    • 1970-01-01
    • 2012-02-01
    • 2017-01-07
    • 2014-11-20
    相关资源
    最近更新 更多