• NHibernate使用MemCache二级缓存


    首先,当然是安装MemCache服务器端了。

      然后配置过程,仅仅两个问题。

      1、NHibernate要与NHibernate.Cache的版本要一致。否则,NHibernate.Caches.MemCache.MemCacheProvider无法实例化。

      2、要引用log4net,否则Memcached.ClientLibrary.SockIOPool无法实例化。

      App.config:

    复制代码
    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <!--配置自定义memcache节点-->
        <section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache"/>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
      </configSections>
      <memcache>
        <memcached host="127.0.0.1" port="11211" weight="2"/>
      </memcache>
      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
          <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
          <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
          <property name="show_sql">true</property>
          <property name="connection.connection_string">Server=KISSDODOG-PC;initial catalog=Test;uid=sa;pwd=123;</property>
          <!--配置二级缓存-->
          <property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property>
          <!--<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>-->
          <property name="cache.use_second_level_cache">true</property>
          <property name="cache.use_query_cache">true</property>
          <property name="cache.default_expiration">300</property>
          <property name="cache.region_prefix">prefix</property>
          <mapping assembly="Model"/>
          <!-- 配置映射的二级缓存 -->
          <class-cache class="Model.PersonModel,Model" usage="read-write"/>
        </session-factory>
      </hibernate-configuration>
      <startup>
        <supportedRuntime version="v2.0.50727"/>
      </startup>
    </configuration>
    复制代码

      Person.hbm.xml

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Model.PersonModel, Model" table="Person">
        <cache usage="read-write"/>
        <id name="PersonId" column="PersonId" type="Int32">
          <generator  class="native"/>
        </id>
        <property name="PersonName" column="PersonName" type="String"/>
        <!--多对一关系:Person属于一个Country name是Person实体类里的-->
        <many-to-one name="Country" column="CountryId" not-null="true" class="Model.CountryModel,Model" foreign-key="FK_Person_Country" />
        <!-- 一个Country里面有多个Person -->
        <set name="ListChild" table="Child" generic="true">
          <key column="ParentId" foreign-key="FK_Child_Person"/>
          <one-to-many class="Model.ChildModel,Model"/>
        </set>
      </class>
    </hibernate-mapping>
    复制代码

      引用如下3个dll:

      

    复制代码
        class Program
        {
            static void Main(string[] args)
            {
                Memcached.ClientLibrary.SockIOPool pool = Memcached.ClientLibrary.SockIOPool.GetInstance();
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
                using (ISession session = sessionFactory.OpenSession())
                {
                    PersonModel p = session.Get<PersonModel>(1);
                    Console.WriteLine(p.PersonId + " : " + p.PersonName);
                }
    
                using (ISession session = sessionFactory.OpenSession())
                {
                    PersonModel p = session.Get<PersonModel>(1);
                    Console.WriteLine(p.PersonId + " : " + p.PersonName);
                }
                Console.ReadKey();
            }
        }
    复制代码

      输出如下:

      

      从结果可以看到,只有第一次查询执行了SQL语句。

      现在停止调试,再次启动调试:

      

      神奇的是,这次两次都没有执行SQL语句。这是因为MemCached是以服务的形式运行在操作系统中,与你的程序是独立的。

      PS:NHibernate的第三方插件,严重跟不上NHibernate的速度。为了使用NHibernate.Cache第三方插件配合 MemCache。不能使用最新版的NHibernate了。看来这个东西的作用还不是那么大。看来,还得学一下MemCache自己实现。

  • 相关阅读:
    Codeforces 1249 F. Maximum Weight Subset
    Codeforces 1249 E. By Elevator or Stairs?
    Codeforces 1249 D2. Too Many Segments (hard version)
    使用subline作为Stata外部编辑器,并实现代码高亮
    Getting a handle on
    Economics degrees
    The threat to world
    他山之石,calling by share——python中既不是传址也不是传值
    Python的几个爬虫代码整理(网易云、微信、淘宝、今日头条)
    一梦江湖费六年——QQ群聊天分析
  • 原文地址:https://www.cnblogs.com/liuguanghai/p/5031872.html
Copyright © 2020-2023  润新知