Http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html
Management NHibernate Level Two cache
The nhibernate level two cache is created by isessionfactory and maintained by Isessionfactory itself. When we use NHibernate to manipulate data, Isessionfactory can automatically synchronize the cache to ensure the validity of the cache. However, when we bulk manipulate the data, it is often nhibernate that the cache is not maintained for long. Isessionfactory provides a programmatic approach to caching management.
Isessionfactory provides a series of evictxxx () methods to easily remove an instance from the level two cache, delete a collection, a named cache, and more
? Evict (Persistentclass): Remove all instances of Persistentclass class from the level two cache
? Evict (Persistentclass, id): Removes the specified persisted instance from the level two cache
? Evictentity (entityname): Remove a named instance from the level two cache
? Evictcollection (roleName): Remove collection from level two cache
? Evictcollection (RoleName, id): Removes the specified collection from the level two cache
? Evictqueries (): Refreshes all query result sets from the level two cache
? Evictqueries (cacheregion): Refreshes the specified query result set from the level two cache
The ISession built-in cache can share the Isessionfactory cache by specifying the Cachemode of the ISession to control how ISession and isessionfactory interact. ISession can interact with isessionfactory in one of the following five ways:
? Ignore: Invalid level Two cache when updating data, other time not interacting with level two cache
? Put: Writes data to a level two cache, but does not read data from a level two cache
? Get: Read data from level two cache, write data to level two cache only when data is updated
? Normal: The default way. Read/write data from level two cache
? Refresh: Write data to Level two cache, want to not read data from level two cache, force level two cache refresh by reading data from the database when the configuration file Settings cache.use_minimal_puts
Test5: ManageNHibernateLevel Two cache
We can use Isessionfactory to provide a series of evictxxx () methods to remove an instance from the level two cache, to see this example, when the first read persisted instance, the result set is saved in the level two cache, and the evict method is used to remove all persisted instances from the two level cache. Query the same data for the second time, the level two cache does not exist and then re-queried from the database ~ ~
[Test] Public voidsessionfactorymanagetest () {isessionfactory _sessionfactory= (NewConfiguration ()). Configure (). Buildsessionfactory (); Console.WriteLine ("first read of persisted instances"); using(ISession _session =_sessionfactory.opensession ()) {Customer Customer1= _session. Get<customer> (1); Customer Customer2= _session. Get<customer> (2); } Console.WriteLine ("Remove all instances of the customer class from the level two cache"); _sessionfactory.evict (typeof(Customer)); //can also be _sessionfactory.evictentity ("DomainModel.Entities.Customer");Console.WriteLine ("second read of persisted instances"); using(ISession _session =_sessionfactory.opensession ()) {Customer Customer1= _session. Get<customer> (1); }}
Test 6: Forcing the cache zone to flush
We use the. Setcachemode (Cachemode.refresh) method provided by ISession to force refreshing the cache area, which avoids data inconsistency problems ~ ~
[Test] Public voidquerycachetest () {using(_session) {Console.WriteLine ("querying a data for the first time, explicitly caching query results"); IList<Customer> customers =_session. CreateQuery ("From Customer C where c.customerid > 2") . Setcacheable (true) . Setcacheregion ("Querycache") . List<Customer>(); Assert.AreEqual ( One, customers. Count); } resetsession (); using(_session) {Console.WriteLine ("querying a data for the second time, explicitly caching query results"); Console.WriteLine ("----Specify a specific named cache zone and force the cache area to be flushed----"); IList<Customer> customers =_session. CreateQuery ("From Customer C where c.customerid > 2") . Setcacheable (true) . Setcacheregion ("Querycache") . Setcachemode (Cachemode.refresh). List<Customer>(); Assert.AreEqual ( One, customers. Count); }}