首先,當然是安裝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自己實現。