SpringBoot学習ノート(8)-Redisの統合

23109 ワード

文書ディレクトリ @[toc] 一、build.gradle 二、アプリケーションproperties 三、コントローラ 共通ツールクラス


バージョンの理由でSpringBoot 2.0統合Redisは、低バージョンのSpringBootとは異なり、本明細書ではこの統合スキームを使用することができます.
SpringBootの詳細については、「SpringBoot使用概要」をクリックしてください.

一、build.gradle

//redis clienet
	compile("redis.clients:jedis:2.9.0")
	//commons pool
	compile("org.apache.commons:commons-pool2:2.6.0")
	//redis starter
	compile("org.springframework.boot:spring-boot-starter-redis:2.0.4.RELEASE")
	//redis data
	compile("org.springframework.data:spring-data-redis:2.0.5.RELEASE")

二、アプリケーションproperties

# Redis     (   0)
spring.redis.database=0
# Redis     
spring.redis.host=localhost
# Redis       
spring.redis.port=6379
# Redis       (    )
spring.redis.password=
#         (          )
spring.redis.jedis.pool.max-active=8
#            (          )
spring.redis.jedis.pool.max-wait=-1
#            
spring.redis.jedis.pool.max-idle=8
#            
spring.redis.jedis.pool.min-idle=0
#       (  )
spring.redis.timeout=1000

SpringBootバージョンが1.5の場合spring.redis.jedis.pool.max-idlはspringと書きます.redis.pool.max-idl

三、コントローラ

 @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping(value="/redis")
    @ResponseBody
    public String redis(){
        System.out.println("hello");
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String hello = ops.get("hello");
        ops.set("redisTest","hello Redis",10000);
        return hello;
    }

以上の構成はすべてテスト済み

共通ツールクラス


@Slf4j
@Component
public class RedisUtil {
   
   		 @Autowired
        private StringRedisTemplate redisTemplate;

        /**
         *   redis  (   expire    )
         * @param key
         * @param value
         * @return
         */
        public boolean set(final String key, String value){
            boolean result = false;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                operations.set(key, value);
                result = true;
            } catch (Exception e) {
                log.error("  redis    !     :" + e.getMessage());
            }
            return result;
        }

        /**
         *   redis  (  expire    )
         * @param key
         * @param value
         * @param expire      
         * @return
         */
        public boolean set(final String key, String value, Long expire){
            boolean result = false;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                operations.set(key, value);
                redisTemplate.expire(key, expire, TimeUnit.MILLISECONDS);
                result = true;
            } catch (Exception e) {
                log.error("  redis  (  expire    )  !     :" + e.getMessage());
            }
            return result;
        }


        /**
         *   redis  
         * @param key
         * @return
         */
        public Object get(final String key){
            Object result = null;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                result = operations.get(key);
            } catch (Exception e) {
                log.error("  redis    !     :" + e.getMessage());
            }
            return result;
        }

        /**
         *   redis         key
         * @param key
         * @return
         */
        public boolean exists(final String key){
            boolean result = false;
            try {
                result = redisTemplate.hasKey(key);
            } catch (Exception e) {
                log.error("  redis         key  !     :" + e.getMessage());
            }
            return result;
        }

        /**
         * redis  key     value
         * @param key
         * @return
         */
        public boolean remove(final String key){
            boolean result = false;
            try {
                if(exists(key)){
                    redisTemplate.delete(key);
                }
                result = true;
            } catch (Exception e) {
                log.error("redis  key     value  !     :" + e.getMessage());
            }
            return result;
        }

        /**
         * redis  keys       value
         * @param keys
         * @return
         */
        public void remove(final List<String> keys){
            for(String key : keys){
                remove(key);
            }
        }


    /**
     * hash    redis  (           )
     * @param key
     * @param value
     * @return
     */
    public boolean hset(String hkey,String key, String value){
        boolean result = false;
        try {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            hashOperations.put(hkey,key,value);
            result = true;
        } catch (Exception e) {
            log.error("  redis    !     :" + e.getMessage());
        }
        return result;
    }

    /**
     *   hash  
     * @param key
     * @param value
     * @return
     */
    public Object hget(String hkey,String key){
        Object result = null;
        try {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            Boolean aBoolean = hashOperations.hasKey(hkey, key);
            if (!aBoolean) {
                return null;
            }
            Object response = hashOperations.get(hkey, key);
            result = response;
        } catch (Exception e) {
            log.error("  redis    !     :" + e.getMessage());
        }
        return result;
    }

}