NHibernate using Memcache level two cache

Source: Internet
Author: User

First of all, of course, install the memcache server side.

Then the configuration process, only two questions.

1, nhibernate to be consistent with the Nhibernate.cache version. Otherwise, NHibernate.Caches.MemCache.MemCacheProvider cannot be instantiated.

2, to reference log4net, otherwise Memcached.ClientLibrary.SockIOPool cannot be instantiated.

App. Config:

<?XML version= "1.0"?><Configuration><configsections><!--Configuring Custom Memcache Nodes-<SectionName= "Memcache"Type= "Nhibernate.caches.memcache.memcachesectionhandler,nhibernate.caches.memcache"/><SectionName= "Hibernate-configuration"Type= "NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/></configsections><Memcache><MemcachedHost= "127.0.0.1"Port= "11211"Weight= "2"/></Memcache><Hibernate-configurationxmlns= "urn:nhibernate-configuration-2.2"><Session-factory><PropertyName= "dialect">nhibernate.dialect.mssql2008dialect</Property><PropertyName= "Connection.provider">nhibernate.connection.driverconnectionprovider</Property><PropertyName= "Show_sql">true</Property><PropertyName= "Connection.connection_string">server=kissdodog-pc;initial catalog=test;uid=sa;pwd=123;</Property><!--Configuring Level Two Caching-<PropertyName= "Cache.provider_class">nhibernate.caches.memcache.memcacheprovider,nhibernate.caches.memcache</Property><!--<property name= "Cache.provider_class" >NHibernate.Cache.HashtableCacheProvider</property>-<PropertyName= "Cache.use_second_level_cache">true</Property><PropertyName= "Cache.use_query_cache">true</Property><PropertyName= "Cache.default_expiration">300</Property><PropertyName= "Cache.region_prefix">prefix</Property><MappingAssembly= "Model"/><!--Configuring a mapped level two cache-<Class-cacheClass= "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-mappingxmlns= "urn:nhibernate-mapping-2.2"><ClassName= "Model.personmodel, Model"Table= "Person"><CacheUsage= "Read-write"/><IdName= "PersonId"Column= "PersonId"Type= "Int32"><GeneratorClass= "Native"/></Id><PropertyName= "PersonName"Column= "PersonName"Type= "String"/><!--A many-to-one relationship: the person belongs to a country name is in the person entity class-<Many-to-oneName= "Country"Column= "Countryid"Not-null= "true"Class= "Model.countrymodel,model"Foreign-key= "Fk_person_country"/><!--There's more than one person in a country.-<SetName= "Listchild"Table= "Child"Generic= "true"> <key column= "ParentID"  Foreign-key = "Fk_child_person" /> <one-to-many class = "Model.childmodel,model" /> </set> </class></ hibernate-mapping>       

Refer to the following 3 DLLs:

  

    Classprogram {Staticvoid Main (String[] args) {Memcached.ClientLibrary.SockIOPool pool =Memcached.ClientLibrary.SockIOPool.GetInstance (); Isessionfactory sessionfactory =NewConfiguration (). 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 (); } }

The output is as follows:

  

As you can see from the results, only the first query executes the SQL statement.

Now stop debugging and start debugging again:

  

Amazingly, the SQL statement was not executed twice this time. This is because memcached is running in the operating system as a service and is independent of your program.

Ps:nhibernate third-party plug-ins, seriously not keep up with nhibernate speed. In order to use Nhibernate.cache third-party plug-ins with MemCache. You cannot use the latest version of NHibernate. It seems that the effect of this thing is not so big. It seems that you have to learn memcache to achieve it yourself.

NHibernate using Memcache level two cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.