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

