淺析SpringBoot整合Mybatis如何實現(xiàn)二級緩存
mybatis 原生
二級緩存,是指多個Sqlsession之間共享數(shù)據(jù),但是也可以使用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) { // 是數(shù)組 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("緩存數(shù)據(jù)格式錯誤"); } }
mapper xml
一定要加 cache-ref 才能進(jìn)行二級緩存
<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)) // 默認(rèn)緩存 10 分鐘 .disableCachingNullValues() // 避免存入控制 .serializeValuesWith( // 設(shè)置序列化 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); } // 更新數(shù)據(jù)后刷新緩存 @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(); } }
到此這篇關(guān)于淺析SpringBoot整合Mybatis如何實現(xiàn)二級緩存的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis二級緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用BigDecimal進(jìn)行運算封裝的實際案例
今天小編就為大家分享一篇關(guān)于Java使用BigDecimal進(jìn)行運算封裝的實際案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Spring?Boot實現(xiàn)分布式任務(wù)調(diào)度的步驟
Spring?Boot提供了一些工具和框架,可以幫助我們輕松地實現(xiàn)分布式任務(wù)調(diào)度,在本文中我們將介紹如何使用Spring?Boot、Spring?Cloud、Quartz和Redis來實現(xiàn)分布式任務(wù)調(diào)度,感興趣的朋友跟隨小編一起看看吧2023-06-06idea使用pagehelper實現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實現(xiàn)后端分頁功能的步驟,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09詳解SpringCloud-Alibaba-Seata分布式事務(wù)
這篇文章主要介紹了SpringCloud-Alibaba-Seata分布式事務(wù)的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Spring Boot如何優(yōu)雅的使用多線程實例詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot如何優(yōu)雅的使用多線程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05