springboot 使用jedis 集成 redis 实现 mybatis 二级缓存
第一步:pom.xml引入相关依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.6.RELEASE</version> </dependency>
注:mybatis 相关使用我在这里就不写了
第二步:编写RedisConfig 配置类
1 @Configuration 2 public class RedisConfig { 3 public JedisPoolConfig getRedisConfig(){ 4 JedisPoolConfig config = new JedisPoolConfig(); 5 config.setMaxIdle(8); 6 config.setMaxTotal(8); 7 config.setMaxWaitMillis(2); 8 config.setMinIdle(0); 9 return config; 10 } 11 @Bean(name = "jedisConnectionFactory") 12 public JedisConnectionFactory getConnectionFactory(){ 13 JedisConnectionFactory factory = new JedisConnectionFactory(getRedisConfig()); 14 factory.setHostName("127.0.0.1"); 15 factory.setPassword("123456"); 16 return factory; 17 } 18 }
第三步:编写RedisCacheTransfer 用以实例化JedisConnectionFactory
@Component public class RedisCacheTransfer { @Autowired public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { RedisCache.setJedisConnectionFactory(jedisConnectionFactory); } }
第四步:编写RedisCache 并且实现 Cache接口
import org.apache.ibatis.cache.Cache; //特意把cache包路径写出来防止你们导错包 public class RedisCache implements Cache { private static final Logger logger = LoggerFactory.getLogger(RedisCache.class); @Autowired private static JedisConnectionFactory jedisConnectionFactory; private final String id; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public RedisCache(final String id){ if (id == null){ throw new IllegalArgumentException("Cache instances require an Id"); } logger.debug("RedisCache id = {}", id); this.id = id; } @Override public String getId() { return this.id; } @Override public void putObject(Object key, Object value) { logger.debug("putObject Key = {} , value = {}",key,value); JedisConnection connection = null; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); connection.set(serializer.serialize(key),serializer.serialize(value)); }catch (JedisConnectionException e){ e.printStackTrace(); }finally { if (connection != null){ connection.close(); } } } @Override public Object getObject(Object key) { logger.debug("getObject key = {}",key); Object result = null; JedisConnection connection = null; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = serializer.deserialize(connection.getJedis().get(serializer.serialize(key))); }catch (JedisConnectionException e){ e.printStackTrace(); }finally { if (connection!=null){ connection.close(); } } return result; } @Override public Object removeObject(Object key) { logger.debug("removeObject key = {}",key); JedisConnection connection = null; Object result = null; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = connection.getJedis().exists(serializer.serialize(key)); }catch (JedisConnectionException e){ e.printStackTrace(); }finally { if (connection != null){ connection.close(); } } return result; } @Override public void clear() { logger.debug("clear"); JedisConnection connection = null; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); connection.getJedis().flushAll(); connection.getJedis().flushDB(); }catch (JedisConnectionException e){ e.printStackTrace(); }finally { if (connection != null){ connection.close(); } } } @Override public int getSize() { logger.debug("getSize"); int result = 0; JedisConnection connection = null; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); result = Integer.valueOf(connection.getJedis().dbSize().toString()); }catch (JedisConnectionException e){ e.printStackTrace(); }finally { connection.close(); } return result; } @Override public ReadWriteLock getReadWriteLock() { logger.debug("getReadWriteLock"); return this.readWriteLock; } public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory){ logger.debug("setJedisConnectionFactory"); RedisCache.jedisConnectionFactory = jedisConnectionFactory; } }
第五步:application.yml 内开启 mybatis 二级缓存
mybatis.configuration.cache-enabled: true
第六步:修改实体类实现 序列号接口
public class ResourceDO implements Serializable { //此处是成员变量以及get set方法 }
第七步:在mapping 中添加cache 标签
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qianxiao.blog.mapper.ResourceDOMapper"> <cache type="com.qianxiao.blog.cache.RedisCache" eviction="LRU" flushInterval="100000" readOnly="true" size="1024"></cache> <sql id="tableName"> resource </sql> <select id="findById" resultType="com.qianxiao.blog.dataobject.ResourceDO"> select * from <include refid="tableName" /> where id = #{id} order by id desc LIMIT 0,1 </select> </mapper>
注:想了解cache 里面各属性请查看我这一篇文章:https://www.cnblogs.com/qianxiaoPro/p/14201364.html
第八步:查看效果
redis内