Flask接口签名sign原理与实例代码浅析
257
2023-05-11
详解Spring boot使用Redis集群替换mybatis二级缓存
1 . pom.xml添加相关依赖
2 . 配置Redis集群,参考spring-data-redis官方文档
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {
/*
* shttp://pring.redis.cluster.nodes[0] = 127.0.0.1:7379
* spring.redis.cluster.nodes[1] = 127.0.0.1:738http://0
* ...
*/
List
/**
* Get initial collection of known cluster nodes in format {@code host:port}.
*
* @return
*/
public List
return nodes;
}
public void setNodes(List
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
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中开启二级缓存
开启mybahttp://tis的二级缓存
spring.datasource.cachePrepStmts=true
5 . 基于注解的使用
@CacheNamespace(implementation = RedisCache.class)
public interface ConfigDaoMapper {
.....
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~