使用mybatisplus的redis自定义缓存
时间: 2023-05-26 19:03:15 浏览: 110
MybatisPlus提供了自定义Redis缓存的功能,可以通过自定义Redis缓存实现数据的快速查询。
1. 引入Redis依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
2. 自定义缓存
```java
@Component
public class RedisCache implements Cache {
private final RedisTemplate<Object, Object> redisTemplate;
private final String cacheName;
private final long ttl;
public RedisCache(RedisTemplate<Object, Object> redisTemplate, String cacheName) {
this.redisTemplate = redisTemplate;
this.cacheName = cacheName;
this.ttl = 60 * 5; //默认5分钟过期时间
}
public RedisCache(RedisTemplate<Object, Object> redisTemplate, String cacheName, long ttl) {
this.redisTemplate = redisTemplate;
this.cacheName = cacheName;
this.ttl = ttl;
}
@Override
public String getId() {
return cacheName;
}
@Override
public void putObject(Object key, Object value) {
redisTemplate.opsForValue().set(key, value, ttl, TimeUnit.SECONDS);
}
@Override
public Object getObject(Object key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public Object removeObject(Object key) {
redisTemplate.delete(key);
return null;
}
@Override
public void clear() {
redisTemplate.delete(redisTemplate.keys("*" + cacheName + "*"));
}
@Override
public int getSize() {
return Math.toIntExact(redisTemplate.keys("*" + cacheName + "*").size());
}
@Override
public ReadWriteLock getReadWriteLock() {
return null;
}
}
```
3. 注册自定义缓存
```java
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
private final RedisConnectionFactory redisConnectionFactory;
public CacheConfig(RedisConnectionFactory redisConnectionFactory) {
this.redisConnectionFactory = redisConnectionFactory;
}
@Bean
public CacheManager cacheManager() {
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.disableCachingNullValues();
RedisCacheConfiguration userCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<String, RedisCacheConfiguration>() {{
put("user", userCacheConfig);
}};
RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(defaultCacheConfig)
.withInitialCacheConfigurations(cacheConfigurations)
.build();
return cacheManager;
}
@Bean
@Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver(cacheManager());
}
@Bean
@Override
public CacheErrorHandler errorHandler() {
return new SimpleCacheErrorHandler();
}
@Bean
public Cache redisCache(RedisTemplate<Object, Object> redisTemplate) {
return new RedisCache(redisTemplate, "product");
}
}
```
4. 使用自定义缓存
在Mapper接口方法上添加@Cacheable注解,指定使用自定义的Redis缓存即可。
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Cacheable(value = "user", key = "#id")
User getById(Long id);
}
```
阅读全文