ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-04-19 17:02:02  阅读:236  来源: 互联网

标签:lang ehcache jcache java String spring cache


前言:本项目基于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的注解比较简单就不再赘述了。

 

 

标签:lang,ehcache,jcache,java,String,spring,cache
来源: https://blog.51cto.com/u_6329998/2718050

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有