paulwong

Spring基于注解的缓存配置--web应用实例

基于方法级别的缓存:通常如果一个带有参数的方法执行后会返回一个Object,那可以认为如果参数相同,则返回的结果一样,这样就可以为这个方法建立一个缓存Map,参数就是KEY,返回的结果就是VALUE,调用此方法前可以先判断参数是否和之前的一样,如果是则从Map中取出结果返回调用者,而无需再进入此方法内执行得到,从而节省了此方法执行时的时间。

http://hanqunfeng.iteye.com/blog/605123

经验证,如果要支持分布式的缓存,用spring-modules-cache不好使,必须转用com.googlecode.ehcache-spring-annotations

全套MAVEN的POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
<modelVersion>4.0.0</modelVersion>
    
<groupId>EhCache-Cluster-Tester</groupId>
    
<artifactId>EhCache-Cluster-Tester</artifactId>
    
<version>0.0.1-SNAPSHOT</version>
    
<packaging>war</packaging>
    
<dependencies>
        
<dependency>
            
<groupId>net.sf.ehcache</groupId>
            
<artifactId>ehcache-jgroupsreplication</artifactId>
            
<version>1.4</version>
        
</dependency>
        
<!-- Spring framework -->
        
<dependency>
            
<groupId>org.springframework</groupId>
            
<artifactId>spring</artifactId>
            
<version>${spring.version}</version>
        
</dependency>

        
<!-- Spring MVC framework -->
        
<dependency>
            
<groupId>org.springframework</groupId>
            
<artifactId>spring-webmvc</artifactId>
            
<version>${spring.version}</version>
        
</dependency>
        
<dependency>
            
<groupId>javax.servlet</groupId>
            
<artifactId>servlet-api</artifactId>
            
<version>2.4</version>
            
<scope>provided</scope>
        
</dependency>
        
<dependency>
            
<groupId>org.slf4j</groupId>
            
<artifactId>slf4j-jdk14</artifactId>
            
<version>1.5.6</version>
            
<type>jar</type>
            
<scope>compile</scope>
        
</dependency>
        
<dependency>
            
<groupId>log4j</groupId>
            
<artifactId>log4j</artifactId>
            
<version>1.2.13</version>
            
<type>jar</type>
            
<scope>compile</scope>
        
</dependency>
        
<dependency>
            
<groupId>cglib</groupId>
            
<artifactId>cglib-nodep</artifactId>
            
<version>2.2.2</version>
            
<type>jar</type>
            
<scope>compile</scope>
        
</dependency>
        
<!-- 
        <dependency>
            <groupId>org.springmodules</groupId>
            <artifactId>spring-modules-cache</artifactId>
            <version>0.9</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        
-->
        
<dependency>
            
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
            
<artifactId>ehcache-spring-annotations</artifactId>
            
<version>1.2.0</version>
            
<type>jar</type>
            
<scope>compile</scope>
            
<exclusions>
                
<exclusion>
                    
<artifactId>spring-aop</artifactId>
                    
<groupId>org.springframework</groupId>
                
</exclusion>
                
<exclusion>
                    
<artifactId>spring-expression</artifactId>
                    
<groupId>org.springframework</groupId>
                
</exclusion>
            
</exclusions>
        
</dependency>
    
</dependencies>
    
<build>
        
<finalName>cluster-test</finalName>
        
<plugins>
            
<plugin>
                
<groupId>org.apache.maven.plugins</groupId>
                
<artifactId>maven-compiler-plugin</artifactId>
                
<configuration>
                    
<source>1.6</source>
                    
<target>1.6</target>
                    
<encoding>UTF-8</encoding>
                
</configuration>
            
</plugin>
            
<plugin>
                
<groupId>org.mortbay.jetty</groupId>
                
<artifactId>jetty-maven-plugin</artifactId>
                
<version>8.0.4.v20111024</version>
                
<!-- 
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                
-->
                
<configuration>
                    
<contextPath>/cluster-test</contextPath>
                    
<scanIntervalSeconds>3</scanIntervalSeconds>
                    
<scanTargetPatterns>
                        
<scanTargetPattern>
                            
<directory>src/main/webapp/WEB-INF</directory>
                            
<excludes>
                                
<exclude>**/*.jsp</exclude>
                            
</excludes>
                            
<includes>
                                
<include>**/*.properties</include>
                                
<include>**/*.xml</include>
                            
</includes>
                        
</scanTargetPattern>
                    
</scanTargetPatterns>
                
</configuration>
            
</plugin>
        
</plugins>
    
</build>
    
<properties>
        
<spring.version>2.5.2</spring.version>
    
</properties>
</project>

posted on 2012-02-16 17:04 paulwong 阅读(2841) 评论(0)  编辑  收藏 所属分类: 缓存


只有注册用户登录后才能发表评论。


网站导航: