spring缓存cache的使用详解
程序员文章站
2022-06-25 10:54:17
目录spring缓存cache的使用springcache配置缓存存活时间spring缓存cache的使用在spring配置文件中添加schema和spring对缓存注解的支持:
spring缓存cache的使用
在spring配置文件中添加schema和spring对缓存注解的支持:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" default-autowire="byname"> <!--缓存配置--> <cache:annotation-driven/>
在spring配置文件中加入缓存管理器:
<!-- generic cache manager --> <bean id="cachemanager" class="org.springframework.cache.support.simplecachemanager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.concurrentmapcachefactorybean" p:name="hardwarecache"/> <bean class="org.springframework.cache.concurrent.concurrentmapcachefactorybean" p:name="bannercache"/> </set> </property> </bean>
然后在代码的service的impl层加上如下注解即可把数据缓存起来:
@cacheable(value="bannercache")
其中@cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力.
当更新了数据库的数据,需要让缓存失效时,使用下面的注解:
这个注解表示让appcache缓存的所有数据都失效。
@cacheevict(value = "appcache", allentries = true)
springcache配置缓存存活时间
spring cache @cacheable本身不支持key expiration的设置,以下代码可自定义实现spring cache的expiration,针对redis、springboot2.0。
直接上代码:
@service @configuration public class customcachemng{ private logger logger = loggerfactory.getlogger(this.getclass()); // 指明自定义cachemanager的bean name @cacheable(value = "test",key = "'obj1'",cachemanager = "customcachemanager") public user cache1(){ user user = new user().setid(1); logger.info("1"); return user; } @cacheable(value = "test",key = "'obj2'") public user cache2(){ user user = new user().setid(1); logger.info("2"); return user; } // 自定义的cachemanager,实现存活2天 @bean(name = "customcachemanager") public cachemanager cachemanager( redistemplate<?, ?> redistemplate) { rediscachewriter writer = rediscachewriter.lockingrediscachewriter(redistemplate.getconnectionfactory()); rediscacheconfiguration config = rediscacheconfiguration.defaultcacheconfig().entryttl(duration.ofdays(2)); return new rediscachemanager(writer, config); } // 提供默认的cachemanager,应用于全局 @bean @primary public cachemanager defaultcachemanager( redistemplate<?, ?> redistemplate) { rediscachewriter writer = rediscachewriter.lockingrediscachewriter(redistemplate.getconnectionfactory()); rediscacheconfiguration config = rediscacheconfiguration.defaultcacheconfig(); return new rediscachemanager(writer, config); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: yeetalk怎么发布动态?yeetalk发布动态教程
下一篇: 当快手主播遇上腾讯系流量