使用jedis操作redis
pom文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
redis的key生成规则
public class RedisKeyUtil {
public static String keyBuilder(String table, String primaryKey, String... args) {
StringBuilder stringBuilder = new StringBuilder(table).append(":");
stringBuilder.append(primaryKey).append(":");
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (i != (args.length-1)) {
stringBuilder.append(arg).append(":");
}else {
stringBuilder.append(arg);
}
}
return stringBuilder.toString();
}
}
util包
package com.hzdf.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class RedisUtils {
private final static Logger log = LoggerFactory.getLogger(RedisUtils.class);
@Autowired
private JedisPool jedisPool;
public static Map<String, String> mapToStrAll(Map<String, Object> map){
if(map != null){
Map<String, String> returnMap = new HashMap<>();
for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
if (stringObjectEntry.getValue() == null) {
returnMap.put(stringObjectEntry.getKey(),"");
}else{
returnMap.put(stringObjectEntry.getKey(),String.valueOf(stringObjectEntry.getValue()));
}
}
return returnMap;
}else {
return null;
}
}
public static Map<String, Object> mapToObjAll(Map<String, String> map){
if(map != null){
Map<String, Object> returnMap = new HashMap<>();
for (Map.Entry<String, String> stringObjectEntry : map.entrySet()) {
if (stringObjectEntry.getValue() == null) {
returnMap.put(stringObjectEntry.getKey(),"");
}else{
returnMap.put(stringObjectEntry.getKey(),String.valueOf(stringObjectEntry.getValue()));
}
}
return returnMap;
}else {
return null;
}
}
public String get(String key, int indexdb) {
Jedis jedis = null;
String value = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
value = jedis.get(key);
log.info(value);
} catch (Exception e) {
log.error("redis:",e);
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return value;
}
public byte[] get(byte[] key,int indexdb) {
Jedis jedis = null;
byte[] value = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
value = jedis.get(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return value;
}
public String set(String key, String value,int indexdb) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage());
log.error("",e);
return "0";
} finally {
returnResource(jedisPool, jedis);
}
}
public String set(byte[] key, byte[] value,int indexdb) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return "0";
} finally {
returnResource(jedisPool, jedis);
}
}
public Long del(String... keys) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.del(keys);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long del(int indexdb,String... keys) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.del(keys);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long del(int indexdb,byte[]... keys) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.del(keys);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long append(String key, String str) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.append(key, str);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage());
return false;
} finally {
returnResource(jedisPool, jedis);
}
}
public String flushDB() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.flushDB();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public Long expire(String key, int value, int indexdb) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.expire(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long ttl(String key,int indexdb) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
return jedis.ttl(key);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long persist(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.persist(key);
} catch (Exception e) {
log.error(e.getMessage());
return -1L;
} finally {
returnResource(jedisPool, jedis);
}
}
public String setex(String key, int seconds, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public Long setnx(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setnx(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public String getSet(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long setrange(String key, String str, int offset) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setrange(key, offset, str);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public List<String> mget(String... keys) {
Jedis jedis = null;
List<String> values = null;
try {
jedis = jedisPool.getResource();
values = jedis.mget(keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return values;
}
public String mset(String... keysvalues) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.mset(keysvalues);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long msetnx(String... keysvalues) {
Jedis jedis = null;
Long res = 0L;
try {
jedis = jedisPool.getResource();
res = jedis.msetnx(keysvalues);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String getset(String key, String value) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String getrange(String key, int startOffset, int endOffset) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long incr(String key, int indexdb) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.incr(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long incrBy(String key, Long integer, int indexdb) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.incrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long decr(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.decr(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long decrBy(String key, Long integer) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.decrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long serlen(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.strlen(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long hset(String key, String field, String value) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hset(key, field, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long hsetnx(String key, String field, String value) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hsetnx(key, field, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String hmset(String key, Map<String, String> hash, int indexdb) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.hmset(key, hash);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String hget(String key, String field) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hget(key, field);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public List<String> hmget(String key, int indexdb, String... fields) {
Jedis jedis = null;
List<String> res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.hmget(key, fields);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long hincrby(String key, String field, Long value) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hincrBy(key, field, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Boolean hexists(String key, String field) {
Jedis jedis = null;
Boolean res = false;
try {
jedis = jedisPool.getResource();
res = jedis.hexists(key, field);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long hlen(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hlen(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long hdel(String key, String... fields) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hdel(key, fields);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> hkeys(String key) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hkeys(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public List<String> hvals(String key) {
Jedis jedis = null;
List<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.hvals(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Map<String, String> hgetall(String key, int indexdb) {
Jedis jedis = null;
Map<String, String> res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.hgetAll(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long lpush(int indexdb, String key, String... strs) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.lpush(key, strs);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long rpush(String key, String... strs) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.rpush(key, strs);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long linsert(String key, BinaryClient.LIST_POSITION where, String pivot,
String value) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.linsert(key, where, pivot, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String lset(String key, Long index, String value) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.lset(key, index, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long lrem(String key, long count, String value) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.lrem(key, count, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String ltrim(String key, long start, long end) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.ltrim(key, start, end);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
synchronized public String lpop(String key) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.lpop(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
synchronized public String rpop(String key, int indexdb) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.rpop(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String rpoplpush(String srckey, String dstkey, int indexdb) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.rpoplpush(srckey, dstkey);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String lindex(String key, long index) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.lindex(key, index);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long llen(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.llen(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public List<String> lrange(String key, long start, long end, int indexdb) {
Jedis jedis = null;
List<String> res = null;
try {
jedis = jedisPool.getResource();
jedis.select(indexdb);
res = jedis.lrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String lset(String key, long index, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.lset(key, index, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public List<String> sort(String key, SortingParams sortingParameters) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sort(key, sortingParameters);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public List<String> sort(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.sort(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public Long sadd(String key, String... members) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sadd(key, members);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long srem(String key, String... members) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.srem(key, members);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String spop(String key) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.spop(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> sdiff(String... keys) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sdiff(keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long sdiffstore(String dstkey, String... keys) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sdiffstore(dstkey, keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> sinter(String... keys) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sinter(keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long sinterstore(String dstkey, String... keys) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sinterstore(dstkey, keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> sunion(String... keys) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sunion(keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long sunionstore(String dstkey, String... keys) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sunionstore(dstkey, keys);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long smove(String srckey, String dstkey, String member) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.smove(srckey, dstkey, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long scard(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.scard(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Boolean sismember(String key, String member) {
Jedis jedis = null;
Boolean res = null;
try {
jedis = jedisPool.getResource();
res = jedis.sismember(key, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String srandmember(String key) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.srandmember(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> smembers(String key) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.smembers(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zadd(String key, double score, String member) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zadd(key, score, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> zrange(String key, long min, long max) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.zrange(key, min, max);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return null;
}
public Long zcount(String key, double min, double max) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.zcount(key, min, max);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long hincrBy(String key, String value, long increment) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.hincrBy(key, value, increment);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedisPool, jedis);
}
}
public Long zrem(String key, String... members) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrem(key, members);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Double zincrby(String key, double score, String member) {
Jedis jedis = null;
Double res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zincrby(key, score, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zrank(String key, String member) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrank(key, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zrevrank(String key, String member) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrevrank(key, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> zrevrange(String key, long start, long end) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrevrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> zrangebyscore(String key, String max, String min) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrevrangeByScore(key, max, min);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> zrangeByScore(String key, double max, double min) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zrevrangeByScore(key, max, min);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zcount(String key, String min, String max) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zcount(key, min, max);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zcard(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zcard(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Double zscore(String key, String member) {
Jedis jedis = null;
Double res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zscore(key, member);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zremrangeByRank(String key, long start, long end) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zremrangeByRank(key, start, end);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Long zremrangeByScore(String key, double start, double end) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.zremrangeByScore(key, start, end);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> keys(String pattern) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
res = jedis.keys(pattern);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public Set<String> keysBySelect(String pattern,int database) {
Jedis jedis = null;
Set<String> res = null;
try {
jedis = jedisPool.getResource();
jedis.select(database);
res = jedis.keys(pattern);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public String type(String key) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.type(key);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedisPool, jedis);
}
return res;
}
public static byte[] ObjTOSerialize(Object obj) {
ObjectOutputStream oos = null;
ByteArrayOutputStream byteOut = null;
try {
byteOut = new ByteArrayOutputStream();
oos = new ObjectOutputStream(byteOut);
oos.writeObject(obj);
byte[] bytes = byteOut.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
}
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
public static void returnResource(JedisPool jedisPool, Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
public static void main(String[] args) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(8);
jedisPoolConfig.setMaxWaitMillis(-1);
jedisPoolConfig.setBlockWhenExhausted(true);
jedisPoolConfig.setJmxEnabled(true);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "IP地址", 6001, 10000, null);
Jedis jedis = jedisPool.getResource();
Set<String> str = jedis.keys("*");
for (String string : str) {
System.out.println(string);
}
}
}