原文:http://blog.csdn.net/centre10/article/details/50761815
http://my.oschina.net/u/866380/blog/521658 (切片方式的带密码、数据库方式的访问)
http://www.cnblogs.com/zr520/p/5062025.html (spring 使用redis集群配置)
要用redis ,则必须添加相关的包
jedis-2.7.2.jar
commons-pool2-2.0.jar
1、spring 和 redis 的非切片方式的配置
redis.properites配置如下:
redis.hostName=172.168.48.197
redis.port=6379
redis.timeout=3000
redis.password=chbigdata
redis.database=1
jedis.pool.maxTotal=300
jedis.pool.maxIdle=300
jedis.pool.maxWaitMillis=100000
jedis.pool.testOnBorrow=true
jedis.pool.testOnReturn=true
java 类中,jedis 2.7.2版本中的释放资源jedis与2.6之前的版本有区别,示例:
/**
*
*
说明:存入对象到缓存
*
时间:2016-06-27 14:21:41
* @param key
* 键
* @param object
* 对象
* @return boolean
*/
public boolean putObject(String key, Object object) {
boolean flag = false;
if (StringUtils.isBlank(key)) {
return flag;
}
if (object == null) {
return flag;
}
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String result = jedis.set(key, JSONObject.toJSONString(object, SerializerFeature.WriteClassName));
if ("ok".equalsIgnoreCase(result)) {
flag = true;
}
} catch (Exception e) {
logger.error("Redis putObject error" + e.getMessage(), e);
}finally {
if (jedis != null) {
try {
jedis.close();
}catch (Exception e) {
logger.error("Jedis putObject method close error" + e.getMessage(), e);
}
}
}
return flag;
}
2、哨兵方式sentinels的集群配置方式请看文章:http://www.cnblogs.com/zr520/p/5062025.html
3、下边来说说spring 和 redis 集群的配置(高可用配置,配置2个实例,则spring默认是启用第1个实例,当第1个实例不可用时,再启用第2个实例;)
将以下XML配置引入spring
配置写法1:与下边配置写法2任意选一种即可,我选的此种方式
配置写法2:其实就是上边的一个变种
properties配置文件:
#redis配置
redis.slaver.host=127.0.0.1
redis.slaver.port=6379
redis.master.host=127.0.0.2
redis.master.port=6379
将shardedJedisPool注入相关的工具类中即可使用
import java.util.ArraygeiList;ge
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
*
标题:
*
描述:
*
Copyright:Copyright(c) 2015 yjph83
*
日期:2016年6月27日
* @author yjph83
*/
public class JedisUtil {
private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
@Resource
private ShardedJedisPool shardedJedisPool;
/**
* 设置一个key的过期时间(单位:秒)
*
* @param key
* key值
* @param seconds
* 多少秒后过期
* @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间
*/
public long expire(String key, int seconds) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.expire(key, seconds);
} catch (Exception ex) {
logger.error("shardedJedis expire error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis expire method close error" + e.getMessage(), e);
}
}
return 0;
}
/**
* 设置一个key在某个时间点过期
*
* @param key
* key值
* @param unixTimestamp
* unix时间戳,从1970-01-01 00:00:00开始到现在的秒数
* @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间
*/
public long expireAt(String key, int unixTimestamp) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.expireAt(key, unixTimestamp);
} catch (Exception ex) {
logger.error("shardedJedis expireAt error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis expireAt method close error" + e.getMessage(), e);
}
}
return 0;
}
/**
* 截断一个List
*
* @param key
* 列表key
* @param start
* 开始位置 从0开始
* @param end
* 结束位置
* @return 状态码
*/
public String trimList(String key, long start, long end) {
if (StringUtils.isBlank(key)) {
return "-";
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.ltrim(key, start, end);
} catch (Exception ex) {
logger.error("shardedJedis trimList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis trimList method close error" + e.getMessage(), e);
}
}
return "-";
}
/**
* 检查Set长度
*
* @param key
* @return
*/
public long countSet(String key) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.scard(key);
} catch (Exception ex) {
logger.error("shardedJedis countSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countSet method close error " + e.getMessage(), e);
}
}
return 0;
}
/**
* 添加到Set中(同时设置过期时间)
*
* @param key
* key值
* @param seconds
* 过期时间 单位s
* @param value
* @return
*/
public boolean addSet(String key, int seconds, String... value) {
boolean result = addSet(key, value);
if (result) {
long i = expire(key, seconds);
return i == 1;
}
return false;
}
/**
* 添加到Set中
*
* @param key
* @param value
* @return
*/
public boolean addSet(String key, String... value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value.length == 0 || value == null){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.sadd(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis addSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* @param key
* @param value
* @return 判断值是否包含在set中
*/
public boolean containsInSet(String key, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (StringUtils.isBlank(value)){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.sismember(key, value);
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis containsInSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获取Set
*
* @param key
* @return
*/
public Set
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.smembers(key);
} catch (Exception ex) {
logger.error("shardedJedis getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 从set中删除value
*
* @param key
* @return
*/
public boolean removeSetValue(String key, String... value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value.length == 0 || value == null){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.srem(key, value);
return true;
} catch (Exception ex) {
logger.error("getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis removeSetValue method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 从list中删除value 默认count 1
*
* @param key
* @param values
* 值list
* @return
*/
public int removeListValue(String key, List
return removeListValue(key, 1, values);
}
/**
* 从list中删除value
*
* @param key
* @param count
* @param values
* 值list
* @return
*/
public int removeListValue(String key, long count, List
if (StringUtils.isBlank(key)) {
return 0;
}
if (values.size() == 0 || values == null){
return 0;
}
int result = 0;
if (values != null && values.size() > 0) {
for (String value : values) {
if (removeListValue(key, count, value)) {
result++;
}
}
}
return result;
}
/**
* 从list中删除value
*
* @param key
* @param count
* 要删除个数
* @param value
* @return
*/
public boolean removeListValue(String key, long count, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.lrem(key, count, value);
return true;
} catch (Exception ex) {
logger.error("getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis removeListValue method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 截取List
*
* @param key
* @param start
* 起始位置
* @param end
* 结束位置
* @return
*/
public List
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.lrange(key, start, end);
} catch (Exception ex) {
logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis rangeList method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 检查List长度
*
* @param key
* @return
*/
public long countList(String key) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.llen(key);
} catch (Exception ex) {
logger.error("countList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countList method close error." + e.getMessage(), e);
}
}
return 0;
}
/**
* 添加到List中(同时设置过期时间)
*
* @param key
* key值
* @param seconds
* 过期时间 单位s
* @param ... value
* @return
*/
public boolean addList(String key, int seconds, String... value) {
boolean result = addList(key, value);
if (result) {
long i = expire(key, seconds);
return i == 1;
}
return false;
}
/**
* 添加到List
*
* @param key
* @param ... value
* @return
*/
public boolean addList(String key, String... value) {
if (key == null || value == null) {
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.lpush(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis addList method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 添加到List(只新增)
*
* @param key
* @param List values
* @return
*/
public boolean addList(String key, List
if (StringUtils.isBlank(key) || list == null || list.size() == 0) {
return false;
}
for (String value : list) {
addList(key, value);
}
return true;
}
/**
* 获取List
*
* @param key
* @return
*/
public List
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.lrange(key, 0, -1);
} catch (Exception ex) {
logger.error("shardedJedis getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getList method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 设置HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @param value
* Json String or String value
* @return
*/
public boolean setHSet(String domain, String key, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value == null)
return false;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.hset(domain, key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis setHSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获得HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @return Json String or String value
*/
public String getHSet(String domain, String key) {
if (StringUtils.isBlank(key))
return null;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.hget(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis getHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getHSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 删除HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @return 删除的记录数
*/
public long delHSet(String domain, String key) {
long count = 0;
if (StringUtils.isBlank(key)) {
return count;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
count = shardedJedis.hdel(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis delHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delHSet method close error." + e.getMessage(), e);
}
}
return count;
}
/**
* 删除HashSet对象
*
* @param domain
* 域名
* @param ... key
* 键值
* @return 删除的记录数
*/
public long delHSet(String domain, String... key) {
long count = 0;
if (key.length == 0 || key == null) {
return count;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
count = shardedJedis.hdel(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis delHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delHSet method close error." + e.getMessage(), e);
}
}
return count;
}
/**
* 判断key是否存在
*
* @param domain
* 域名
* @param key
* 键值
* @return
*/
public boolean existsHSet(String domain, String key) {
boolean isExist = false;
if (StringUtils.isBlank(key)) {
return isExist;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
isExist = shardedJedis.hexists(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis existsHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis existsHSet method close error." + e.getMessage(), e);
}
}
return isExist;
}
/**
* 全局扫描hset
*
* @param match
* field匹配模式
* @return
*/
public List
ShardedJedis shardedJedis = null;
try {
int cursor = 0;
shardedJedis = shardedJedisPool.getResource();
ScanParams scanParams = new ScanParams();
scanParams.match(match);
Jedis jedis = shardedJedis.getShard(domain);
ScanResult
List
do {
scanResult = jedis.hscan(domain, String.valueOf(cursor),
scanParams);
list.addAll(scanResult.getResult());
cursor = Integer.parseInt(scanResult.getStringCursor());
} while (cursor > 0);
return list;
} catch (Exception ex) {
logger.error("shardedJedis scanHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis scanHSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 返回 domain 指定的哈希集中所有字段的value值
*
* @param domain
* @return
*/
public List
ShardedJedis shardedJedis = null;
List
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hvals(domain);
} catch (Exception ex) {
logger.error("shardedJedis hvals error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis hvals method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 返回 domain 指定的哈希集中所有字段的key值
*
* @param domain
* @return
*/
public Set
ShardedJedis shardedJedis = null;
Set
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hkeys(domain);
} catch (Exception ex) {
logger.error("shardedJedis hkeys error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis hkeys method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 返回 domain 指定的哈希key值总数
*
* @param domain
* @return
*/
public long lenHset(String domain) {
ShardedJedis shardedJedis = null;
long retList = 0;
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hlen(domain);
} catch (Exception ex) {
logger.error("shardedJedis lenHset error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis lenHset method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 设置指定位置的String类型的值排序集合
*
* @param key
* @param score
* @param value
* @return
*/
public boolean setSortedSet(String key, long score, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.zadd(key, score, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setSortedSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis setSortedSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 根据String类型的key,起始、结束记录位置获得排序集合
*
* @param key
* @param startScore
* @param endScore
* @param orderByDesc
* @return
*/
public Set
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
if (orderByDesc) {
return shardedJedis.zrevrangeByScore(key, endScore, startScore);
} else {
return shardedJedis.zrangeByScore(key, startScore, endScore);
}
} catch (Exception ex) {
logger.error("shardedJedis getSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSoredSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 根据String类型的key,起始、结束记录位置计算排序长度
*
* @param key
* @param startScore
* @param endScore
* @return
*/
public long countSoredSet(String key, long startScore, long endScore) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
Long count = shardedJedis.zcount(key, startScore, endScore);
return count == null ? 0L : count;
} catch (Exception ex) {
logger.error("shardedJedis countSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countSoredSet method close error." + e.getMessage(), e);
}
}
return 0L;
}
/**
* 根据String类型的key和value删除排序集合
*
* @param key
* @param value
* @return
*/
public boolean delSortedSet(String key, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
long count = shardedJedis.zrem(key, value);
return count > 0;
} catch (Exception ex) {
logger.error("shardedJedis delSortedSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delSortedSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获得排序集合
*
* @param key
* @param startRange
* @param endRange
* @param orderByDesc
* @return
*/
public Set
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
if (orderByDesc) {
return shardedJedis.zrevrange(key, startRange, endRange);
} else {
return shardedJedis.zrange(key, startRange, endRange);
}
} catch (Exception ex) {
logger.error("shardedJedis getSoredSetByRange error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSoredSetByRange method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 获得排序打分
*
* @param key
* @return
*/
public Double getScore(String key, String member) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.zscore(key, member);
} catch (Exception ex) {
logger.error("shardedJedis getSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getScore method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 存入String的值,并设置过期时间
* @param key
* @param value
* @param second 秒
* @return
*/
public boolean set(String key, String value, int second) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.setex(key, second, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis set error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis set method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 存入String 类型的值
* @param key
* @param value
* @return
*/
public boolean set(String key, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.set(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis set error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis set method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获取值,如果值为空,则可以指定返回值
* @param key
* @param defaultValue
* @return
*/
public String get(String key, String defaultValue) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.get(key) == null ? defaultValue : shardedJedis.get(key);
} catch (Exception ex) {
logger.error("shardedJedis get error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis get method close error." + e.getMessage(), e);
}
}
return defaultValue;
}
/**
* 根据key来删除值
* @param key
* @return
*/
public boolean del(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.del(key);
return true;
} catch (Exception ex) {
logger.error("shardedJedis del error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis del method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 名称为key的string加1操作
* @param key
* @return
*/
public long incr(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.incr(key);
} catch (Exception ex) {
logger.error("shardedJedis incr error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis incr method close error." + e.getMessage(), e);
}
}
return 0;
}
/**
* 名称为key的string减1操作
* @param key
* @return
*/
public long decr(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.decr(key);
} catch (Exception ex) {
logger.error("shardedJedis decr error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis decr method close error." + e.getMessage(), e);
}
}
return 0;
}
}