The cache function in spring3.1.m1 is briefly introduced below.
The cache module in spring3.1.m1 is org. springframework. context-3.1.0.M1.jar
Similar to the modules module of 2.5, the annotation cache of 3.1 also declares the Annotation on the method, and 3.1 also provides two Annotations:
@ Cacheable: adds the return value of the method to the cache.
@ Cacheevict: responsible for clearing the cache
@ CacheableThe following parameters are supported:
Value: the cache location name. It cannot be blank. If ehcache is used, it is the name of the cache declared in ehcache. xml.
Key: the cached key. It is null by default. It indicates that the parameter type and value of the method are used as the key. spel is supported.
Condition: the trigger condition. It is added to the cache only when the condition is met. The default value is null, indicating that all the conditions are added to the cache. spel is supported.
For example:
Java code
- // Save the cache to andcache, and add a string (the method name is used here) to the userid In the parameter as the cache key
- @ Cacheable (value = "andcache", key = "# userid + 'findbyid '")
- Public systemuser findbyid (string userid ){
- Systemuser user = (systemuser) Dao. findbyid (systemuser. Class, userid );
- Return user;
- }
- // Save the cache to andcache. The parameter value and type are used as the cache key by default when the length of the parameter userid is smaller than 32.
- @ Cacheable (value = "andcache", condition = "# userid. Length <32 ")
- Public Boolean isreserved (string userid ){
- System. Out. println ("Hello andcache" + userid );
- Return false;
- }
@ CacheevictThe following parameters are supported:
Value: the name of the cache location. It cannot be empty. Same as above.
Key: the cached key. The default value is null.
Condition: trigger condition. The cache is cleared only when the condition is met. The default value is null. spel is supported.
Allentries: True indicates to clear all caches in value. The default value is false.
For example:
Java code
- // Clear the cache of the specified key
- @ Cacheevict (value = "andcache", key = "# user. userid + 'findbyid '")
- Public void modifyuserrole (systemuser user ){
- System. Out. println ("Hello andcache Delete" + User. getuserid ());
- }
- // Clear all caches
- @ Cacheevict (value = "andcache", allentries = true)
- Public final void setreservedusers (string [] reservedusers ){
- System. Out. println ("Hello andcache deleteall ");
- }
In general, our update operation only needs to refresh a value in the cache. Therefore, it is very important to define the cached key value. It is best to be unique, this can accurately clear specific caches without affecting other cache values.,
For example, for user operations, you can set the key value using the (userid + method name) method.,Of course, you can also find a more suitable way to set it.
Spel: Spring Expression Language
For details about spel, refer to the following address:
Http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/expressions.html
After learning about the cache annotation, let's talk about how to make the annotation take effect. In fact, we need to add some configuration in the spring configuration file.
1. Spring-Cache
First, let's take a look at how to use spring3.1's own cache,
You need to add the cache configuration in the namespace.
XML Code
- <Beans xmlns = "http://www.springframework.org/schema/beans"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: P = "http://www.springframework.org/schema/p"
- Xmlns: cache = "http://www.springframework.org/schema/cache"
- Xsi: schemalocation ="
- Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- Http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
Then add the following statement:
XML Code
- <! -- Enable the cache annotation function. This is required; otherwise, the annotation will not take effect. In addition, the annotation must be declared to take effect in the spring main configuration file. -->
- <Cache: annotation-driven cache-Manager = "cachemanager"/>
- <! -- Change manager of spring. Two cache location names are defined here, which are values in annotations -->
- <Bean id = "cachemanager" class = "org. springframework. cache. Support. simplecachemanager">
- <Property name = "caches">
- <Set>
- <Bean
- Class = "org. springframework. cache. Concurrent. concurrentcachefactorybean"
- P: Name = "default"/>
- <Bean
- Class = "org. springframework. cache. Concurrent. concurrentcachefactorybean"
- P: Name = "andcache"/>
- </Set>
- </Property>
- </Bean>
2. Spring-ehcache
Next, let's talk about the ehcache support. In fact, you only need to replace cachemanager with the cachemanager of ehcache, as shown below:
XML Code
- <! -- Enable the cache annotation function. This is required; otherwise, the annotation will not take effect. In addition, the annotation must be declared to take effect in the spring main configuration file. -->
- <Cache: annotation-driven cache-Manager = "cachemanager"/>
- <! -- Cachemanager factory class, specifying the location of ehcache. xml -->
- <Bean id = "cachemanagerfactory" class = "org. springframework. cache. ehcache. ehcachemanagerfactorybean"
- P: configlocation = "classpath:/config/ehcache. xml"/>
- <! -- Declare cachemanager -->
- <Bean id = "cachemanager" class = "org. springframework. cache. ehcache. ehcachecachemanager"
- P: cachemanager-ref = "cachemanagerfactory"/>
Ehcache. xml
XML Code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: nonamespaceschemalocation = "ehcache. XSD" updatecheck = "true"
- Monitoring = "autodetect">
- <! --
- <Diskstore Path = "Java. Io. tmpdir"/> -->
- <Diskstore Path = "E:/cachetmpdir"/>
- <Defaultcache maxelementsinmemory = "10000" Eternal = "false"
- Timetoidleseconds = "120" timetoliveseconds = "120" overflowtodisk = "true"
- Maxelementsondisk = "10000000" diskpersistent = "false"
- Diskexpirythreadintervalseconds = "120" memorystoreevictionpolicy = "LRU"/>
- <Cache name = "andcache" maxelementsinmemory = "10000"
- Maxelementsondisk = "1000" Eternal = "false" overflowtodisk = "true"
- Diskspoolbuffersizemb = "20" timetoidleseconds = "300" timetoliveseconds = "600"
- Memorystoreevictionpolicy = "LFU"/>
- </Ehcache>
OK, so that the annotation cache will take effect.
The attachment contains a small example written by myself. The project structure is as follows: Run demotest under com. piaoyi. function. demo.
References:
Http://blog.springsource.com/2011/02/23/spring-3-1-m1-caching/
Http://hi.baidu.com/coolcooldool/blog/item/3b541533c72b40e21a4cffda.html