nHibernate Caching


Published on

Published in: Technology
  • Nice and much briefed presentation about NHibernate. Juts need to mention that there is another cache provider called NCache which offers plug in for NHibernate and it is doing pretty good job for me.

    Are you sure you want to  Yes  No
    Your message goes here

nHibernate Caching

  1. 1. nHibernate Caching<br />Albert Kuo<br />1<br />
  2. 2. Database Transaction<br />nHibernate Caching<br />nHibernate Caching Implementation<br />Reference<br />2<br />Agenda<br />
  3. 3. Database Transaction<br />3<br />
  4. 4. Understanding database transactions<br />4<br />
  5. 5. NHibernateITransaction API<br />5<br />
  6. 6. NHibernateITransaction API<br />6<br />
  7. 7. nHibernate Caching<br />7<br />
  8. 8. Used for optimizing database applications.<br />A cache is designed to reduce traffic between your application and the database by conserving data already loaded from the database.<br />Database access is necessary only when retrieving data that is not currently available in the cache.<br />The application may need to empty (invalidate) the cache from time to time if the database is updated or modified in some way, because it has no way of knowing whether the cache is up to date<br />What is caching?<br />8<br />
  9. 9. nHibernate Cache<br />Persistence context cache<br />Pluggable, scope is process or cluster<br />9<br />
  10. 10. Two different Caches<br />First-level cache<br />Second-level cache<br />associated with the Session object<br />Hibernate uses first-level cache on a per transaction basis (within a single transaction boundary)<br />Used mainly to reduce the number of SQL queries it needs to generate within a given transaction.<br />For example, if an object is modified several times within the same transaction, Hibernate will generate only one SQL UPDATE statement at the end of the transaction, containing all the modifications<br />associated with the SessionFactory object<br />Second-level cache keeps loaded objects at the Session Factory level across transaction boundaries<br />The objects in the second-level cache are available to the whole application, not just to the user running the query<br />This way, each time a query returns an object that is already loaded in the cache, one or more database transactions potentially are avoided.<br />10<br />
  11. 11. Use it when you need to cache actual query results, rather than just persistent objects<br />Query-level Cache<br />11<br />
  12. 12. nHibernate Caching Implementation<br />12<br />
  13. 13. http://nhforge.org/media/p/6.aspx<br />Where to download<br />13<br />
  14. 14. How to configure?<br />You have 3 DLLs need to be added into reference<br />14<br />
  15. 15. How to configure ? – cont.<br />Add two property into &lt;session-factory&gt;<br />&lt;!-- nHibernate Cache configuration --&gt;<br />&lt;property name=&quot;cache.provider_class&quot;&gt;NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence&lt;/property&gt;<br />&lt;property name=&quot;cache.use_query_cache&quot;&gt;true&lt;/property&gt;<br />&lt;property name=&quot;cache.use_second_level_cache&quot;&gt;true&lt;/property&gt;<br />&lt;property name=&quot;cache.default_expiration&quot;&gt;120&lt;/property&gt;<br />15<br />
  16. 16. How to configure ? – cont.<br />16<br />
  17. 17. How to configure ? – cont.<br />Configure caching strategy for each mapping file<br />17<br />
  18. 18. read only<br />If your application needs to read but never modify instances of a persistent class, a read-only cache may be used. This is the simplest and best performing strategy.<br />read/write<br />If the application needs to update data, a read-write cache might be appropriate. This cache strategy should never be used if serializable transaction isolation level is required.<br />nonstrict read/write<br />If the application only occasionally needs to update data and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate.<br />Cache StrategieCache Strategies (for Second-Level)<br />18<br />
  19. 19. 19<br />Run Search Function<br />
  20. 20. 2009-10-01 16:36:49,250 INFO NHibernate.Cfg.SettingsFactory.CreateCacheProvider(:0) - cache provider: NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence<br />2009-10-01 16:36:49,250 INFO NHibernate.Cfg.SettingsFactory.BuildSettings(:0) - query cache factory: NHibernate.Cache.StandardQueryCacheFactory<br />2009-10-01 16:36:49,578 DEBUG NHibernate.Cache.CacheFactory.CreateCache(:0) - cache for: HelloNHibernate.vo.Contact usage strategy: read-write<br />2009-10-01 16:36:49,750 DEBUG NHibernate.Cache.CacheFactory.CreateCache(:0) - cache for: HelloNHibernate.vo.People usage strategy: read-write<br />2009-10-01 16:36:50,125 DEBUG NHibernate.Loader.Loader.InitializeEntitiesAndCollections(:0) - total objects hydrated: 3<br />2009-10-01 16:36:50,125 DEBUG NHibernate.Engine.TwoPhaseLoad.InitializeEntity(:0) - resolving associations for [HelloNHibernate.vo.People#3]<br />2009-10-01 16:36:50,140 DEBUG NHibernate.Engine.Loading.LoadContexts.LocateLoadingCollection(:0) - creating collection wrapper:[HelloNHibernate.vo.People.Contacts#3]<br />2009-10-01 16:36:50,140 DEBUG NHibernate.Engine.TwoPhaseLoad.InitializeEntity(:0) - adding entity to second-level cache: [HelloNHibernate.vo.People#3]<br />2009-10-01 16:36:50,140 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Caching: HelloNHibernate.vo.People#3<br />2009-10-01 16:36:50,140 DEBUG NHibernate.Caches.Prevalence.PrevalenceCache.Get(:0) - Fetching object &apos;NHibernate-Cache:HelloNHibernate.vo.People:HelloNHibernate.vo.People#3@3&apos; from the cache.<br />2009-10-01 16:36:50,156 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Item was already cached: HelloNHibernate.vo.People#3<br />20<br />Check log<br />Set cache provider<br />Cache strategy<br />Fetch data from cache<br />
  21. 21. nHibernate in Action, Manning, 2009/2<br />http://www.manning.com/kuate/<br />nHibernate online doc: Chapter 14. Improving performance<br />https://www.hibernate.org/hib_docs/nhibernate/html/performance.html<br />nHibernate online doc: Chapter 20. NHibernate.Caches<br />https://www.hibernate.org/hib_docs/nhibernate/html/caches.html<br />NHibernate考察系列 06 进阶篇<br />http://www.cnblogs.com/riccc/archive/2007/04/17/nhibernate-entity-lifecycle-secondary-cache-interceptor.html<br />NHibernate之旅(22):探索NHibernate一级缓存<br />http://www.cnblogs.com/lyj/archive/2008/11/24/1340253.html<br />21<br />Reference<br />