Upload
guo-albert
View
5.909
Download
8
Tags:
Embed Size (px)
Citation preview
1
nHibernate CachingAlbert Kuo
2
Database Transaction nHibernate Caching nHibernate Caching Implementation Reference
Agenda
3
Database Transaction
4
Understanding database transactions
5
NHibernate ITransaction API
6
NHibernate ITransaction API
7
nHibernate Caching
8
Used for optimizing database applications. A cache is designed to reduce traffic between your
application and the database by conserving data already loaded from the database.◦Database access is necessary only when retrieving data
that is not currently available in the cache.◦ 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
What is caching?
9
nHibernate CachePersistence context cache
Pluggable, scope is process or cluster
10
Two different Caches
First-level cache Second-level cache
associated with the Session object
Hibernate uses first-level cache on a per transaction basis (within a single transaction boundary)◦ Used mainly to reduce the number
of SQL queries it needs to generate within a given transaction.
◦ 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
associated with the SessionFactory object
Second-level cache keeps loaded objects at the Session Factory level across transaction boundaries
The objects in the second-level cache are available to the whole application, not just to the user running the query◦ This way, each time a query
returns an object that is already loaded in the cache, one or more database transactions potentially are avoided.
11
Use it when you need to cache actual query results, rather than just persistent objects
Query-level Cache
12
nHibernate Caching Implementation
14
• NHibernate.Caches.Prevalence makes it possible to use the underlying Bamboo.Prevalence implementation as cache provider.
Prevalence
• NHibernate.Caches.SysCache makes it possible to use the underlying System.Web.Caching.Cache implementation as cache provider.
• This means that you can rely on ASP.NET caching feature to understand how it worksSysCache
Cache ProvidersYou can choose one of provider to use
15
How to configure?
Prevalence SysCache
16
How to configure?
Prevalence SysCache
Add four properties into <session-factory> <!-- nHibernate Cache configuration --> <property
name="cache.provider_class">NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.default_expiration">120</property>
Add four properties into <session-factory>
<!-- nHibernate Cache configuration --> <property
name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.default_expiration">120</property>
17
How to configure ? – cont.
18
How to configure ? – cont.
19
How to configure ? – cont.
Configure caching strategy for each mapping file
20
read only◦ 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.
read/write◦ 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.
nonstrict read/write◦ If the application only occasionally needs to update data
and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate.
Cache StrategieCache Strategies (for Second-Level)
21
Run Search Function
22
2009-10-01 16:36:49,250 INFO NHibernate.Cfg.SettingsFactory.CreateCacheProvider(:0) - cache provider: NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence
2009-10-01 16:36:49,250 INFO NHibernate.Cfg.SettingsFactory.BuildSettings(:0) - query cache factory: NHibernate.Cache.StandardQueryCacheFactory
2009-10-01 16:36:49,578 DEBUG NHibernate.Cache.CacheFactory.CreateCache(:0) - cache for: HelloNHibernate.vo.Contact usage strategy: read-write
2009-10-01 16:36:49,750 DEBUG NHibernate.Cache.CacheFactory.CreateCache(:0) - cache for: HelloNHibernate.vo.People usage strategy: read-write
2009-10-01 16:36:50,125 DEBUG NHibernate.Loader.Loader.InitializeEntitiesAndCollections(:0) - total objects hydrated: 3 2009-10-01 16:36:50,125 DEBUG NHibernate.Engine.TwoPhaseLoad.InitializeEntity(:0) - resolving associations for
[HelloNHibernate.vo.People#3] 2009-10-01 16:36:50,140 DEBUG NHibernate.Engine.Loading.LoadContexts.LocateLoadingCollection(:0) - creating collection wrapper:
[HelloNHibernate.vo.People.Contacts#3] 2009-10-01 16:36:50,140 DEBUG NHibernate.Engine.TwoPhaseLoad.InitializeEntity(:0) - adding entity to second-level cache:
[HelloNHibernate.vo.People#3] 2009-10-01 16:36:50,140 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Caching: HelloNHibernate.vo.People#3 2009-10-01 16:36:50,140 DEBUG NHibernate.Caches.Prevalence.PrevalenceCache.Get(:0) - Fetching object 'NHibernate-
Cache:HelloNHibernate.vo.People:HelloNHibernate.vo.People#3@3' from the cache. 2009-10-01 16:36:50,156 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Item was already cached: HelloNHibernate.vo.People#3
Check log – Prevalence Set cache provider
Cache strategy
Fetch data from cache
23
2009-10-19 13:21:27,046 DEBUG NHibernate.Cache.ReadWriteCache.Get(:0) - Cache lookup: Persistence.vo.CaseState#0
2009-10-19 13:21:27,046 DEBUG NHibernate.Caches.SysCache.SysCache.Get(:0) - Fetching object 'NHibernate-Cache:Persistence.vo.CaseState:Persistence.vo.CaseState#0@0' from the cache.
2009-10-19 13:21:27,046 DEBUG NHibernate.Cache.ReadWriteCache.Get(:0) - Cache hit: Persistence.vo.CaseState#0
2009-10-19 13:21:27,046 DEBUG NHibernate.Event.Default.DefaultLoadEventListener.AssembleCacheEntry(:0) - assembling entity from second-level cache: [Persistence.vo.CaseState#0]
2009-10-19 13:21:27,046 DEBUG NHibernate.Event.Default.DefaultLoadEventListener.AssembleCacheEntry(:0) - Cached Version:
2009-10-19 13:21:27,046 DEBUG NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(:0) - resolved object in second-level cache: [Persistence.vo.CaseState#0]
2009-10-19 13:21:27,046 DEBUG NHibernate.Engine.TwoPhaseLoad.InitializeEntity(:0) - adding entity to second-level cache: [Persistence.vo.Cases#200910090114]
2009-10-19 13:21:27,046 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Caching: Persistence.vo.Cases#200910090114
2009-10-19 13:21:27,046 DEBUG NHibernate.Caches.SysCache.SysCache.Get(:0) - Fetching object 'NHibernate-Cache:Persistence.vo.Cases:Persistence.vo.Cases#200910090114@-909291618' from the cache.
2009-10-19 13:21:27,046 DEBUG NHibernate.Cache.ReadWriteCache.Put(:0) - Item was already cached: Persistence.vo.Cases#200910090114
Check log – SysCache
24
nHibernate in Action, Manning, 2009/2◦ http://www.manning.com/kuate/
nHibernate online doc: Chapter 14. Improving performance◦ https://www.hibernate.org/hib_docs/nhibernate/html/performance.h
tml
nHibernate online doc: Chapter 20. NHibernate.Caches◦ https://www.hibernate.org/hib_docs/nhibernate/html/caches.html
NHibernate 考察系列 06 进阶篇◦ http://www.cnblogs.com/riccc/archive/2007/04/17/nhibernate-entity-
lifecycle-secondary-cache-interceptor.html
NHibernate 之旅 (22) :探索 NHibernate 一级缓存◦ http://www.cnblogs.com/lyj/archive/2008/11/24/1340253.html
Reference