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开启缓存注解的更多相关文章

  1. springboot 开启缓存

    Caching Data with Spring This guide walks you through the process of enabling caching on a Spring ma ...

  2. Spring之缓存注解@Cacheable

    https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...

  3. spring-boot -缓存注解

    缓存:商品信息放到缓存中间件中, 验证码几秒钟有效也是放在缓存中间件. 缓存规范 交互流程: 如果需要使用jRS107需要导入包: java.cache.cache-api JSR107提供的是接口, ...

  4. springboot与缓存(redis,或者caffeine,guava)

    1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...

  5. SpringBoot 整合缓存Cacheable实战详细使用

    前言 我知道在接口api项目中,频繁的调用接口获取数据,查询数据库是非常耗费资源的,于是就有了缓存技术,可以把一些不常更新,或者经常使用的数据,缓存起来,然后下次再请求时候,就直接从缓存中获取,不需要 ...

  6. Spring缓存注解@CachePut , @CacheEvict,@CacheConfig使用

    Cacheable CachePut CacheEvict CacheConfig 开启缓存注解 @Cacheable @Cacheable是用来声明方法是可缓存的.将结果存储到缓存中以便后续使用相同 ...

  7. springboot:自定义缓存注解,实现生存时间需求

    需求背景:在使用springbot cache时,发现@cacheabe不能设置缓存时间,导致生成的缓存始终在redis中. 环境:springboot 2.1.5 + redis 解决办法:利用AO ...

  8. SpringBoot之日志注解和缓存优化

    SpringBoot之日志注解和缓存优化 日志注解: 关于SpringBoot中的日志处理,在之前的文章中页写过: 点击进入 这次通过注解+Aop的方式来实现日志的输出: 首先需要定义一个注解类: @ ...

  9. SpringBoot + redis + @Cacheable注解实现缓存清除缓存

    一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...

随机推荐

  1. MathType只有你会的几个技巧

    太阳的后裔终于结局了,我们的乔妹被宋仲基撩走了,其中的撩妹技能你学到了几招?没学会,不要紧,还有MathType!只要你会MathType这几个技巧,撩妹绝对杠杠的.这可是连宋仲基都不会的! Math ...

  2. 移动HTML 5前端性能优化指南

    概述 PC优化手段在Mobile侧同样适用 在Mobile侧我们提出三秒种渲染完成首屏指标 基于第二点,首屏加载3秒完成或使用Loading 基于联通3G网络平均338KB/s(2.71Mb/s),所 ...

  3. Duilib教程-HelloDuilib及DuiDesigner的简单使用

    一.HelloDuilib 1. 首先理解DUILIB显示的一个基本流程,如下图: 在Duilib中,WindowImplBase 这个类代表了图中 “CWndClass”. 所以我们需要做的是: 1 ...

  4. file.wirtelines()方法【python】

    转自:http://www.jb51.net/article/66643.htm

  5. C语言各种存储模式的区别?最常用的存储模式有哪些?

    DOS用一种段地址结构来编址计算机的内存,每一个物理内存位置都有一个可通过段地址一偏移量的方式来访问的相关地址.为了支持这种段地址结构,大多数C编译程序都允许你用以下6种存储模式来创建程序: ---- ...

  6. UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样

    本文转载至 http://blog.csdn.net/hengshujiyi/article/details/9086093- (void)initFeedBackViews { //设置页面的背景颜 ...

  7. Java语言基本数据类型

    ■Java是一种强类型语言,每个变量都必须声明其类型.■Java的数据类型分为两大类:基本类型和引用类型(引用数据类型的大小统一为4个字节,记录的是其引用对象的地址).■Java中定义了3类8种基本数 ...

  8. click() bind() live() delegate()区别

    click(),bind(),live()都是执行事件时使用的方法 1.click()单击事件方法: $("a").click(function() { alert("h ...

  9. puremvc TS源码 (未测试...)

    根据as的puremvc修改的,尚未具体测试.... https://files.cnblogs.com/files/gamedaybyday/puremvc_ts.rar 补充一个小游戏puremv ...

  10. 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学

    [BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...