@Caching&@CacheConfig
public @interface Caching { Cacheable[] cacheable() default {}; CachePut[] put() default {}; CacheEvict[] evict() default {}; }
CacheConfig 抽取缓存公共配置
@CacheConfig(cacheNames={"department"}) @Service public class DepartmentService { @Autowired private DepartmentMapper departmentMapper; // 缓存数据 @Cacheable(key = "#id") public Department getById(Integer id){ return departmentMapper.getById(id); } }
搭建 redis 环境&测试
本机安装
# 启动
$ redis-server
# 停止本机redis
$ redid-cli shutdown
可以使用 Docker 方式安装
# 安装
docker pull redis
# 启动
docker run -d -p 6379:6379 --name my-redis redis
# 查看运行状态
docker ps
Redis 命令
http://www.redis.cn/commands.html
RedisTemplate&序列化机制
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
指定 redis 地址
spring:
redis:
host: localhost
redis 常见 5 中数据类型
String 字符串
List 列表
Set 集合
Hash 散列
ZSet 有序集合
测试使用
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; @SpringBootTest public class RedisTest { // k-v字符串形式 @Autowired private StringRedisTemplate stringRedisTemplate; // k-v对象形式 @Autowired private RedisTemplate redisTemplate; @Test public void testRedis(){ stringRedisTemplate.opsForValue().set("name", "Tom"); String name = stringRedisTemplate.opsForValue().get("name"); System.out.println(name); } }
保存对象:序列化后再保存
1、手动转换数据为 json
2、自定义序列化规则
package com.example.demo.config; import com.example.demo.pojo.Employee; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; @Configuration public class MyRedisConfig { @Bean public RedisTemplate<Object, Employee> employeeRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<Object, Employee> redisTemplate = new RedisTemplate<Object, Employee>(); redisTemplate.setConnectionFactory(factory); // 设置序列化器 Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class); redisTemplate.setDefaultSerializer(serializer); return redisTemplate; } }
测试
package com.example.demo; import com.example.demo.pojo.Employee; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; @SpringBootTest public class RedisTest { @Autowired RedisTemplate<Object, Employee> employeeRedisTemplate; @Test public void testRedisSerializer(){ Employee employee = new Employee(); employee.setName("Tom"); employee.setAge(23); employeeRedisTemplate.opsForValue().set("emp", employee); } }
引入 redis-starter 之后,缓存组件使用 RedisCache
利用序列化保存数据,都是以 Object 形式保存,默认使用 jdk 序列化机制
可以保存为 json