Spring for .NET Community Forums    

Go Back   Spring for .NET Community Forums > General > NHibernate

Reply
 
Thread Tools Display Modes
  #1  
Old 01-12-2009, 02:35 AM
abstraction abstraction is offline
Junior Member
New User
 
Join Date: Mar 2007
Posts: 10
Default Second Level Cache Configuration

Would someone post an example of their working 2nd level cache configuration? My google fu isn't turning anything up.

Thanks!
Reply With Quote
  #2  
Old 02-23-2009, 08:22 PM
abstraction abstraction is offline
Junior Member
New User
 
Join Date: Mar 2007
Posts: 10
Default

Let me rephrase my question. Has anyone successfully used Spring+NHibernate+2nd level cache?
Reply With Quote
  #3  
Old 02-25-2009, 07:10 AM
superciccio superciccio is offline
Member
Spring User
 
Join Date: Apr 2008
Posts: 44
Default

Yes, I use Spring+NHibernate+2nd level cache successfully.
For other reasons, I manage almost all dinamically in code.

This is the piece of code that initializes a Session Factory:



Spring.Data.NHibernate.LocalSessionFactoryObject sessionFactory;

sessionFactory = new LocalSessionFactoryObject();

// provider

sessionFactory.DbProvider = pDbProvider; // custom DelegatingDbProvider object
sessionFactory.HibernateProperties.Add("connection .driver_class", pHibernateProvider); // for eg. "NHibernate.Driver.OracleDataClientDriver"
sessionFactory.HibernateProperties.Add("dialect", pHibernateDialect); // for eg. "NHibernate.Dialect.Oracle9Dialect"

// cache

sessionFactory.HibernateProperties.Add("cache.use_ second_level_cache", "true");
sessionFactory.HibernateProperties.Add("cache.regi on_prefix", this.pConfigurationHash); // a string

// for Indexus

//sessionFactory.HibernateProperties.Add("cache.prov ider_class", "NHibernate.Caches.SharedCache.SharedCacheProvider , NHibernate.Caches.SharedCache");
//sessionFactory.HibernateProperties.Add("cache.serv er", "127.0.0.1:48888");

// for MemCached

sessionFactory.HibernateProperties.Add("cache.prov ider_class", "NHibernate.Caches.MemCache.MemCacheProvider, NHibernate.Caches.MemCache");
sessionFactory.HibernateProperties.Add("cache.serv er", "127.0.0.1:11211"); // server address

sessionFactory.HibernateProperties.Add("expiration ", "3600");


... resources for NH mapping


sessionFactory.AfterPropertiesSet();




In NH mapping you have to explicitly enable 2nd level caching:


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="x.y.z, v.w" table="t">

<cache usage="read-write"/>

... properties

</class>

</hibernate-mapping>



And finally, you have to include needed assemblies.
For MemCached, for eg. these should be the requirements:

Memcached.ClientLibrary.dll
NHibernate.Caches.MemCache.dll
Commons.dll
ICSharpCode.SharpZipLib.dll
log4net.dll

Cheers
Reply With Quote
  #4  
Old 02-25-2009, 07:17 AM
superciccio superciccio is offline
Member
Spring User
 
Join Date: Apr 2008
Posts: 44
Default

Also have a look at this:

http://allegiance.chi-town.com/MemCacheDManager.aspx
Reply With Quote
  #5  
Old 03-19-2009, 07:57 AM
superciccio superciccio is offline
Member
Spring User
 
Join Date: Apr 2008
Posts: 44
Default

Any feedback?
I'm interested in other user's experiences with 2nd level cache.

egards
Reply With Quote
  #6  
Old 03-25-2009, 12:38 AM
Stingray Stingray is offline
Junior Member
New User
 
Join Date: Mar 2009
Posts: 10
Default

We use Spring with NH 1.2 and NCache.

Everything is configured in spring and NH config files.
If your app is in a farm be careful what caching concurrency strategy you use with your NH classes. The ReadWrite stategy will require your caching software to support distributed locking.

NH flattens the structure of cached classes and still delivers pretty good performance, but if your cache is out of proc, there will be deserialisation overhead each time you hit the cache. This caused us some issues initially

Cheers
Reply With Quote
  #7  
Old 04-15-2009, 06:56 AM
superciccio superciccio is offline
Member
Spring User
 
Join Date: Apr 2008
Posts: 44
Default

Stingray, could you please specify what does "This caused us some issues initially" mean?

Regards
Reply With Quote
  #8  
Old 04-22-2009, 12:32 PM
Stingray Stingray is offline
Junior Member
New User
 
Join Date: Mar 2009
Posts: 10
Default

Hi,

Load testing highlighted some issues regarding the deserialisation of objects that were stored in the cache. It significantly impacted the performance of the application because binary deserialisation is cpu intensive. We changed our code to reduce the size of the collection we were retrieving from the cache and the number of times we retrieved.

Original code for 1 aspx page execution
40 hits to the cache which retrieved a collection with 500 objects

Refactored code for 1 aspx page execution
1 hit to the cache to retrieve a collection with about 50 objects
We stored the collection in the HTTPContext, thereby reducing the cache hits to 1 and reduced the collection size by making it page specific.

I would do load testing as early as possible in your SDLC.

Cheers
Reply With Quote
  #9  
Old 06-18-2009, 05:53 PM
zvolkov zvolkov is offline
Junior Member
New User
 
Join Date: Jan 2009
Posts: 15
Default

Not sure which version of NHibernate.Caches superciccio is using, but in 2.0.0.GA that I'm using you can't specify cache.server as NHibernate parameter. It must be in App.Config like so:

<configuration>
<configSections>
<section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHa ndler, NHibernate.Caches.MemCache"/>
</configSections>

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

</configuration>
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:10 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.