温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring boot中mybatis的二级缓存怎么使用Redis集群进行替换

发布时间:2020-11-21 15:33:56 来源:亿速云 阅读:359 作者:Leah 栏目:编程语言

这篇文章给大家介绍Spring boot中mybatis的二级缓存怎么使用Redis集群进行替换,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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中mybatis的二级缓存怎么使用Redis集群进行替换就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI