package
com.imooc.o2o.config.redis;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
com.imooc.o2o.cache.JedisPoolWriper;
import
com.imooc.o2o.cache.JedisUtil;
import
redis.clients.jedis.JedisPoolConfig;
/**
* spring-redis.xml里的配置
*
* @author xiangze
*
*/
@Configuration
public
class
RedisConfiguration {
@Value
(
"${redis.hostname}"
)
private
String hostname;
@Value
(
"${redis.port}"
)
private
int
port;
@Value
(
"${redis.pool.maxActive}"
)
private
int
maxTotal;
@Value
(
"${redis.pool.maxIdle}"
)
private
int
maxIdle;
@Value
(
"${redis.pool.maxWait}"
)
private
long
maxWaitMillis;
@Value
(
"${redis.pool.testOnBorrow}"
)
private
boolean
testOnBorrow;
@Autowired
private
JedisPoolConfig jedisPoolConfig;
@Autowired
private
JedisPoolWriper jedisWritePool;
@Autowired
private
JedisUtil jedisUtil;
/**
* 创建redis连接池的设置
*
* @return
*/
@Bean
(name =
"jedisPoolConfig"
)
public
JedisPoolConfig createJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig =
new
JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
return
jedisPoolConfig;
}
/**
* 创建Redis连接池,并做相关配置
*
* @return
*/
@Bean
(name =
"jedisWritePool"
)
public
JedisPoolWriper createJedisPoolWriper() {
JedisPoolWriper jedisPoolWriper =
new
JedisPoolWriper(jedisPoolConfig, hostname, port);
return
jedisPoolWriper;
}
/**
* 创建Redis工具类,封装好Redis的连接以进行相关的操作
*
* @return
*/
@Bean
(name =
"jedisUtil"
)
public
JedisUtil createJedisUtil() {
JedisUtil jedisUtil =
new
JedisUtil();
jedisUtil.setJedisPool(jedisWritePool);
return
jedisUtil;
}
/**
* Redis的key操作
*
* @return
*/
@Bean
(name =
"jedisKeys"
)
public
JedisUtil.Keys createJedisKeys() {
JedisUtil.Keys jedisKeys = jedisUtil.
new
Keys();
return
jedisKeys;
}
/**
* Redis的Strings操作
*
* @return
*/
@Bean
(name =
"jedisStrings"
)
public
JedisUtil.Strings createJedisStrings() {
JedisUtil.Strings jedisStrings = jedisUtil.
new
Strings();
return
jedisStrings;
}
}