1. First set ehcache and create the configuration file ehcache. xml. The default location is class-path, which can be placed in your src directory:
Code:
<? XML version = "1.0" encoding = "UTF-8"?> <Ehcache> <Diskstore Path = "Java. Io. tmpdir"/> <Defaultcache Maxelementsinmemory = "10000" <! -- Maximum number of caches --> Eternal = "false" <! -- Whether the cache is persistent --> Overflowtodisk = "true" <! -- Whether to save the data to the disk. When the system is running --> Timetoidleseconds = "300" <! -- Destroy the cache after being idle for n seconds --> Timetoliveseconds = "180" <! -- Destroy the cache after it has been alive for n seconds --> Diskpersistent = "false" Diskexpirythreadintervalseconds = "120"/> </Ehcache> |
2. Set in the hibernate configuration file:
Code:
<! -- Set the cache interface class of hibernate, which is included in the hibernate package --> <Property name = "cache. provider_class"> org. hibernate. cache. ehcacheprovider </property> <! -- Use query cache? --> <Property name = "hibernate. cache. use_query_cache"> true </property> If spring is used to call the sessionfactory of hibernate, set it as follows: <! -- Hibernatesession factory management --> <Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean"> <Property name = "datasource"> <Ref bean = "datasource"/> </Property> <Property name = "hibernateproperties"> <Props> <Prop key = "hibernate. dialect"> org. hibernate. dialect. oracle9dialect </prop> <Prop key = "connection. provider_class"> org. hibernate. Connection. c3p0connectionprovider </prop> <Prop key = "hibernate. show_ SQL"> true </prop> <Prop key = "hibernate. cache. use_query_cache"> true </prop> <Prop key = "hibernate. cache. provider_class"> org. hibernate. cache. ehcacheprovider </prop> </Props> </Property> <Property name = "mappingdirectorylocations"> <List> <Value>/WEB-INF/classes/CN/rmic/manager/hibernate/</value> </List> </Property> </Bean> |
Note: If you do not set "query cache", Hibernate will only cache a single Persistent object obtained using the load () method. If you want to cache it, use findall (), list (), iterator (), createcriteria (), createquery (), and other methods to obtain the data result set, you need to set
Hibernate. cache. use_query_cache is true.
3. Add <Cache Usage = "read-only"/> to the HBM file.
......
[Read the full text]