一、简介
同Ehcache 一样,classpath 下存在redis 配置,就默认使用RedisCacheManager 作为缓存提供者。
二、流程
1.新建spring boot Web 项目,pom.xml 添加依赖
<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>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce.core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.配置application.yml
spring:
redis:
host: 192.168.243.133
cache:
redis:
time-to-live: 180s
3.User.java
package com.vincent.po;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
4.UserService.java
package com.vincent.service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.vincent.po.User;
@Service
public class UserService {
@Cacheable(cacheNames = {"default_cache"},key = "#id")
public User get(String id) {
User user = new User();
user.setName("Vincent");
user.setAge(26);
System.out.println("cache method...");
return user;
}
@CachePut(cacheNames = {"default_cache"},key = "#id")
public User update(String id){
User user = new User();
user.setName("Vincent-" + id);
user.setAge(26);
System.out.println("update method...");
return user;
}
@CacheEvict(cacheNames = {"default_cache"},key = "#id")
public void delete(String id) {
System.out.println("delete method...");
}
}
5.TestController.java
package com.vincent.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vincent.po.User;
import com.vincent.service.UserService;
@RestController
public class TestController {
@Autowired
private UserService userService;
@GetMapping("/test/add")
public User add(String id,HttpServletRequest request) {
return userService.get(id);
}
@GetMapping("/test/update")
public User update(String id) {
return userService.update(id);
}
@GetMapping("/test/del")
public void del(String id) {
userService.delete(id);
}
}
6.@SpringBootApplication 修饰的类添加@EnableCaching 注解
三、测试
1.访问 http://localhost:8080/test/add?id=test-cache
查看redis 数据信息
2.访问 http://localhost:8080/test/update?id=test-cache 修改缓存数据
3.访问 http://localhost:8080/test/del?id=test-cache 删除缓存数据
四、总结
1.redis 中的key 都有一个前缀,默认是"缓存名::"
2.redis 中的缓存信息非常容易配置过期时间(application.yml 中配置spring.cache.redis.time-to-live )