二级缓存,是指多个Sqlsession之间共享数据,但是也可以使用Redis这样的缓存作为存储点,但是不支持mybatisplus 里的方法,本文我们就来聊聊SpringBoot整合Mybatis实现二级缓存的相关方法吧
mybatis 原生
二级缓存,是指多个Sqlsession之间共享数据,但是也可以使用Redis这样的缓存作为存储点。 但是不支持mybatisplus 里的方法。
处理类,用于拦截mapper缓存。
import org.apache.ibatis.cache.Cache;
public record MybatisRedisCache(String id) implements Cache {
private static final StringRedisTemplate redisTemplate;
static {
redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
}
@Override
public String getId() {
return id;
}
@Override
public void putObject(Object key, Object value) {
if (value != null) {
redisTemplate.opsForValue().set(getKey(key), JSONUtil.toJsonStr(value));
}
}
@Override
public Object getObject(Object key) {
String string = redisTemplate.opsForValue().get(getKey(key));
if (string == null) return null;
return isArray(string);
}
@Override
public Object removeObject(Object key) {
String redisKey = getKey(key);
Object value = redisTemplate.opsForValue().get(redisKey);
redisTemplate.delete(redisKey);
return value;
}
@Override
public void clear() {
redisTemplate.delete(Objects.requireNonNull(redisTemplate.keys(getKey("*"))));
}
@Override
public int getSize() {
return 0;
}
private String getKey(Object key) {
return id + ":" + key.hashCode();
}
public Object isArray(String str) {
JSON json = JSONUtil.parse(str);
if (json instanceof cn.hutool.json.JSONArray) {
// 是数组
return JSONUtil.toList((cn.hutool.json.JSONArray) json, Object.class);
}
if (json instanceof cn.hutool.json.JSONObject) {
// 是对象
return JSONUtil.toBean((cn.hutool.json.JSONObject) json, Object.class);
}
throw new RuntimeException("缓存数据格式错误");
}
}
mapper xml
一定要加 cache-ref 才能进行二级缓存
<mapper namespace="com.example.dao.UserMapper">
<cache-ref namespace="com.example.dao.UserMapper"/>
<select id="selectAll" resultType="com.example.Demo">
select *
from demo
</select>
</mapper>
配置 CacheNamespace 使用就可以了
@CacheNamespace(implementation = MybatisRedisCache.class)
public interface UserMapper extends BaseMapper<Demo> {
List<Demo> selectAll();
}
Spring Cache
@Configuration
@EnableCaching
public class RedisCacheConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10)) // 默认缓存 10 分钟
.disableCachingNullValues() // 避免存入控制
.serializeValuesWith( // 设置序列化
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())
);
// 返回
return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}
}
Cacheable 缓存、CacheEvict 删除,拼接 key
@Service
public class DemoService extends ServiceImpl<UserMapper, Demo> {
@Cacheable(value = "user:cache", key = "#id")
public Demo getOne(Long id) {
return getById(id);
}
@Caching(evict = {
@CacheEvict(value = "user:cache", key = "#id"),
@CacheEvict(value = "user:all", allEntries = true)}
)
public void deleteById(String id) {
removeById(id);
}
// 更新数据后刷新缓存
@Caching(
put = {@CachePut(value = "user:cache", key = "#demo.id", unless = "#result == null")},
evict = {@CacheEvict(value = "user:all", allEntries = true)}
)
public void updateUser(Demo demo) {
updateById(demo);
}
@Cacheable(value = "user:all")
public List<Demo> listDemo() {
return list();
}
}
到此这篇关于浅析SpringBoot整合Mybatis如何实现二级缓存的文章就介绍到这了,更多相关SpringBoot Mybatis二级缓存内容请搜索QQ沐编程以前的文章或继续浏览下面的相关文章希望大家以后多多支持QQ沐编程!
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END