The following are a collection of resources and tips if you want to use memcached as the 2nd level caching strategy for NHibernate.  I must admit it was not as easy as it should have been, but only because the required files are not in one place.

Regarding Memcached

Download for windows
http://jehiah.cz/projects/memcached-win32/

Once downloaded you can do the following to install

1. Save exe to C:\memcached\memcached.exe
2. Start > run > cmd
3. cd\
    cd c:\memcached
    C:\memcached>memcached.exe -d install

You can run memcached –h for a help file, the possible options are as follows:

image

NOTE : If you are using Vista then you need to start the command prompt with elevated privileges

Go to Control Panel > Administrative Tools > Services

Locate the memcached service and notice is should be automatic and start it by right clicking and select start

Regarding NHibernate

Link to NHibernate Download - I am using 2.1.0.GA
http://nhforge.org/media/g/nhibernate/default.aspx

NHibernate Caches
http://nhforge.org/media/g/caches/default.aspx

Example config entry:
The memcached client will look for this entry when it initializes, without this it will throw a NullReferenceException.

<section name="memcache"
     type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" /> 

Properties:
Configuration properties then of the memcached client are here.  Not too sure about the weight property but I think this is key to when you add more servers.

  <memcache>
    <memcached host="127.0.0.1" port="11211" weight="2" />
  </memcache> 

Using the Cache element inside the mapping file:
I have chosen to set caching switch inside the mapping files for my business objects.  Their are a few options here which you can find inside the docs, so please read up on which is best for you, i should do the same lol ;-)

  <class name="uk.co.andrewrea.forum.BusinessLogic.Domain.User, uk.co.andrewrea.forum.BusinessLogic" table="[User]">
    <cache usage="read-write"/>

Example hibernate config file:
This is my current hibernate config file I am using, just so it can give you an idea of what one would look like which is working with both lazy loading and distributed 2nd level caching and also using MS SQL 2005.  I have stored my mapping files inside a separate assembly hence the mapping element.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="NHibernate1">
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">
      Server=REA_ANDREW-PC\SQL_EXPRESS_ADV;initial catalog=nhibernate;Integrated Security=true
    </property>
    <property name="adonet.batch_size">10</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="use_outer_join">true</property>
    <property name="command_timeout">60</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="cache.use_query_cache" >true</property>
    <property name="cache.use_second_level_cache">true</property>
    <property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property>
    <mapping assembly="uk.co.andrewrea.forum.BusinessLogic.NHibernate"/>
  </session-factory>
</hibernate-configuration> 

I hope this helps other who are looking to use NHibernate and its caching provider contributions.  Make sure that the versions of NHibernate which the contributions are expecting are correct.  If you have multiple projects inside your solution make sure all of the references are in sync!

Cheers,

Andrew


Tuesday, August 11, 2009 12:29:05 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer