Using MemCached with NHibernate
There's nothing says how to use MemCached in NHibernate, here is the steps to approach this. It's very easy in fact.
1. Install and start MemCached. http://jehiah.cz/projects/memcached-win32/.
2. Copy the ICSharpCode.SharpZipLib.dll, Memcached.ClientLibrary.dll and NHibernate.Caches.MemCache.dll from NHB's bin dir to your app's dir.
3. Make sure the following configuration section exists in your app.config or web.config:
4. Tell NHB which cache provider class you are using in hibernate.config:
In the end don't forget to give out your cache strategies in your .hbm files or queries.
The following is the configuration properties that can be used in the <session-factory> node in hibernate.config (documentations from .NET memcached client library):
failover(bool): If this flag is set to true and a socket fails to connect, the pool will attempt to return a socket from another server if one exists. If set to false, then getting a socket will return null if it fails to connect to the requested server.
initial_connections(Int32): the initial number of connections per server setting in the available pool.
maintenance_sleep(Int64): the sleep time(in milliseconds) between runs of the pool maintenance thread. If set to 0, then the maintenance thread will not be started.
max_busy(Int64): the maximum busy time(in milliseconds) for threads in the busy pool.
max_connections(Int32): the maximum number of spare connections allowed in the available pool.
max_idle(Int64): the maximum idle time for threads in the avaiable pool.
min_connections(Int32): the minimum number of spare connections to maintain in the available pool.
nagle(bool): the Nagle algorithm flag for the pool. If false, will turn off Nagle's algorithm on all sockets created.
socket_timeout(Int32): the socket timeout(in milliseconds) for reads.
socket_connect_timeout(Int32): the socket timeout(in milliseconds) for connects.
1. Install and start MemCached. http://jehiah.cz/projects/memcached-win32/.
2. Copy the ICSharpCode.SharpZipLib.dll, Memcached.ClientLibrary.dll and NHibernate.Caches.MemCache.dll from NHB's bin dir to your app's dir.
3. Make sure the following configuration section exists in your app.config or web.config:
<configSections>
<section name="memcache"
type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
</configSections>
<memcache>
<memcached host="127.0.0.1" port="11211" weight="2" />
</memcache>
The weight attribute correspond to SockIOPool.Weight property in Memcached.ClientLibrary, representing the buckets number assigned to the server. In other words it indicates the load ability among the MemCached servers.<section name="memcache"
type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
</configSections>
<memcache>
<memcached host="127.0.0.1" port="11211" weight="2" />
</memcache>
4. Tell NHB which cache provider class you are using in hibernate.config:
<add key="hibernate.cache.provider_class" value="NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache" />
In the end don't forget to give out your cache strategies in your .hbm files or queries.
The following is the configuration properties that can be used in the <session-factory> node in hibernate.config (documentations from .NET memcached client library):
failover(bool): If this flag is set to true and a socket fails to connect, the pool will attempt to return a socket from another server if one exists. If set to false, then getting a socket will return null if it fails to connect to the requested server.
initial_connections(Int32): the initial number of connections per server setting in the available pool.
maintenance_sleep(Int64): the sleep time(in milliseconds) between runs of the pool maintenance thread. If set to 0, then the maintenance thread will not be started.
max_busy(Int64): the maximum busy time(in milliseconds) for threads in the busy pool.
max_connections(Int32): the maximum number of spare connections allowed in the available pool.
max_idle(Int64): the maximum idle time for threads in the avaiable pool.
min_connections(Int32): the minimum number of spare connections to maintain in the available pool.
nagle(bool): the Nagle algorithm flag for the pool. If false, will turn off Nagle's algorithm on all sockets created.
socket_timeout(Int32): the socket timeout(in milliseconds) for reads.
socket_connect_timeout(Int32): the socket timeout(in milliseconds) for connects.