這篇文章給大家介紹Spring boot中mybatis的二級緩存怎么使用redis集群進行替換,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了臺山免費建站歡迎大家使用!
1 . pom.xml添加相關(guān)依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <!-- 依賴 --> <dependencies> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- redis相關(guān) --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency> <dependencies>
2 . 配置Redis集群,參考spring-data-redis官方文檔
@Component @ConfigurationProperties(prefix = "spring.redis.cluster") public class ClusterConfigurationProperties { /* * spring.redis.cluster.nodes[0] = 127.0.0.1:7379 * spring.redis.cluster.nodes[1] = 127.0.0.1:7380 * ... */ List<String> nodes; /** * Get initial collection of known cluster nodes in format {@code host:port}. * * @return */ public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; } } @Configuration public class AppConfig { /** * Type safe representation of application.properties */ @Autowired ClusterConfigurationProperties clusterProperties; public @Bean RedisConnectionFactory connectionFactory() { return new JedisConnectionFactory( new RedisClusterConfiguration(clusterProperties.getNodes())); } }
3 . 自定義二級緩存類
public class RedisCache implements Cache { private static final String PREFIX = "SYS_CONFIG:"; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true); private String id; private JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(); private static RedisConnectionFactory redisConnectionFactory; public RedisCache(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) { RedisClusterConnection conn = redisConnectionFactory .getClusterConnection(); if (key == null) return; String strKey = PREFIX + key.toString(); conn.set(strKey.getBytes(), jdkSerializer.serialize(value)); conn.close(); } @Override public Object getObject(Object key) { if (key != null) { String strKey = PREFIX + key.toString(); RedisClusterConnection conn = redisConnectionFactory .getClusterConnection(); byte[] bs = conn.get(strKey.getBytes()); conn.close(); return jdkSerializer.deserialize(bs); } return null; } @Override public Object removeObject(Object key) { if (key != null) { RedisClusterConnection conn = redisConnectionFactory .getClusterConnection(); conn.del(key.toString().getBytes()); conn.close(); } return null; } @Override public void clear() { // 關(guān)鍵代碼,data更新時清理緩存 RedisClusterConnection conn = redisConnectionFactory .getClusterConnection(); Set<byte[]> keys = conn.keys((PREFIX+"*").getBytes()); for (byte[] bs : keys) { conn.del(bs); } conn.close(); } @Override public int getSize() { // TODO Auto-generated method stub return 0; } @Override public ReadWriteLock getReadWriteLock() { return this.readWriteLock; } public static void setRedisConnectionFactory(RedisConnectionFactory redisConnectionFactory) { RedisCache.redisConnectionFactory = redisConnectionFactory; } }
使用一個Transfer類間接注入RedisConnectionFactory
@Component public class RedisCacheTransfer { @Autowired public void setJedisConnectionFactory( RedisConnectionFactory jedisConnectionFactory) { RedisCache.setRedisConnectionFactory(jedisConnectionFactory); } }
4 . 在application.propreties中開啟二級緩存
開啟mybatis的二級緩存
spring.datasource.cachePrepStmts=true
5 . 基于注解的使用
@CacheNamespace(implementation = RedisCache.class) public interface ConfigDaoMapper { ..... }
關(guān)于Spring boot中mybatis的二級緩存怎么使用Redis集群進行替換就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前標題:Springboot中mybatis的二級緩存怎么使用Redis集群進行替換
文章路徑:http://jinyejixie.com/article4/ipicie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站營銷、品牌網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)