[Nhib.pdf] second-level cache (1)
For details about the second-level cache, refer to [nhib.pdf] cache (nhib.pdf. Caches ). The nhib1_session has an internal (first-level) cache that stores its entities. These caches are not shared, so when the session is destroyed, its cache is also destroyed. Nhib.pdf provides a second-level cache system that works at the SessionFactory level. Therefore, it is shared by sessions generated by the same SessionFactory. In NHibernate, when we enable the NHibernate level-2 cache. When ISession is used for data operations, nhibsion first looks for the required data from the built-in cache (level-1 cache). If the built-in cache does not have the required data, It queries the level-2 cache, if required data exists in the second-level cache, the cached data is used directly. Otherwise, the data is queried from the database and put into the cache. Nhibableitself provides a HashtableCache Based on Hashtable, but it has very limited functions and poor performance and is not suitable for large-scale applications, we can use a third-party cache provider as the NHibernate level-2 cache. Disadvantages of Using Cache: If the cache policy is improperly set, nhib.pdf does not know how other applications modify the database and update the cache in a timely manner. Therefore, it is recommended that you only cache read-only data that is frequently used by the system, has a small amount of data, and is not modified by other applications. The Nhibernate second-level Cache provider NHibernate provides the NHibernate. Cache. ICacheProvider interface to support third-party Cache providers. When developing a cache provider, You need to implement this interface as a direct adapter for nhib.pdf and cache implementation. Nhibprovider provides built-in adapters for common Cache providers, all of which implement the NHibernate. Cache. ICacheProvider interface. Nhib.pdf. cache. ICacheProvider is defined as follows: Copy code 1 namespace nhib.pdf. cache 2 {3 // Summary: 4 // Support for pluggable caches 5 public interface ICacheProvider 6 {7 // Summary: 8 // Configure the cache 9 // 10 // parameters: 11 // regionName: 12 // the name of the cache region13 // 14 // properties: 15 // configuration settings16 ICache BuildCache (string regionName, IDictionary <string, string> properties); 17 // 18 // Abstract: 19 // generate a timestamp20 long NextTimestamp (); 21 // 22 // Abstract: 23 // Callback to perform any necessary initialization of the underlying cache24 // implementation during ISessionFactory construction.25 // 26 // parameter: 27 // properties: 28 // current configuration settings29 void Start (IDictionary <string, string> properties); 30 // 31 // Summary: 32 // Callback to perform any necessary cleanup of the underlyi Ng cache implementation33 // during NHibernate. ISessionFactory. Close (). 34 void Stop (); 35} 36} copy the code in addition to a Hashtable-based HashtableCache provided by NHibernate itself. The NHibernate Contrib provides six third-party NHibernate second-level cache providers, which are fully open-source. We can directly download its assembly and reference it to our project. How to use the example nhib.pdf. Caches. MemCacheNHibernate. Caches. PrevalenceNHibernate. Caches. SharedCacheNHibernate. Caches. SysCacheNHibernate. Caches. syscache2nhib.pdf. Caches. Velocity? By default, NHibernate does not enable level-2 caching. To use the second-level cache, you must explicitly enable the second-level cache in the NHibernate configuration file. You can configure application-level or distributed cache for each specific class and set for the NHibernate second-level cache. Cache concurrency policies when two independent transactions access the database at the same time, may cause loss of updates, non-repeated read and other concurrency issues. Similarly, when two concurrent transactions access the cache at the same time, various concurrency problems may also occur. Therefore, you also need to set the corresponding concurrent access policy at the cache level. Nhib.pdf has four built-in concurrent access policies: read-only cache. Applicable to read-only data. It can be used in a group set. Read-write: read/write cache. Nonstrict-read-write: Non-strict read/write cache. The consistency between the cache and the database is not guaranteed. Transactional: the transaction cache. Provides the transaction isolation level for Repeatable reads. In the NHibernate configuration file, the cache. provider_class attribute explicitly specifies the cache implementation. The attribute value is the specific class name of the cache adapter. If you use the above third-party cache provider, you also need to configure the cache provider itself. For details about the third-party cache provider configuration, refer to [NHibernate] cache (NHibernate. Caches ). The first step is to start with the HashtableCache cache provided by Nhibernate and learn how to configure the cache. The specified cache provider Class 1 <property name = "Cache. provider_class"> nhibernate. cache. HashtableCacheProvider </property> 1 <! -- Enable Level 2 cache and use cache. use_second_level_cache attribute explicitly enables the second-level cache. The parameter value is the Bool value. Here, the enable setting is true --> 2 <property name = "cache. use_second_level_cache "> true </property> if a third-party cache provider is used, you need to configure the third-party cache provider itself. You need to configure the third-party cache provider cache attributes in detail: the storage time, expiration time, and number of objects that can be cached. Specify a cache policy for each persistence class and set. Method 1: configure the cache Policy for the class and set through the <cache> element in the ing file, add the <cache> element to the Class element or set element for configuration. Note: The <cache> element must be before the <id> element. 1 <cache usage = "read-only | read-write | nonstrict-read-write" region = "Default class or set name"/> Method 2: configure the file hibernate in nhib.pdf. cfg. configure cache policies for classes and collections by using the <class-cache> element and <collection-cache> element in xml. 1 <class-cache class = "class Name" region = "Default class name" include = "all | non-lazy" 2 usage = "read-only | read-write | nonstrict -read-write | transactional "/> <! -- Specify a collection --> <collection-cache collection = "collection name" region = "Default collection name" usage = "read-only | read-write | nonstrict-read-write | transactional "/> attribute meaning region: optional. The default value is the class or set name. It is used to specify the region name of the second-level cache, which corresponds to a named cache region of the cache implementation. Include: Optional. The default value is all. When non-lazy is used, the attributes of the persistent instance with delayed loading are not cached. Usage: Declares the cache synchronization policy, which is described above. There are two ways to define a cache policy. Which one is better? In the nhibernate configuration file, you can set more convenient and easy to maintain for each class. If you set cache policies for each persistent class separately, it will be a little difficult to maintain. Test enabling Level 2 caching in nhib.pdf and specifying the cache Customer class copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2