springboot 2.0がredisをどのように統合するかについて、多くのチュートリアルを見て、表現があまり使いにくいです.ここは単機版です.


1.jarパッケージ


    org.springframework.boot
    spring-boot-starter-data-redis


    redis.clients
    jedis

2. application.yml
spring:
    redis:
      host: 127.0.0.1
      port: 6379
      #  redis    (  )
      timeout: 100ms
      jedis:
        pool:
          max-wait: 1000ms
          min-idle: 1
          max-idle: 10
          max-active: 1000

3.自分のjedisツールクラスに書き込む
public class JedisClientPool {
   @Autowired
   private JedisPool jedisPool;


   @Override
   public String set(String key, String value) {
      Jedis jedis = jedisPool.getResource();
      String result = jedis.set(key, value);
      jedis.close();
      return result;
   }

   @Override
   public String get(String key) {
      Jedis jedis = jedisPool.getResource();
      String result = jedis.get(key);
      jedis.close();
      return result;
   }

   @Override
   public Boolean exists(String key) {
      Jedis jedis = jedisPool.getResource();
      Boolean result = jedis.exists(key);
      jedis.close();
      return result;
   }

   @Override
   public Long expire(String key, int seconds) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.expire(key, seconds);
      jedis.close();
      return result;
   }

   @Override
   public Long ttl(String key) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.ttl(key);
      jedis.close();
      return result;
   }

   @Override
   public Long incr(String key) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.incr(key);
      jedis.close();
      return result;
   }

   @Override
   public Long hset(String key, String field, String value) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.hset(key, field, value);
      jedis.close();
      return result;
   }

   @Override
   public String hget(String key, String field) {
      Jedis jedis = jedisPool.getResource();
      String result = jedis.hget(key, field);
      jedis.close();
      return result;
   }

   @Override
   public Long hdel(String key, String... field) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.hdel(key, field);
      jedis.close();
      return result;
   }

   @Override
   public Boolean hexists(String key, String field) {
      Jedis jedis = jedisPool.getResource();
      Boolean result = jedis.hexists(key, field);
      jedis.close();
      return result;
   }

   @Override
   public List hvals(String key) {
      Jedis jedis = jedisPool.getResource();
      List result = jedis.hvals(key);
      jedis.close();
      return result;
   }

   @Override
   public Long del(String key) {
      Jedis jedis = jedisPool.getResource();
      Long result = jedis.del(key);
      jedis.close();
      return result;
   }

   public JedisPool getJedisPool() {
      return jedisPool;
   }

   public void setJedisPool(JedisPool jedisPool) {
      this.jedisPool = jedisPool;
   }
}

4.構成クラスの作成
@Configuration
public class JedisPoolFactory {

    //    redis      
    @Autowired
    private RedisProperties properties;

    @Bean
    public JedisClientPool getJedisClientPool() {
        JedisClientPool jedisClientPool = new JedisClientPool();
        jedisClientPool.setJedisPool(getJedisPool());
        return jedisClientPool;
    }

    @Bean
    public JedisPool getJedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(properties.getJedis().getPool().getMaxIdle());
        config.setMaxTotal(properties.getJedis().getPool().getMaxActive());
        config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis());
        JedisPool pool = new JedisPool(config, properties.getHost(), properties.getPort(), 100);
        return pool;
    }
}

5.okテスト
@Autowired
private JedisClientPool jedisClientPool;
jedisClientPool.set("SESSION:"+token,JsonUtils.objectToJson(users.get(0)));