springboot 搭配redis缓存
1.引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2.application.properties
spring.port=8080
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0
3.配置redisConfig
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean("oneKeyGenerator")
public KeyGenerator getKeyGenerator (){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
} ;
}
}
4.测试接口
package com.example.cache.service;
import com.example.cache.domain.User;
public interface UserService {
User addUser(User user);
User updateUser(Integer id);
User selectUser(Integer id);
void deleteUser(Integer id);
}
5.实现类
@Service
public class UserServiceImpl implements UserService {
// 生成redis中的key名字为userCache::
@CachePut(value = "userCache",key = "#user.id")
@Override
public User addUser(User user) {
return user;
}
// 生成redis中的key名字为userCache::1
@Cacheable(cacheNames = "userCache",key = "#id",sync = true)
@Override
public User selectUser(Integer id) {
User user = new User();
user.setId(id);
user.setName("cicada5mile");
System.out.println("执行数据库查询");
return user;
}
// 删除缓存中的key名字为userCache::1
@CacheEvict(value = "userCache",key = "#id",beforeInvocation = true)
@Override
public void deleteUser(Integer id) {
}
}
6.测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CacheApplication.class)
public class UserTest {
@Resource
UserService userService;
@Test
public void testAdd (){
User user = new User() ;
user.setId(1);
user.setName("cicada");
userService.addUser(user) ;
}
@Test
public void testSelect (){
userService.selectUser(1) ;
}
@Test
public void testDelete (){
userService.deleteUser(3) ;
}
}
7.补充说明
@Cacheable
@CacheEvict
使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,
而使用@CacheEvict标记的方法会在方法执行前或者执行后移除Spring Cache中的某些元素
@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
8.redis样例

springboot 搭配redis缓存的更多相关文章
- springboot整合redis缓存
使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- SpringBoot使用redis缓存List<Object>
一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...
- springboot集成redis缓存
1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...
- SpringBoot整合redis缓存(一)
准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...
- Java SpringBoot使用Redis缓存和Ehcache
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...
- springboot整合redis缓存一些知识点
前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- SpringBoot(七) - Redis 缓存
1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...
随机推荐
- BLE广播信道空中包详解
广播信道空中包 在学习BLE的过程中,对于广播信道的空中包有许多混淆的地方,包括各个空中包的用途,帧格式等.现在想把他们做一个总结和归纳. BLE广播信道中的空中包分为有以下几种: 可连接非定向广播 ...
- 1083 List Grades
Given a list of N student records with name, ID and grade. You are supposed to sort the records with ...
- Linux在shell终端中清空DNS缓存,刷新DNS的方法
现在很多Linux发行版都没有内置DNS本地缓存,Linux不像Windows那样可以使用ipconfig /flushdns来刷新,在Linux下无需刷新,因为本身没有缓存. 前言 在Linux系统 ...
- Laravel打印sql日志
直接打印 use Log; use DB; DB::connection()->enableQueryLog(); Log::info(DB::getQueryLog()); //print_r ...
- deep freeze standard v8.x
62S93Y4Q-6C6E4EQQ-7MKMZA9F-RK5H6CSZ-AB7W3BMMPJ6GGQDCPT5EPVWGJP3EEQ5GAV2TCQD7-F8J933CA-6AEHN9ZG-2FW3M ...
- LA3905流星
题意: 在一个二维平面上有n个流星,每个流星有自己的初始位置和速度,有一个照相机,张相机的可视范围是一个矩形框,左下角(0,0)右上角(w ,h),然后问你相机的矩形内出现的最多的流星数是 ...
- python中让输出不换行
Python2.x python2.x中输出默认是换行的,为了抑制换行,可以在打印最后加一个逗号 Python3.x 到了python3中,print 变成一个函数,这种语法便行不通了. 我们可以使用 ...
- Linux中的shell和bash
目录 shell shell script sh bash Dash tty .pty 和 pts 学安全的我们,经常会听到说获得某服务器的shell,就是指获得某个服务器的操作权限.我们学习linu ...
- 1.简单认识PHP和环境搭建
1.关于PHP PHP(Hypertext Perprocessor,超文本预处理器),是一种服务器端.跨平台.HTML嵌入式的脚本语言,其独特的语法混合了C.Java.和Perl语言的特点,是一种被 ...
- Python中根据库包名学习使用该库包
目录 Python库包模块 import 语句 from-import 语句 搜索路径 PYTHONPATH 变量 命名空间和作用域 查看模块中所有变量和函数,以及查看具体函数的用法 globals( ...