1. 程式人生 > >SpringBoot-Redis之通過spring注入JedisPool

SpringBoot-Redis之通過spring注入JedisPool

springboot驅動註解,使用spring注入JedisPool便可封裝自己的redis工具類。這種方式通過jedisPool,以後講通過spring data redis實現。

  1. springBoot驅動註解:
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import
org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * 類名:RedisCacheConfiguration<br> * 描述:<br> * 建立人:<br> * 建立時間:2016/9/6 17:33<br> * * @version
v1.0 */
@Configuration @EnableCaching public class RedisCacheConfiguration extends CachingConfigurerSupport { Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value
("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.password}") private String password; @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, password); return jedisPool; } }
  • redis配置檔案:
# REDIS (RedisProperties)
# Redis資料庫索引(預設為0)
spring.redis.database=0
# Redis伺服器地址
spring.redis.host=192.168.1.80
# Redis伺服器連線埠
spring.redis.port=6379
# Redis伺服器連線密碼(預設為空)
spring.redis.password=redis
# 連線池最大連線數(使用負值表示沒有限制)
spring.redis.pool.max-active=8
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1
# 連線池中的最大空閒連線
spring.redis.pool.max-idle=8
# 連線池中的最小空閒連線
spring.redis.pool.min-idle=0
# 連線超時時間(毫秒)
spring.redis.timeout=0
  • 測試:
@Autowired
JedisPool jedisPool;

String uuid = UUID.randomUUID().toString() 
Jedis jedis = jedisPool.getResource()       
jedis.setex("ssssssss", 1000, uuid);
  • JedisPool簡單 進行封裝:
public class JedisClientSingle {

    @Autowired
    private JedisPool jedisPool;

    //獲取key的value值
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String str = "";
        try {
             str = jedis.get(key);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String str = "";
        try {
             str = jedis.set(key, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    public String setex(String key, int seconds, String value) {
        Jedis jedis = jedisPool.getResource();
        String str = "";
        try {
             str = jedis.setex(key, seconds, value);
        } finally {
            try {
                jedis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str;
    }
}