Ehcache的简单学习2-Ehcache和springmvc的配合使用

一、Maven引入

  	<!-- 私库的地址 begin -->
  	<repositories>
	     <repository>
	         <id>nexus.center</id>
	         <name>local private nexus</name>
	         <url>http://search.maven.org</url>
	         <releases>
	             <enabled>true</enabled>
	         </releases>
	         <snapshots>
	             <enabled>false</enabled>
	         </snapshots>
	     </repository>
	</repositories>
  	<!-- 私库的地址 end -->

  			<dependency>
			    <groupId>net.sf.ehcache</groupId>
			    <artifactId>ehcache-core</artifactId>
			    <version>2.6.11</version>
			</dependency>

二、配置文件

1.applicationContext.xml

注意:要引入cache的命名空间!

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop.xsd 
                        http://www.springframework.org/schema/cache 
                        http://www.springframework.org/schema/cache/spring-cache.xsd">

	<!-- Scans for @Repository, @Service and @Component -->
	<!-- 注意:此处不排除controller的注解,会导致controller会初始化两次! -->
	<context:component-scan base-package="com.book.*" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	 <!-- ehcache 开始 -->
	  <!-- 支持缓存注解 -->
	  <cache:annotation-driven cache-manager="cacheManager" />
	 
	  <!-- 默认是cacheManager -->
	  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
	    <property name="cacheManager" ref="cacheManagerFactory"/>
	  </bean>
	 
	  <!-- ehcache管理器配置 -->
	  <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
	    <property name="configLocation" value="classpath:ehcache.xml"/>
	  </bean>
	  <!-- ehcache 结束 -->
	
	<!-- 加载正式环境 -->
    <import resource="classpath*:applicationContext-db.xml" />
	<!-- 加载配置文件 -->
	<bean id="propertyConfigurerForProject"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>classpath*:*.properties</value>
			</list>
		</property>
	</bean> 
	
</beans>

2.spingmvc-servlet.xml

注意:要引入cache的命名空间!同时,要引入:

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

因为,spingmvc的配置文件和applicationContext的配置文件不在一个xml中,要分别引入注解,@Cacheable才能起作用!这点注意一下,如果你的配置了@Cacheable不起作用,可能是这个原因!!

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation=" 
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">
	
	<!-- 指定一个包让其自动扫描 -->
	<context:component-scan base-package="com.book.web.controller">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
	</context:component-scan>

	<!--  换成下面的可以防止中文乱码问题!spring版本必需为3.1或以上,上面的命名空间不需要版本号!
	<mvc:annotation-driven/>-->
	<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
              <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
	<!-- 支持缓存注解 ,如果springmvc的spingmvc的配置文件分开,
	必须在两个文件中都加入这个配置,Cacheable注解才起作用!-->
	<cache:annotation-driven cache-manager="cacheManager" />
	
	<!-- 下面注释的部分表示:强调所有的请求都要经过springmvc框架 -->
    <mvc:default-servlet-handler/>
	
	<!-- 放行了以/static/开始的请求 -->
	<mvc:resources location="/static/" mapping="/static/**"/> 
	
	<!-- 当一个方法完全是为了跳转时,我们可以省略该方法,而在此写一个配置就行了
	<mvc:view-controller path="/index" view-name="index"/>
	<mvc:view-controller path="/main" view-name="main"/>
	<mvc:view-controller path="/success" view-name="success"/> -->
	
	<!-- 配置springmvc的视图解析器 -->
	<bean id="viewResolver" 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="suffix" value=".jsp"/>
		<property name="prefix" value="/WEB-INF/views/"/>
	</bean>
	
	<!-- 文件上传解析器   -->
	<bean id="multipartResolver" 
	    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
	    <property name="maxUploadSize" value="100000"/> 
	</bean>
</beans>

3.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir"/>
    <!-- 默认缓存 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>


  <!-- 测试 a-->
  <cache
      name="cachea"
      eternal="true"
      overflowToDisk="true"
      diskPersistent="true"
      timeToIdleSeconds="0"
      timeToLiveSeconds="0"
      maxEntriesLocalHeap="10000"
      maxEntriesLocalDisk="1000000"
      diskExpiryThreadIntervalSeconds="120"
      memoryStoreEvictionPolicy="LRU">
  </cache>
	 
	 <!--LRU Used最近最少使用算法 ,10分-->
	<cache name="EhcacheMap" maxElementsInMemory="5000" eternal="false" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="300" 
		diskSpoolBufferSizeMB="1024" diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU">
	</cache>
	
	<!--LFU 最不经常使用 ,最少2分钟,最长3分, 最多1万个 , maxElementsInMemory="8000"  maxBytesLocalHeap="128M" --> 
	<cache name="EhCacheLocal" statistics="true" maxElementsInMemory="50000" eternal="false" overflowToDisk="false"
	diskExpiryThreadIntervalSeconds="120" timeToIdleSeconds="120" timeToLiveSeconds="180" memoryStoreEvictionPolicy="LFU">
	</cache>
	<cache name="EhCachePage" statistics="true" maxElementsInMemory="2000" eternal="false" overflowToDisk="false" 
    diskExpiryThreadIntervalSeconds="120" timeToIdleSeconds="120" timeToLiveSeconds="180" memoryStoreEvictionPolicy="LFU">
    </cache>


	<cache name="ipNetTypeCache" maxElementsInMemory="2000000"
		   eternal="false" overflowToDisk="false" />
	<cache name="ipCountryCache" maxElementsInMemory="500000"
		   eternal="false" timeToIdleSeconds="1200" overflowToDisk="false" />
