依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>cache</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
启动类
@SpringBootApplication
@EnableCaching //开启缓存功能
@Slf4j
public class CacheRedisApplication {
public static void main(String[] args) {
SpringApplication.run(CacheRedisApplication.class, args);
}
}
yml配置
server:
port: 8080
spring:
application:
#应用的名称,可选
name: cache_redis
redis:
host: 192.168.2.112
port: 20006
database: 2
cache:
redis:
#设置缓存有效期
time-to-live: 1800000
# 不配置会导致key存在空串(缓存key里面存在a:"":b情况)
key-prefix: hhhh
注解实现
package com.ywj.cache.controller;
import com.ywj.cache.entity.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author:yaoxiaojian
* @create: 2023-02-24 15:39
* @Description:
*/
@RestController
@RequestMapping("/user")
public class CacheController {
/**
* @CachePut: 将方法的返回值放入缓存
* value: 缓存的名称,每个缓存名称下可以有多个key
* key: 缓存的key
*/
@CachePut(value = "userCache", key = "#user.id")
@PostMapping
public User save(@RequestBody User user) {
return user;
}
/**
* @CacheEvict: 清理指定缓存
* value: 缓存的名称,每个缓存名称下可以有多个key
* key: 缓存的key
*/
@CacheEvict(value = "userCache", key = "#id")
@DeleteMapping("/{id}")
public void delete(@PathVariable String id) {
System.out.println("hhhh");
}
/**
* @Cacheable: 在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
* value: 缓存的名称,每个缓存名称下可以有多个key
* key: 缓存的key
* condition: 条件,满足条件才缓存数据
* unless: 满足条件则不缓存
* */
@Cacheable(value = "userCache",key = "#id",unless = "#result == null ")
@GetMapping("/{id}")
public User getById(@PathVariable String id){
User user = new User();
user.setId("qiweiqi");
return user;
}
//表达式合并
@Cacheable(value = "userCache",key = "#query.id+':'+#query.username")
@GetMapping("/list")
public List<User> list(User query){
List<User> list = new ArrayList<User>();
for (int i = 0; i < 5; i++) {
User user = new User();
user.setId("1");
user.setUsername("1");
user.setPassword("1");
list.add(user);
}
return list;
}
}