SpringBoot开启缓存注解
https://blog.csdn.net/sanjay_f/article/details/47372967
https://www.cnblogs.com/lic309/p/4072848.html
https://blog.csdn.net/u012373815/article/details/54564076/
Spring Boot开启缓存注解
1:Spring Boot 常用缓存配置:
@Cacheable、@CachePut、@CacheEvict 注释介绍
@Cacheable
开启缓存,将查询到的结果对象,存到缓存中,一般用在查询方法上
@Cacheable(cacheNames = {"queryNews"})
public News queryNews(long htmlid) {
logger.debug(htmlid);
return foreMapper.queryNews(htmlid);
}
查询结果作为缓存,给缓存一个名字,如果没有默认的键,就用方法的参数作为键。
@CachePut
将添加的对象加到缓存中,一般用在插入,更新方法上
@CacheEvict
将缓存废弃,一般添加到删除方法上
@CacheEvict(cacheNames = {"queryByCategory","queryNews","backQuery","selectCount"},allEntries=true)
public int delete(long htmlid) throws Exception{
int i1=manageMapper.deleteNewshtmlid(htmlid);
int i2=manageMapper.deleteNewslistHtmlid(htmlid);
return i1;
}
一次可以删除多个缓存,根据缓存名。allEtries设置为true,将集合中的缓存一下删完。
2:自定义缓存,使用Spring Boot配置
添加依赖
'net.sf.ehcache:ehcache:2.8.3'
package com.li.configuration; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; @Configuration
// 标注启动了缓存
@encaching
public class CacheConfiguration { /*
* ehcache 主要的管理器
*/
@Bean(name = "appEhCacheCacheManager")
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
return new EhCacheCacheManager (bean.getObject ());
} /*
* 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
*/
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("/encache.xml"));
cacheManagerFactoryBean.setShared (true);
return cacheManagerFactoryBean;
}
}
添加配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache" /> <defaultCache eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="queryByCategory" eternal="true" maxElementsInMemory="5000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="queryNews" eternal="true" maxElementsInMemory="5000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="queryGroup" eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="queryByHtmlid" eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="query" eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="backQuery" eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> <cache name="selectCount" eternal="true" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> </ehcache>
SpringBoot开启缓存注解的更多相关文章
- springboot 开启缓存
Caching Data with Spring This guide walks you through the process of enabling caching on a Spring ma ...
- Spring之缓存注解@Cacheable
https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...
- spring-boot -缓存注解
缓存:商品信息放到缓存中间件中, 验证码几秒钟有效也是放在缓存中间件. 缓存规范 交互流程: 如果需要使用jRS107需要导入包: java.cache.cache-api JSR107提供的是接口, ...
- springboot与缓存(redis,或者caffeine,guava)
1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...
- SpringBoot 整合缓存Cacheable实战详细使用
前言 我知道在接口api项目中,频繁的调用接口获取数据,查询数据库是非常耗费资源的,于是就有了缓存技术,可以把一些不常更新,或者经常使用的数据,缓存起来,然后下次再请求时候,就直接从缓存中获取,不需要 ...
- Spring缓存注解@CachePut , @CacheEvict,@CacheConfig使用
Cacheable CachePut CacheEvict CacheConfig 开启缓存注解 @Cacheable @Cacheable是用来声明方法是可缓存的.将结果存储到缓存中以便后续使用相同 ...
- springboot:自定义缓存注解,实现生存时间需求
需求背景:在使用springbot cache时,发现@cacheabe不能设置缓存时间,导致生成的缓存始终在redis中. 环境:springboot 2.1.5 + redis 解决办法:利用AO ...
- SpringBoot之日志注解和缓存优化
SpringBoot之日志注解和缓存优化 日志注解: 关于SpringBoot中的日志处理,在之前的文章中页写过: 点击进入 这次通过注解+Aop的方式来实现日志的输出: 首先需要定义一个注解类: @ ...
- SpringBoot + redis + @Cacheable注解实现缓存清除缓存
一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...
随机推荐
- 【转】Internet与Intranet区别
提起Internet,大家都知道它是一个蓬勃发展的国际互联网. 而Intranet则是近两年才发展起来的新事物,通常被称作企业内部网. Internet是一组全球范围内信息资源的名字.这些资源非常巨大 ...
- easyui------自动合并行
转载: http://www.cnblogs.com/xiangzhong/p/5088259.html#undefined 1.引入插件 $.extend($.fn.datagrid.methods ...
- 有人问thinkphp的标签解析的时候为什么出现标签内内容空格丢失
举例如下 该代码被解析后 变为 并不是 active li bg 这里面的空格没有了 我试了多次,确实是这样,后来想了想 应该是框架解析的时候自动处理了,然后找了找框架代码 Template.cl ...
- HDFS原理解析(总体架构,读写操作流程)
前言 HDFS 是一个能够面向大规模数据使用的,可进行扩展的文件存储与传递系统.是一种允许文件通过网络在多台主机上分享的文件系统,可让多机器上的多用户分享文件和 存储空间.让实际上是通过网络来访问文件 ...
- grep递归查找子目录
想要在各种文件里面找一个指定的文本,本来的方法太土了,在网上搜了一下,发现个好的方法,不过也有些问题.原文如下: 第一个,这个是看别人脚本的,配合find实现,-maxdepth指定深度,如果查找到底 ...
- 关于C中I/O缓冲区的解释
用户程序调用C标准I/O库函数读写文件或设备,而这些库函数要通过系统调用把读写请求传给内核,最终由内核驱动磁盘或设备完成I/O操作.C标准库为每个打开的文件分配一个I/O缓冲区以加速读写操作,通过文件 ...
- MQTT协议笔记之订阅
前言 记忆不太好的时候,只能翻看以前的文章/笔记重新温习一遍,但找不到MQTT协议有关订阅部分的描述,好不容易从Evernote中找到贴出来,这样整个MQTT协议笔记,就比较齐全了. SUBSCRIB ...
- spring aop 样例
基于注解的AOP 方式 1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver- ...
- [干货] 有了微信小程序,谁还学ReactNative?
版权声明:本文由贺嘉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/145 来源:腾云阁 https://www.qclou ...
- js判断手机型号
由于oppo手机自带浏览器的高度底部多了144px导航栏 所以:专门针对oppo手机做适配: var dowphone = document.getElementById("dowphone ...