详解Spring boot使用Redis集群替换mybatis二级缓存
程序员文章站
2023-12-20 16:24:34
1 . pom.xml添加相关依赖
org.springframework.boot&l...
1 . pom.xml添加相关依赖
<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相关 --> <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() { // 关键代码,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 { ..... }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。