NHibernate使用MemCache二级缓存

时间:2023-03-08 22:25:14

  首先,当然是安装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:

  NHibernate使用MemCache二级缓存

    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>();
Console.WriteLine(p.PersonId + " : " + p.PersonName);
} using (ISession session = sessionFactory.OpenSession())
{
PersonModel p = session.Get<PersonModel>();
Console.WriteLine(p.PersonId + " : " + p.PersonName);
}
Console.ReadKey();
}
}

  输出如下:

  NHibernate使用MemCache二级缓存

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

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

  NHibernate使用MemCache二级缓存

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

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