Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)

前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范)

一、依赖

    除了ehcache和cache-api外,注意引用spring-context-support
                    
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context-support</artifactId>
                        <version>4.3.16.RELEASE</version>
                    </dependency>
		    <dependency>
		        <groupId>org.ehcache</groupId>
		        <artifactId>ehcache</artifactId>
		        <version>3.5.2</version>
		    </dependency>
		    <dependency>
		        <groupId>javax.cache</groupId>
		        <artifactId>cache-api</artifactId>
		        <version>1.0.0</version>
		    </dependency>

二、配置

1、ehcache配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache:config
    xmlns:ehcache="http://www.ehcache.org/v3"
    xmlns:jcache="http://www.ehcache.org/v3/jsr107">

  <ehcache:service>
    <jcache:defaults>
      <jcache:cache name="default" template="myDefaultTemplate"/>
    </jcache:defaults>
  </ehcache:service>

  <ehcache:cache alias="allCameraCache">
    <ehcache:key-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:key-type>
    <ehcache:value-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</ehcache:value-type>
    <ehcache:expiry>
      <ehcache:tti unit="minutes">20</ehcache:tti><!-- 数据过期时间20分钟 -->
    </ehcache:expiry>
    <ehcache:heap unit="entries">200</ehcache:heap><!-- 最多缓存200个对象 -->
  </ehcache:cache>

<!-- 使用模板,可以覆盖模板的属性 -->
  <ehcache:cache alias="cameraCache" uses-template="myDefaultTemplate">
    <ehcache:key-type>java.lang.Object</ehcache:key-type>
    <ehcache:value-type>java.lang.Object</ehcache:value-type>
	<ehcache:expiry>
      <ehcache:tti unit="minutes">30</ehcache:tti><!-- 数据过期时间30分钟,覆盖模板默认属性 -->
    </ehcache:expiry>
    <ehcache:heap unit="entries">500</ehcache:heap><!-- 最多缓存500个对象 -->
  </ehcache:cache>
  
  <!-- 默认模板 -->
  <ehcache:cache-template name="myDefaultTemplate">
    <ehcache:expiry>
      <ehcache:none/><!-- 缓存永不过期 -->
    </ehcache:expiry>
  </ehcache:cache-template>

</ehcache:config>

2、spring配置

<?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: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/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
	<!--eguid博客地址:http://bog.eguid.cc-->
	<cache:annotation-driven cache-manager="cacheManager" /><!--扫描cache注解,如果已有可以不要-->
	<context:component-scan base-package="cc.eguid.cache" /><!--扫描路径 -->
	<!-- jcache缓存 -->
	<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">  
    	    <property name="cacheManagerUri" value="classpath:config/ehcache.xml" />  <!--改成配置文件对应的路径-->
	</bean>
	<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">  
	    <property name="cacheManager" ref="jCacheManager" />
	</bean>  
</beans>


三、使缓存生效

1、注解方式使用

@Cacheable(value="cameraCache",key="#userid")

public String getCameraList(String userid,Integer otherparam) {

...

}

spring-cache的注解比较简单就不再赘述了。



猜你喜欢

转载自blog.csdn.net/eguid_1/article/details/80197624