NHibernate 和 Memcached - 教程/示例

发布于 2024-11-02 11:48:28 字数 168 浏览 7 评论 0原文

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

我对示例配置的外观感兴趣,以及是否需要在代码中执行任何操作,或者我是否可以通过 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.

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.

Thanks for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2024-11-09 11:48:28

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

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

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

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

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:

<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. The memcached one takes a dependency on ICSharpCode.SharpZipLib and Memcached.ClientLibrary as well (s/b included in the download)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文