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 ...
随机推荐
- python2.7.13环境搭建
查看当前系统中的 Python 版本,可以看到实验室的这台服务器已经安装了 Python 2.6.6 python --version 检查 CentOS 版本,我们可以看到这台服务器的 CentOS ...
- 【Android开发】如何设计开发一款Android App
本文从开发工具选择,UI界面.图片模块.网络模块.数据库产品选择.性能.安全性等几个方面讲述了如果开发一个Android应用.现在整理出来分享给广大的Android程序员. 开发工具的选择 开发工具我 ...
- HTML&CSS精选笔记_列表与超链接
列表与超链接 列表标记 无序列表ul 无序列表的各个列表项之间没有顺序级别之分,是并列的 <ul> <li>列表项1</li> <li>列表项2< ...
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- python常用BIF汇总
append():在列表末尾增加一个数据项:例如a.append('hello') pop():用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值:例如a.pop() extend(): ...
- Linux环境下$开头的相关变量的含义
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- 如果我们的MCU调试不通过,我们该如何测试?(未完待续)
我们拿到一款MCU,首先在选型的时候对他们都会有一个大概的了解,首先我们知道它多大与的FLASH,多大的RAM,是否与我们的项目适合,资源上是否足够,内频是否足够,运行速率能否达到标准,性能怎么样,比 ...
- php学习八:封装
一:在php中,用class关键字来创建一个类,即进行封装:在类里面有成员属性和方法行为组成: 1.成员属性:用关键字var来声明,可以给初始值也可以不给;现在var废弃,用public来声明,pub ...
- Anfroid 在界面中显示图片 ImageView
ImageView1.什么是ImageView是显示图片的一个控件2.ImageView属性android:src ImageView的内容颜色 android:background ImageVie ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...