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连接工厂类 ...
随机推荐
- C# 默认访问修饰符
c# 中类,成员,枚举,结构等默认访问修饰符是? 根据MSDN文档有: [MSDN] Classes and structs that are not nested within other clas ...
- Hibernate 配置文件的基础配置
Hibernate 配置文件主要用于配置数据库连接和 Hibernate运行时所需的各种属性 每个 Hibernate 配置文件对应一个 Configuration 对象 Hibernate.cfg. ...
- Java 中的字符串转为二进制
/** * 将字符串转为二进制 */ public class StrConversion { public static void main(String args[]) { String str ...
- 顺序表的原理与python中的list类型
数据是如何在内存中存储的? 在32位的计算机上,1个字节有8位,内存寻址的最小单位就是字节.假设我们有一个int类型的值,它从0x10开始,一个int占据4个字节,则其结束于0x13. 那么数据类型有 ...
- CSS3_元素拖曳原理_设置全局点击捕获_九宫格碰撞检测_自定义滚动条
拖曳原理: 元素的初始位置 + 鼠标距离差 = 元素最终位置 使元素可以拖动 function dragElement(obj){ obj.onmousedown = function(e){ e = ...
- textarea 中的换行符
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DEV中右键菜单如何只在非空单元格上显示?
问题: 1. 开发时,我的winform程序中有很多gridview,我希望右键菜单只在我点击非空的行时才显示,点击其他空白区域时不显示: 2. 有一个树状导航图,treelist 中的节点都有右键菜 ...
- centos7查看网卡UUID
https://blog.csdn.net/kepa520/article/details/50222049 查看网卡UUID nmcli con show 查看mac地址 nmcli device ...
- Java作业二(2017-9-18)
/*程序员龚猛,求整数各个位上的和*/ import java.util.Scanner; public class Helloworld{ public static void main(Strin ...
- yum安装mysql5.7
[root@ycj ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm //下载安装 ...