您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關mybatis plus中怎么將redis作為二級緩存,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
mybatis-plus.configuration.cache-enabled=true
@Bean(value = "redisTemplate") public RedisTemplate<string, object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<string, object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); //Use Jackson 2Json RedisSerializer to serialize and deserialize the value of redis (default JDK serialization) Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //將類名稱序列化到json串中 objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //設置輸入時忽略JSON字符串中存在而Java對象實際沒有的屬性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //Use String RedisSerializer to serialize and deserialize the key value of redis RedisSerializer redisSerializer = new StringRedisSerializer(); //key redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); //value redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }
package com.qctchina.headsetserver.config; import com.qctchina.headsetserver.util.SpringUtil; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.cache.Cache; import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * @author shuangyueliao * @create 2019/9/10 14:02 * @Version 0.1 */ @Slf4j public class MybatisRedisCache implements Cache { // 讀寫鎖 private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true); //這里使用了redis緩存,使用springboot自動注入 private RedisTemplate<string, object> redisTemplate; private String id; public MybatisRedisCache(final String id) { if (id == null) { throw new IllegalArgumentException("Cache instances require an ID"); } this.id = id; } @Override public String getId() { return this.id; } @Override public void putObject(Object key, Object value) { if (redisTemplate == null) { //由于啟動期間注入失敗,只能運行期間注入,這段代碼可以刪除 redisTemplate = (RedisTemplate<string, object>) SpringUtil.getBean("redisTemplate"); } if (value != null) { redisTemplate.opsForValue().set(key.toString(), value); } } @Override public Object getObject(Object key) { if (redisTemplate == null) { //由于啟動期間注入失敗,只能運行期間注入,這段代碼可以刪除 redisTemplate = (RedisTemplate<string, object>) SpringUtil.getBean("redisTemplate"); } try { if (key != null) { return redisTemplate.opsForValue().get(key.toString()); } } catch (Exception e) { e.printStackTrace(); log.error("緩存出錯 "); } return null; } @Override public Object removeObject(Object key) { if (redisTemplate == null) { //由于啟動期間注入失敗,只能運行期間注入,這段代碼可以刪除 redisTemplate = (RedisTemplate<string, object>) SpringUtil.getBean("redisTemplate"); } if (key != null) { redisTemplate.delete(key.toString()); } return null; } @Override public void clear() { log.debug("清空緩存"); if (redisTemplate == null) { redisTemplate = (RedisTemplate<string, object>) SpringUtil.getBean("redisTemplate"); } Set<string> keys = redisTemplate.keys("*:" + this.id + "*"); if (!CollectionUtils.isEmpty(keys)) { redisTemplate.delete(keys); } } @Override public int getSize() { if (redisTemplate == null) { //由于啟動期間注入失敗,只能運行期間注入,這段代碼可以刪除 redisTemplate = (RedisTemplate<string, object>) SpringUtil.getBean("redisTemplate"); } Long size = redisTemplate.execute((RedisCallback<long>) RedisServerCommands::dbSize); return size.intValue(); } @Override public ReadWriteLock getReadWriteLock() { return this.readWriteLock; } }
SpringUtil是手動獲取bean的工具類
@Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } public static Object getBean(String name){ return applicationContext.getBean(name); } public static <t> T getBean(String name, Class<t> clazz){ return applicationContext.getBean(name, clazz); } public static <t> T getBean(Class<t> clazz){ return applicationContext.getBean(clazz); } }
@CacheNamespace(implementation= MybatisRedisCache.class,eviction=MybatisRedisCache.class) public interface CommonMapper extends BaseMapper<common> {
看完上述內容,你們對mybatis plus中怎么將redis作為二級緩存有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。