Spring bootはRedisクラスタを使ってmybatis 2級キャッシュを置換します。


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を使って間接的にRedisConnection Factoryを注入します。

@Component 
public class RedisCacheTransfer {

@Autowired
public void setJedisConnectionFactory(
    RedisConnectionFactory jedisConnectionFactory) {
  RedisCache.setRedisConnectionFactory(jedisConnectionFactory);
}
}

4.appication.propretiesで2段階キャッシュを開く
mybatisの二級キャッシュを開きます。

spring.datasource.cachePrepStmts=true
5.注釈による使用

@CacheNamespace(implementation = RedisCache.class)
public interface ConfigDaoMapper {
  .....
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。