加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

详解Spring boot使用Redis集群替换mybatis二级缓存

发布时间:2020-12-14 20:03:15 所属栏目:Java 来源:网络整理
导读:1 . pom.xml添加相关依赖 parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version1.5.1.RELEASE/version/parent !-- 依赖 -- dependencies !-- mybatis -- dependency groupIdorg.mybatis.spring.boot/gr

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 {
  .....
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • 实例详解Spring Boot实战之Redis缓存登录验证码
  • 详解SpringBoot集成Redis来实现缓存技术方案
  • Spring Boot中使用Redis做缓存的方法实例
  • Spring Boot 基于注解的 Redis 缓存使用详解
  • 详解Spring Boot使用redis实现数据缓存
  • Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)
  • SpringBoot使用Redis缓存的实现方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读