2013年8月3日星期六

spring

 

spring cache

 

Spring 3.1 introduces exciting annotation-based (annotation) cache (cache) technology, which is essentially not a specific cache implementations (eg EHCache or OSCache), but an abstraction of the cache used by the Add a small amount of existing code that defines the various annotation, which can reach the cache method returns the object effect.

 

Spring caching technology also has considerable flexibility, not only can use SpEL (Spring Expression Language) to define the cache key and a variety of condition, also provides out of the box cache temporary storage solutions, but also to support and mainstream Professional cached instance EHCache integration.

 

Its characteristics are summarized as follows:

 
      
  • configuration by a small amount can make existing code annotation notes support cache
  •   
  • support out of the box Out-Of-The-Box, which do not have to install and deploy additional third-party components can use the cache
  •   
  • Support Spring Express Language, an object can be used to define any property or method cache key and condition
  •   
  • support AspectJ, and through its implementation of any method caching support
  •   
  • supports custom key and custom cache manager, with considerable flexibility and scalability
  •  
 

I mainly on spring and ehcache integrated approach (based maven project), as spring cache details, please refer to " annotation-driven spring cache cache description ", in order to better understand, it is recommended to see this before the first article of this examples do it again.

 

maven jar package ready to work

 

major need several

 

slf4j-log4j12 (log4j log)

 

ehcache-core (ehcache core package)

 

spring-webmvc (spring webmvc)

 

spring-context-support (spring context expansion pack)

 

testing requires

 

      junit   

 

      spring-test   

 These

good enough, do not add additional content. Of course maven project report will be automatically loaded rely in. With a jar package diagram

 

 

then attach pom file configuration:

 
  
        <dependency> 
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
 
 

configure spring

 

Spring-cache-anno.xml file:

 
  
<?xml version="1.0" encoding="UTF-8"?> 
<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:aop
="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee
="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util
="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tool
="http://www.springframework.org/schema/tool" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation
="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"

default-lazy-init
="true">
<bean id="accountServiceBean1" class="com.cacheOfAnno.AccountService" />
<!-- 对指定包中的所有类进行扫描,以完成bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.cacheOfAnno" />
<cache:annotation-driven cache-manager="cacheManager" />
<!-- cacheManager工厂类 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation
="classpath:ehcache.xml" />
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref
="cacheManagerFactory" />
</beans>
 
 

configuration ehcache

 
  
<?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" dynamicConfig="true">
<!-- <diskStore path="java.io.tmpdir" /> 系统临时文件目录 -->
<!--
配置自定义缓存
maxElementsInMemory:缓存中允许创建的最大对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk:内存不足时,是否启用磁盘缓存。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
-->
<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="accountCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
-->
<cache name="accountCache" maxElementsInMemory="0"
maxElementsOnDisk
="10000000" eternal="true" overflowToDisk="true"
diskSpoolBufferSizeMB
="50" />
</ehcache>
 
 

test the cache

 

test file Main.java

 
  
package com.cacheOfAnno; 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bean.Account;

public class Main {

/**
*
@param args
*/
public static void main(String[] args) {

ApplicationContext context
= new ClassPathXmlApplicationContext("spring-cache-anno.xml");// 加载 spring 配置文件

AccountService s
= (AccountService) context.getBean("accountServiceBean1");
// 第一次查询,应该走数据库
System.out.print("first query...");
s.getAccountByName(
"som");
// 第二次查询,应该不查数据库,直接返回缓存的值
System.out.print("second query...");
s.getAccountByName(
"som");
System.out.println();

System.out.println(
"start testing clear cache...");
// 更新某个记录的缓存,首先构造两个账号记录,然后记录到缓存中
Account account1 = s.getAccountByName("somebody1");
Account account2
= s.getAccountByName("somebody2");
// 开始更新其中一个
account1.setId(1212);
s.updateAccount(account1);
s.getAccountByName(
"somebody1");// 因为被更新了,所以会查询数据库
s.getAccountByName("somebody2");// 没有更新过,应该走缓存
s.getAccountByName("somebody1");// 再次查询,应该走缓存
// 更新所有缓存
System.out.println();
s.reload();
s.getAccountByName(
"somebody1");// 应该会查询数据库
s.getAccountByName("somebody2");// 应该会查询数据库
s.getAccountByName("somebody1");// 应该走缓存
s.getAccountByName("somebody2");// 应该走缓存
}

}
  
 
 
 

test results:

 
  
first query...real querying db...som 
second query
...
start testing clear cache
...
real querying db
...somebody1
real querying db
...somebody2
real update db
...somebody1
real querying db
...somebody1
real querying db
...somebody2
real querying db
...somebody1

real querying db
...somebody1
real querying db
...somebody2
real querying db
...somebody1
real querying db
...somebody2
 
 

attached to the project file structure (specific examples) can be understood finish spring cache to be modified. Here is not to provide source code of the project.

 

没有评论:

发表评论