2013年8月4日星期日

java spring using annotations to implement caching

 

example here using spring3.1.4 + ehcache

 

use of annotations added cache is spring3.1

 

use:

 

1.ehcache dependency + spring dependency

 
  
<!-- ehcache依赖--> 
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.7.2</version>
</dependency>
 
 
  
        <dependency> 
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<!-- 这里有注解类需要的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
  


cglib
< span> cglib
2.2.2
< / dependency>

  
 
 
 

2.spring + ehcache simple configuration

 

Ehcache basic configuration, meaning specific parameters see: http://www.ehcache.org/documentation/configuration/configuration

                                                                                                                                                                                             
name

cache unique identifier

maxElementsInMemory

create maximum number of

eternal

whether the cache is permanent, default false

timeToLiveSeconds

cache survival time

timeToIdleSeconds

long without access to clear the cache

overflowToDisk

memory is written to disk, the default False

maxElementsOnDisk

persisted to disk maximum number

memoryStoreEvictionPolicy

the cache is full, clear cache rules,

comes with three kinds: FIFO (First In First Out), LFU (least used), LRU (least recently used)

diskSpoolBufferSizeMB

disk cache buffer size, the default 30M

diskExpiryThreadIntervalSeconds

disk failure thread time interval

 

2.1 ehcache.xml first create a configuration file

 
  
<ehcache> 
<defaultCache
maxElementsInMemory="10000"
eternal
="false"
timeToIdleSeconds
="120"
timeToLiveSeconds
="120"
overflowToDisk
="false"
maxElementsOnDisk
="10000000"
diskPersistent
="false"
diskExpiryThreadIntervalSeconds
="120"
memoryStoreEvictionPolicy
="LRU"
/>

<cache
name="onecache"
maxElementsInMemory
="10000"
eternal
="false"
overflowToDisk
="false"
timeToIdleSeconds
="10"
timeToLiveSeconds
="10"/>
</ehcache>
 
 

 

 

2.2 in the spring of apllication.xml join injected cache

 
  
    <cache:annotation-driven cache-manager="cacheManager"/> 

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref
="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation
="classpath:ehcache.xml"
p:shared
="true"/>
 
 

here also need to head into the configuration file

 
  
 xmlns:cache="http://www.springframework.org/schema/cache" 
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
...
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
"
 
 

to add the mvc-servlet.xml

 
  
  <mvc:annotation-driven/>  <!-- 挺重要的,不加注解不会被扫描-->
 
 

3. annotation using

 

3.1 for object class

 
  
@Cacheable(value = "onecache")
class A1{}
 
 

This class method return values ​​are cached,

 

3.2 for method method

 
  
 @Cacheable(value = "onecache", key = "#name",condition = "#age < 25") 
public xx findEmployeeBySurname(String firstName, String name, int age) {
return xxx
}
 
 

This will be the value of age is less than 25, according to name the key cache

 

3.3 Clear

 
  
@CacheEvict(value="onecache",key="#name + 'haha'") 
public void delete(String name) {
System.out.println(
"delete one name");
}
 
 You can also use the following

Clear All

 
  
@CacheEvict(value="oneCache",allEntries=true)
 
 

4. code calls

 
  
  @Autowired 
private CacheManager cacheManager;

//获取当前时间
public String getABCCache() {
cacheManager.getCache("ccc").put("hello", new Date().toString());
return (String) cacheManager.getCache("ccc").get("hello").get();
}
 
 

 

 

 

1 条评论: