前言:本項目基於spring4.x構建,使用ehcache3.5.2和JCache(jsr107規範)html
<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>
<?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>
<?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>
@Cacheable(value="cameraCache",key="#userid")java
public String getCameraList(String userid,Integer otherparam) {spring
...
api
}緩存
spring-cache的註解比較簡單就再也不贅述了。ui