</ehcache>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
	id="WebApp_ID" version="3.0">
	
	<!-- 加载Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:context/applicationContext.xml</param-value>
	</context-param>
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
	
	<!-- springmvc的配置 -->
	<servlet>
		<!-- 此处的name关系到springmvc-servlet.xml中springmvc名称 -->
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 如果springmvc-servlet.xml放在WEB-INF下面,可以省略这个配置! -->
			<param-value>classpath*:context/springmvc-servlet.xml</param-value>
		</init-param>
		<init-param>
			<param-name>publishContext</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
		<!-- / 表示过滤所有 -->
	</servlet-mapping>
	
</web-app>

三、代码

1.测试的service

package com.book.web.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 测试一下springmvc和ehcache结合
 * @author liweihan
 *
 */
@Service
public class TestUserService {
	
	private Map<Integer,String> usersData = new ConcurrentHashMap<Integer, String>();
	private static Logger logger = LoggerFactory.getLogger(TestUserService.class);
	
	public TestUserService() {
		logger.info(" ====== 用户初始化数据开始....");
		usersData.put(1, "韩超");
		usersData.put(2, "我的博客:http://blog.51cto.com/hanchaohan");
		usersData.put(3, "我的新博客:https://my.oschina.net/hanchao/blog");
		logger.info(" ====== 用户初始化数据结束...");
	}
	
	/**
	 * 注意:我们只缓存result不为NULL的结果
	 * @param userId
	 * @return
	 */
	@Cacheable(value="cachea",key="'user_' + #userId",unless="#result == null")
	public String get(int userId) {
		logger.info(" ====== 用户信息:" + usersData.get(userId));
		return usersData.get(userId);
	}
	
	/**
	 * 注意事项:
	 * 在此类的get2()方法内部,我们调用get()方法的试试,@Cacheable是不起作用的;
	 * 原因:是@Cacheable是基于SpringAOP代理类,get()方法是内部方法,
	 * 在get2()中直接调用get()方法时,是不走代理的!
	 * 
	 * @param userId
	 * @return
	 */
	public String get2(int userId) {
		logger.info(" get2 ====== 用户信息:" + usersData.get(userId));
		return get(userId);
	}

	
	/**
	 * 数据修改了,我们要更新缓存
	 * 注意事项:
	 * @CachePut:这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中。
	 * 
	 * @param userId
	 * @return
	 */
	@CachePut(value="cachea",key="'user_' + #userId")
	public String update(int userId,String value) {
		logger.info(" ====== update 用户信息:" + value);
		usersData.put(userId, value);
		return value;
	}
	
	/**
	 * 根据Id清除缓存
	 * @param userId
	 */
	@CacheEvict(value="cachea",key="'user_' + #userId")
	public void clearById(int userId) {
		logger.info(" ====== clear cache 用户Id:" + userId);
	}
	
	/**
	 * cachea中的所有缓存都被清除!
	 */
	@CacheEvict(value="cachea",allEntries=true)
	public void clearAll() {
		logger.info(" ====== clear all cache !");
	}
}

还可以这样写:

155754_8qvD_220449.png

2.Contrller

package com.book.web.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * springmvc和ehcache结合
 * @author liweihan
 *
 */
@Controller
@RequestMapping("/cachet")
public class TestSpringmvc {
	
	@Autowired
	TestUserService testUserService;
	
	private static Logger logger = LoggerFactory.getLogger(TestSpringmvc.class);
	
