SpringBoot缓存之redis--最简单的使用方式
第一步:配置redis
这里使用的是yml类型的配置文件
mybatis:
mapper-locations: classpath:mapping/*.xml
spring:
datasource:
name: miaosha
url: jdbc:mysql://127.0.0.1:3306/miaosha?serverTimezone=UTC
username: root
password: 1234
type: com.alibaba.druid.pool.DruidDataSource #数据源
driverClassName: com.mysql.jdbc.Driver
redis:
host: 10.0.75.1 #地址
port: 6379 #端口号
timeout: 20000 #连接超时时间
cache: #缓存类型
type: redis
第二步:在启动类上添加 @EnableCaching 注解
@SpringBootApplication(scanBasePackages = {"com.miaoshaproject"})
@MapperScan("com.miaoshaproject.dao")
@EnableCaching
public class App { public static void main( String[] args ) {
ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
}
}
第三步:在需要缓存的方法上添加 @Cacheable 注解
@Service
@CacheConfig(cacheNames = {"itemService"})
public class ItemServiceImpl implements ItemService { @Override
@Cacheable(value = {"item"},key ="#p0")
public String getItemById(Integer id) {
String name = "123";
return name;
}
}
注:关于springboot缓存名的说明:
使用SpringBoot缓存必须配置名字可以使用@CacheConfig(cacheNames = {"itemService"})在类上配置该类公用的名字,也可以使用@Cacheable(value=”item”)在方法上配置只适用于该方法的名字。如果类和方法上都有配置,以方法上的为准。
springBoot会自动拼装缓存名,规则是:配置的名字+两个冒号+方法的实参;
注:关于@CacheConfig和@Cacheable注解的说明:
@Cacheable(value=”item”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 item 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象。
在上面代码示例中@Cacheable注解设置了两个参数一个是value,一个是key。key的值"#p0"在执行过程中会被getItemById方法的实参所替换,例如id的值3 那么缓存的名字就会是"item::3";如果不设置key,系统会自动也会是这个效果。
如果是无参方法:
@CacheConfig is a class-level annotation that allows to share the cache names,如果你在你的方法写别的名字,那么依然以方法的名字为准。
SpringBoot缓存之redis--最简单的使用方式的更多相关文章
- Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程
Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...
- SpringBoot缓存技术
一.SpringBoot整合Ehhcache 添加maven依赖 <dependency> <groupId>org.springframework.boot</grou ...
- SpringBoot使用@Cacheable实现最简单的Redis缓存
前言 之前我们使用过RedisTemplate来实现redis缓存,然后使用工具类来实现操作redis的存储.这样的方式好处是很自由,但是还不是最简单的处理方式.对于一些简单的应用来说,其实redis ...
- SpringBoot中Shiro缓存使用Redis、Ehcache
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
- 在springboot中使用redis缓存,将缓存序列化为json格式的数据
背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...
- springboot 2 集成 redis 缓存 序列化
springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...
- SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制
一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...
- SpringBoot缓存管理(二) 整合Redis缓存实现
SpringBoot支持的缓存组件 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springframework.cache.Cache和org.spri ...
- 由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
随机推荐
- 【JavaScrpt】用js创建html上的元素
// 在body下创建一个div var createDiv=document.createElement("div"); createDiv.id='id_i'; createD ...
- PostgreSQL自学笔记:9 索引
9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...
- 移动端的搜索用的是from提交
html部分 <form action="javascript:searchSubmit();"> <input type="search" ...
- php获取文章的第一张图片
今天做东西的时候遇到一个问题就是如何把文章提取出来作为文章列表呢? 因为用了Ueditor,所以提交的数据包含了html标签. 搜索了一会找到了一个方案,用php自带的函数去掉了html标签. $ar ...
- JS的javascript:void(0)用法
javascript:void(0)用法如下: <a href="javascript:void(0)"></a> // 执行js函数,0表示不执行函数. ...
- 2018-2019-1 20189210 《LInux内核原理与分析》第八周作业
一 .可执行程序工作原理 程序编译 预处理:gcc -E hello.c -o hello.i 编译:gcc -S hello.i -o hello.s -m32 汇编:gcc -c hello.s ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- AWS Nginx Started but not Serving AWS上Nginx服务器无法正常工作
After install the Nginx on AWS instance, and visit your public ip address, you might see the followi ...
- apt下载open-jdk8报错add-apt-repository: command not found
今天下载jdk8报错 在Ubuntu下,时不时会有这个错误的. add-apt-repository: command not found sudo apt-get install software- ...
- 怎样将PDF文件转换成Excel表格
PDF文件怎样转换成Excel表格呢?因为很多的数据信息现在都是通过PDF文件进行传输的,所以很多时候,信息的接受者都需要将这些PDF文件所传输的数据信息转换成Excel表格来进行整理,但是我们应该怎 ...