【22】redis 의 jedisPool 패키지(주해 실현)

4414 단어 redis
spring boot 를 사용 한 후 spring 의 xml 설정 파일 이 없 으 면@주석 으로 바 꿉 니 다.
위 편 은 크게 다 르 지 않 습 니 다.설정 파일 을 사용 하 시 겠 습 니까?주석 을 사용 하 시 겠 습 니까?redis 클 러 스 터 를 사용 하 시 겠 습 니까?클 러 스 터 를 사용 하 시 겠 습 니까?
RedisPool.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description: RedisPool    
 **/
@Configuration
public class RedisPool {
    Logger logger = LoggerFactory.getLogger(RedisPool.class);

    @Value("${redis.host}")
    private  String host ;
    @Value("${redis.auth}")
    private  String auth ;
    @Value("${redis.maxIdle}")
    private  int maxIdle;

    @Value("${redis.maxTotal}")
    private  int maxTotal;

    @Value("${redis.port}")
    private  int port ;
    @Value("${redis.timeout}")
    private  int timeout;

    @Bean
    public JedisPool redisPoolFactory() {
        logger.info("JedisPool    !!");
        logger.info("redis  :" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        //jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, auth);

        return jedisPool;
    }


}

RedisService.java

public interface RedisService {
    String setex(String key,String token,int seconds);
    String get(String key);
    long del(String key);
}

RedisServiceImpl.java

import com.base.util.RedisPool;
import com.base.util.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @description:
 **/
@Service
public class RedisServiceImpl implements RedisService {

    Logger logger = LoggerFactory.getLogger(RedisPool.class);

    @Autowired
    JedisPool jedisPool;

    @Override
    public String setex(String key, String token, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.setex(key,seconds,token);

        } catch (Exception e) {
            logger.error("JedisUtil setex:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }

        }
        return null;
    }

    @Override
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);

        } catch (Exception e) {

            logger.error("JedisUtil get:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }
        }
        return  null;
    }

    @Override
    public long del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            logger.error("JedisUtil del:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }
        }
        return 0;
    }
}

application.yml
server:
  port: 8080
  context-path: /sid

redis:
  host: 192.168.2.240
  auth: 123123
  port: 6379
  maxIdle: 200
  maxTotal: 300
  timeout: 3000


쓰다
@Autowired
RedisService redisService ;

redisService.setex("redis:key","testvalue",5000);

좋은 웹페이지 즐겨찾기