前言:本項目基於spring4.x構建,使用ehcache3.5.2和JCache(jsr107規範)php
1、依賴
除了ehcache和cache-api外,注意引用spring-context-supportjava
-
-
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context-support</artifactId>
-
<version>4.3.16.RELEASE</version>
-
-
-
<groupId>org.ehcache</groupId>
-
<artifactId>ehcache</artifactId>
-
-
-
-
<groupId>javax.cache</groupId>
-
<artifactId>cache-api</artifactId>
-
-
2、配置
一、ehcache配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
-
xmlns:ehcache="http://www.ehcache.org/v3"
-
xmlns:jcache="http://www.ehcache.org/v3/jsr107">
-
-
-
-
<jcache:cache name="default" template="myDefaultTemplate"/>
-
-
-
-
<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:tti unit="minutes">20</ehcache:tti>
-
-
<ehcache:heap unit="entries">200</ehcache:heap>
-
-
-
-
<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:tti unit="minutes">30</ehcache:tti>
-
-
<ehcache:heap unit="entries">500</ehcache:heap>
-
-
-
-
<ehcache:cache-template name="myDefaultTemplate">
-
-
-
-
</ehcache:cache-template>
-
-
二、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">
-
-
<cache:annotation-driven cache-manager="cacheManager" />
-
<context:component-scan base-package="cc.eguid.cache" />
-
-
<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
-
<property name="cacheManagerUri" value="classpath:config/ehcache.xml" />
-
-
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
-
<property name="cacheManager" ref="jCacheManager" />
-
-
3、使緩存生效
一、註解方式使用
@Cacheable(value="cameraCache",key="#userid")spring
public String getCameraList(String userid,Integer otherparam) {api
...緩存
}ui
spring-cache的註解比較簡單就再也不贅述了。spa