参考:
Tomcat7+Redis存储Session http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831
测试类
final Set
sentinels.add("10.139.5.180:26379");
sentinels.add("10.139.5.181:26379");
sentinels.add("10.139.5.232:26379");
final JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(50);
config.setMaxTotal(500);
config.setTimeBetweenEvictionRunsMillis(30000);
config.setMinEvictableIdleTimeMillis(1800000);
config.setSoftMinEvictableIdleTimeMillis(1800000);
config.setMaxWaitMillis(1500);
config.setTestOnBorrow(true);
config.setTestWhileIdle(false);
config.setTestOnReturn(false);
config.setBlockWhenExhausted(false);
final JedisSentinelPool pool = new JedisSentinelPool("mymaster",
sentinels, config);
final Jedis jedis = pool.getResource();
jedis.set("jedis123", "jedi12s");
spring配置
spring配置 对应的代码
@Component
public class RedisClient extends RedisCache {
@Resource(name = "shardedJedisPool")
@Override
protected void setPool(final JedisSentinelPool pool) {
super.pool = pool;
}
}
public abstract class RedisCache implements Cache {
private static final Logger LOGGER = LoggerFactory
.getLogger(RedisClient.class);
protected JedisSentinelPool pool;
protected abstract void setPool(JedisSentinelPool pool);
private Jedis getResource() {
Jedis jedis;
try {
jedis = pool.getResource();
} catch (final Exception e) {
throw e;
}
return jedis;
}
public String set(final String key, final String value, final String nxxx,
final String expx, final long time) {
if (StringUtils.isEmpty(key) || isNull(value)) {
return null;
}
Jedis jedis = null;
try {
jedis = getResource();
return jedis.set(key, value, nxxx, expx, time);
} catch (final Exception e) {
LOGGER.error("RedisClient-set", e);
throw e;
} finally {
jedis.close();
}
}
public String get(final String key) {
if (StringUtils.isEmpty(key)) {
return null;
}
Jedis jedis = null;
try {
jedis = getResource();
return jedis.get(key);
} catch (final Exception e) {
LOGGER.error("RedisClient-get", e);
throw e;
} finally {
jedis.close();
}
}
}
public interface Cache {
/**
* 获取 缓存
* 从缓存中根据key取得其String类型的值,如果key不存在则返回null,如果key存在但value不是string类型的,
* 则返回一个error。这个方法只能从缓存中取得value为string类型的值。
*
* @param key
* @return String
*/
String get(final String key);
/**
* 存入 缓存,不存在创建,存在则覆盖
* 存入类型:String
*
* @param key
* @param value
* @return String
*/
String set(final String key, final String value);
/**
* 检查某个key是否在缓存中存在,如果存在返回true,否则返回false;需要注意的是,即使该key所对应的value是一个空字符串,
* 也依然会返回true。
*
* @param key
* @return boolean
*/
boolean exists(final String key);
/**
* 删除一个Key,如果删除的key不存在,则直接忽略。
*
* @param key
* @return 被删除的keys的数量
*/
Long del(final String key);
}