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 ...
随机推荐
- mysqldump工具,通过--where选项,导出指定表中指定数据?
需求描述: 今天在使用mysqldump工具导出表的时候,考虑能不能导出满足条件的数据行,不要 将表都导出来,查找资料,通过--where选项,就可以实现目的,做个实验,在此记录下. 操作过程: 1. ...
- [ML] I'm back for Machine Learning
Hi, Long time no see. Briefly, I plan to step into this new area, data analysis. In the past few yea ...
- SQLServer------如何快速插入几万条测试数据
方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...
- swift - 移除界面上的所有元素
下面代码可以遍历移除页面视图上的所有元件: //清空所有子视图 func clearViews() { for v in self.view.subviews as [UIView] { v.remo ...
- rpm源码安装mysql
1)访问官网(mysql社区服务器) http://downloads.mysql.com/archives/community/ 2)选择自己需要的版本和对应服务器(例如 服务器是centos 6. ...
- 如果程序太大而不能在DOS下运行,怎样才能使它在DOS下运行呢?
如果你的程序因太大(超过640KB)而无法在DOS下运行,有两种办法可为该程序提供更多的内存.一种办法是使用覆盖管理程序(overlay manager).覆盖管理程序用来管理程序的模块,并根据需要把 ...
- N32903系列的基础知识(1)
N32903U1DN使用ARM926EJ-S内核,其内部集成的JPEG编解码器.CMOS摄像头接口.32通道的声音处理单元(SPU).ADC.DAC等不仅可以满足各种的应用需求,还能减少生产方面的物料 ...
- Effective C++ —— 实现(五)
条款26 : 尽可能延后变量定义式的出现时间 1. 你不只应该延后变量的定义,直到非得使用该变量的前一刻为止,甚至应该尝试延后这份定义直到能够给它初值实参为止.这样,不仅能够避免构造(和析构)非必要对 ...
- Effective C++ —— 设计与声明(四)
条款18 : 让接口容易被正确使用,不易被误用 欲开发一个“容易被正确使用,不容易被误用”的接口,首先必须考虑客户可能做出什么样的错误操作. 1. 明智而审慎地导入新类型对预防“接口被误用”有神奇疗 ...
- jsonObject的一些方法
1.从前端传过来的数字,默认是Integer类型不能直接用Long接收 错误写法: 报错:Exception in thread "main" java.lang.ClassCas ...