日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的注解比較簡單就不再贅述了。

    ?

    轉載于:https://www.cnblogs.com/eguid/p/9667159.html

    總結

    以上是生活随笔為你收集整理的Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。