	/**
	 * 获取用户信息:直接调用缓存方法
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get/{id}")
	public String getUser(@PathVariable(value = "id") int id) {
		System.getProperty("java.io.tmpdir") ;
		logger.info(" ----- 用户Id:{},{}",id,System.getProperty("java.io.tmpdir"));
		return testUserService.get(id);
	}
	
	/**
	 * 获取用户信息2:间接获取缓存方法
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get2/{id}")
	public String getUser2(@PathVariable(value = "id") int id) {
		logger.info(" get2 ----- 用户Id:{}",id);
		return testUserService.get2(id);
	}

	/**
	 * 更新用户信息:同时更新缓存!
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update/{id}")
	public String updateUser(@PathVariable(value = "id") int id) {
		logger.info(" update ----- 用户Id:{}",id);
		testUserService.update(id,"更新的内容"+id);
		return "OK";
	}
	
	/**
	 * 根据Id清除缓存
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/clear/{id}")
	public String clearUserCacheById(@PathVariable(value = "id") int id) {
		testUserService.clearById(id);
		return "OK";
	}
	
	/**
	 * 清除所有缓存
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/clearall")
	public String clearAll() {
		testUserService.clearAll();
		return "clearAll";
	}
}

3.使用@CacheConfig

package com.book.web.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 测试一下springmvc和ehcache结合
 * @author liweihan
 *
 */
@Service
@CacheConfig(cacheNames="cachea")
public class TestUserService {
	
	private Map<Integer,String> usersData = new ConcurrentHashMap<Integer, String>();
	private static Logger logger = LoggerFactory.getLogger(TestUserService.class);
	
	public TestUserService() {
		logger.info(" ====== 用户初始化数据开始....");
		usersData.put(1, "韩超");
		usersData.put(2, "我的博客:http://blog.51cto.com/hanchaohan");
		usersData.put(3, "我的新博客:https://my.oschina.net/hanchao/blog");
		logger.info(" ====== 用户初始化数据结束...");
	}
	
	/**
	 * 注意:我们只缓存result不为NULL的结果
	 * @param userId
	 * @return
	 */
	@Cacheable(key="'user_' + #userId",unless="#result == null")
	public String get(int userId) {
		logger.info(" ====== 用户信息:" + usersData.get(userId));
		return usersData.get(userId);
	}
	
	/**
	 * 注意事项:
	 * 在此类的get2()方法内部,我们调用get()方法的试试,@Cacheable是不起作用的;
	 * 原因:是@Cacheable是基于SpringAOP代理类,get()方法是内部方法,
	 * 在get2()中直接调用get()方法时,是不走代理的!
	 * 
	 * @param userId
	 * @return
	 */
	public String get2(int userId) {
		logger.info(" get2 ====== 用户信息:" + usersData.get(userId));
		return get(userId);
	}

	
	/**
	 * 数据修改了,我们要更新缓存
	 * 注意事项:
	 * @CachePut:这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中。
	 * 
	 * @param userId
	 * @return
	 */
	@CachePut(key="'user_' + #userId")
	public String update(int userId,String value) {
		logger.info(" ====== update 用户信息:" + value);
		usersData.put(userId, value);
		return value;
	}
	
	/**
	 * 根据Id清除缓存
	 * @param userId
	 */
	@CacheEvict(key="'user_' + #userId")
	public void clearById(int userId) {
		logger.info(" ====== clear cache 用户Id:" + userId);
	}
	
	/**
	 * cachea中的所有缓存都被清除!
	 */
	@CacheEvict(allEntries=true)
	public void clearAll() {
		logger.info(" ====== clear all cache !");
	}
}

访问相关链接可以看到相关效果!

http://localhost:8080/cachet/get/3

http://localhost:8080/cachet/clear/3

四、注解解释

@CachePut:这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中。

@Cacheable:当重复使用相同参数调用方法的时候,方法本身不会被调用执行,即方法本身被略过了,取而代之的是方法的结果直接从缓存中找到并返回了

@CachEvict: 主要针对方法配置,能够根据一定的条件对缓存进行清空

@CacheConfig(cacheNames = "cachea"):配置了该数据访问对象中返回的内容将存储于名为cachea的缓存对象中,我们也可以不使用该注解,直接通过@Cacheable自己配置缓存集的名字来定义。cacheNames为Spring 4新增,作为value的别名),用于指定缓存存储的集合名。由于Spring 4中新增了@CacheConfig,因此在Spring 3中原本必须有的value属性,也成为非必需项了

五、注意事项

《1.》@Cacheable标注的方法,如果其所在的类实现了某一个接口,那么该方法也必须出现在接口里面,否则cache无效。
具体的原因是, Spring把实现类装载成为Bean的时候,会用代理包装一下,所以从Spring Bean的角度看,只有接口里面的方法是可见的,其它的都隐藏了,自然课看不到实现类里面的非接口方法,@Cacheable不起作用。

在使用Spring @Cacheable注解的时候,要注意,如果类A的方法f()被标注了@Cacheable注解,那么当类A的其他方法,例如:f2(),去直接调用f()的时候,@Cacheable是不起作用的,原因是@Cacheable是基于Spring AOP代理类,f2()属于内部方法,直接调用f()时,是不走代理的

《2.》当方法返回空值或null时,不想被缓存,可以使用:unless="#result == null" ,见:https://stackoverflow.com/questions/12113725/how-do-i-tell-spring-cache-not-to-cache-null-value-in-cacheable-annotation

162848_FFbU_220449.png

 

转载于:https://my.oschina.net/hanchao/blog/1596654

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值