关于MyBatis Plus的缓存机制
MyBatis Plus有两级缓存 前段时间差点被他害死 也就是缓存带来的经典问题—脏读
Mybatis 缓存
MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大地 提升查询效率。
一级缓存-MyBatis默认打开一级缓存、不允许关闭
一级缓存:也称为本地缓存,基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为SqlSession,用于保存用户在一次会话过程中查询的结果,用户一次会话中只能使用一个sqlSession,各个SqlSession之间的缓存相互隔离,当 Session flush 或 close 之后,该 SqlSession 中的所有 Cache 就将清空,MyBatis默认打开一级缓存、不允许关闭。
二级缓存(默认是开启)
注意:二级缓存的作用域不然更新了数据,还是使用查询到缓存的数据)
二级缓存(默认是开启)
也称为全局缓存,是mapper级别的缓存。二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap 存储,所以默认也是本地缓存。不同之处在于其存储作用域为 Mapper(Namespace),可以在多个SqlSession之间共享,是针对一个表的查结果的存储,可以共享给所有针对这张表的查询的用户。也就是说对于mapper级别的缓存不同的sqlsession是可以共享的,并且可自定义存储源,如 Ehcache、Redis。默认开启二级缓存,但是还需要配置才可以使用。
如果某个SQL不想被缓存,可以单独处理一下:
1、SQL走的是xml文件查询:配置useCache=“false”
2、SQL走的是注解形式:@Options(useCache=false)
解决脏数据问题-(没法解决,不使用二级缓存,采用第三方缓存redis)
先改yaml
mybatis-plus: #开启二级缓存,使用redis配置 cache-enabled: true
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * @class: RedisConfiguration * redis的配置文件,template对映序列化规则,以及混村有效时间的设置 */ public class RedisConfiguration { @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl( Duration.ofHours(1)); // 设置缓存有效期一小时 return RedisCacheManager .builder( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig(Thread.currentThread().getContextClassLoader())).build(); } @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; } }
再进行自定义缓存管理↓
import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.cache.Cache; import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; import org.springframework.util.CollectionUtils; import java.util.HashSet; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * @class: MybatisRedisCache */ @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"); } try { Set<String> keys = scan(this.id); if (!CollectionUtils.isEmpty(keys)) { redisTemplate.delete(keys); } } catch (Exception e) { log.error("清空缓存", e); } } public Set<String> scan(String matchKey) { if (redisTemplate == null) { redisTemplate = (RedisTemplate<String, Object>) SpringUtil.getBean("redisTemplate"); } Set<String> keys = redisTemplate.execute((RedisCallback<Set<String>>) connection -> { Set<String> keysTmp = new HashSet<>(); Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match("*" + matchKey + "*").count(1000).build()); while (cursor.hasNext()) { keysTmp.add(new String(cursor.next())); } return keysTmp; }); return 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; } }
Spring工具类
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @class: SpringUtil * 工具类 */ @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); } }
在mapper层添加注解
Java //redis缓存注解 @CacheNamespace(implementation= MybatisRedisCache.class,eviction= MybatisRedisCache.class) public interface UserMapper extends BaseMapper { }
在启动类添加注解
Java @EnableCaching
这样 这辈子都用不到 MyBatis的二级缓存啦哈哈哈哈哈!