실 용 Redis 조작 클래스
147483 단어 자바
import com.yr.phoenix.utils.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* \* Created with IntelliJ IDEA.
* \* User: yr
* \* Date: 2017/9/14
* \* Time: 10:57
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@Component
public class RedisClient {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JedisPool jedisPool;
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param
*/
public void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/*** Description: jedis
* @author yepengjun
* @date 2017 9 14
* @param
*/
public Jedis getJedis() throws Exception {
Jedis jedis = jedisPool.getResource();
return jedis;
}
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param key
*/
public String get(String key){
String value = null;
Jedis jedis = null;
try {
jedis= getJedis();
value = jedis.get(key);
} catch(Exception e){
logger.error("RedisClient get :{}",e);
} finally {
returnResource(jedis);
}
return value;
}
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param key value
*/
public String set(String key,String value){
Jedis jedis = null;
String ans = null;
try {
jedis = getJedis();
ans = jedis.set(key,value);
} catch (Exception e) {
logger.error("RedisClient set :{}",e);
} finally {
//
returnResource(jedis);
}
return ans;
}
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param key seconds value
*/
public String setex(String key,int seconds,String value){
Jedis jedis = null;
String ans = null;
try {
jedis = getJedis();
ans = jedis.setex(key,seconds,value);
} catch (Exception e) {
logger.error("RedisClient setex :{}",e);
} finally {
//
returnResource(jedis);
}
return ans;
}
/*** Description:
* key offset value
* 0 ,offset offset
*
* example:
* value : [email protected]
* str : abc
* 7
* RES : bigsea.abc.cn
* @author yepengjun
* @date 2017 9 14
* @param
* @return value
*/
public Long setrange(String key,String str,int offset){
Jedis jedis = null;
Long result = 0L;
try {
jedis = getJedis();
result = jedis.setrange(key, offset, str);
} catch (Exception e) {
logger.error("RedisClient setrange :{}",e);
} finally {
//
returnResource(jedis);
}
return result;
}
/*** Description: key value
* @author yepengjun
* @date 2017 9 14
* @param
* @return value , null ,
*/
public List mget(String...keys){
Jedis jedis = null;
List values = null;
try {
jedis = getJedis();
values = jedis.mget(keys);
} catch (Exception e) {
logger.error("RedisClient mget :{}",e);
} finally {
//
returnResource(jedis);
}
return values;
}
/*** Description: key:value,
* obj.mset(new String[]{"key2","value1","key2","value2"})
* @author yepengjun
* @date 2017 9 14
* @param
* @return
*/
public String mset(String...keysvalues){
Jedis jedis = null;
String ans = null;
try{
jedis = getJedis();
ans = jedis.mset(keysvalues);
}catch (Exception e) {
logger.error("RedisClient mset :{}",e);
} finally {
//
returnResource(jedis);
}
return ans;
}
/*** Description: key value
* @author yepengjun
* @date 2017 9 14
* @param key str
*/
public Long append(String key,String str){
Jedis jedis = null;
Long result = 0L;
try {
jedis = getJedis();
result = jedis.append(key, str);
} catch (Exception e) {
logger.error("RedisClient append :{}",e);
} finally {
//
returnResource(jedis);
}
return result;
}
/*** Description: key
* @author yepengjun
* @date 2017 9 14
* @param
*/
public Boolean exists(String key){
Jedis jedis = null;
boolean result = false;
try {
jedis = getJedis();
result = jedis.exists(key);
} catch (Exception e) {
logger.error("RedisClient exists :{}",e);
} finally {
//
returnResource(jedis);
}
return result;
}
/*** key value, key 0,nx==> not exist
* @author yepengjun
* @date 2017 9 14
* @param
* @return 1 0
*/
public Long setnx(String key,String value){
Jedis jedis = null;
Long result = 0L;
try {
jedis = getJedis();
result = jedis.setnx(key,value);
} catch (Exception e) {
logger.error("RedisClient setnx :{}",e);
} finally {
//
returnResource(jedis);
}
return result;
}
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param key seconds
*/
public Long expire(String key,int seconds){
Jedis jedis = null;
Long res = 0l;
try {
jedis = getJedis();
res = jedis.expire(key, seconds);
} catch (Exception e) {
logger.error("RedisClient expire :{}",e);
} finally {
//
returnResource(jedis);
}
return res;
}
/*** Description:
* @author yepengjun
* @date 2017 9 14
* @param keys
*/
public Long del(String...keys){
Jedis jedis = null;
Long res = 0L;
try {
jedis = getJedis();
res = jedis.del(keys);
} catch (Exception e) {
logger.error("RedisClient del :{}",e);
} finally {
//
returnResource(jedis);
}
return res;
}
/*** Description: key
* @author yepengjun
* @date 2017 9 14
* @param key
*/
public Long timetolive(String key){
Jedis jedis = null;
Long res = 0l;
try {
jedis = getJedis();
res = jedis.ttl(key);
} catch (Exception e) {
logger.error("RedisClient timetolive :{}",e);
} finally {
//
returnResource(jedis);
}
return res;
}
/*** Description: key:value, , key ,
* @author yepengjun
* @date 2017 9 14
* @param
* @return 1 0
*/
public Long msetnx(String...keysvalues){
Jedis jedis = null;
Long res = 0L;
try {
jedis = getJedis();
res =jedis.msetnx(keysvalues);
} catch (Exception e) {
logger.error("RedisClient msetnx :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/*** Description: key ,
* @author yepengjun
* @date 2017 9 14
* @param
* @return key null
*/
public String getset(String key,String value){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.getSet(key, value);
} catch (Exception e) {
logger.error("RedisClient getset :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key value
* @param key
* @param startOffset 0
* @param endOffset
* @return null
*/
public String getrange(String key, int startOffset ,int endOffset){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
logger.error("RedisClient getrange :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key value +1 , value int , key value 1
* @param key
* @return
*/
public Long incr(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.incr(key);
} catch (Exception e) {
logger.error("RedisClient incr :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key value , key , value
* @param key
* @param integer
* @return
*/
public Long incrBy(String key,Long integer){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.incrBy(key, integer);
} catch (Exception e) {
logger.error("RedisClient incrBy :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key , key , key -1
* @param key
* @return
*/
public Long decr(String key) {
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.decr(key);
} catch (Exception e) {
logger.error("RedisClient decr :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
*
* @param key
* @param integer
* @return
*/
public Long decrBy(String key,Long integer){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.decrBy(key, integer);
} catch (Exception e) {
logger.error("RedisClient decrBy :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key value
* @param key
* @return null
*/
public Long serlen(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.strlen(key);
} catch (Exception e) {
logger.error("RedisClient serlen :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field , key ,
* @param key
* @param field
* @param value
* @return 0 null
*/
public Long hset(String key,String field,String value) {
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hset(key, field, value);
} catch (Exception e) {
logger.error("RedisClient hset :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field , key ,
* @param key
* @param field
* @param value
* @return 0 null
*/
public Long hsetObject(String key,String field,Object value) {
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value));
} catch (Exception e) {
logger.error("RedisClient hset :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field , key , field , 0
* @param key
* @param field
* @param value
* @return
*/
public Long hsetnx(String key,String field,String value){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hsetnx(key, field, value);
} catch (Exception e) {
logger.error("RedisClient hsetnx :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key hash field
* @param key
* @param hash
* @return OK null
*/
public String hmset(String key,Map, String> hash){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.hmset(key, hash);
} catch (Exception e) {
logger.error("RedisClient hmset :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field value
* @param key
* @param field
* @return null
*/
public String hget(String key, String field){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.hget(key, field);
} catch (Exception e) {
logger.error("RedisClient hget :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key fields value value null
* @param key
* @param fields String String
* @return
*/
public List hmget(String key,String...fields){
Jedis jedis = null;
List res = null;
try {
jedis = getJedis();
res = jedis.hmget(key, fields);
} catch (Exception e) {
logger.error("RedisClient hmget :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field value
* @param key
* @param field
* @param value
* @return
*/
public Long hincrBy(String key ,String field ,Long value){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hincrBy(key, field, value);
} catch (Exception e) {
logger.error("RedisClient hincrBy :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field value
* @param key
* @param field
* @return
*/
public Boolean hexists(String key , String field){
Jedis jedis = null;
Boolean res = false;
try {
jedis = getJedis();
res = jedis.hexists(key, field);
} catch (Exception e) {
logger.error("RedisClient hexists :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field
* @param key
* @return
*/
public Long hlen(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hlen(key);
} catch (Exception e) {
logger.error("RedisClient hlen :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field
* @param key
* @param fields field
* @return
*/
public Long hdel(String key ,String...fields){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hdel(key, fields);
} catch (Exception e) {
logger.error("RedisClient hdel :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field
* @param key
* @return
*/
public Set hkeys(String key){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.hkeys(key);
} catch (Exception e) {
logger.error("RedisClient hkeys :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key key value
* @param key
* @return
*/
public List hvals(String key){
Jedis jedis = null;
List res = null;
try {
jedis = getJedis();
res = jedis.hvals(key);
} catch (Exception e) {
logger.error("RedisClient hvals :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key field value
* @param key
* @return
*/
public Map, String> hgetall(String key){
Jedis jedis = null;
Map, String> res = null;
try {
jedis = getJedis();
res = jedis.hgetAll(key);
} catch (Exception e) {
logger.error("RedisClient hgetall :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list
* @param key
* @param strs string string
* @return list value
*/
public Long lpush(String key ,String...strs){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.lpush(key, strs);
} catch (Exception e) {
logger.error("RedisClient lpush :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list
* @param key
* @param strs string string
* @return list value
*/
public Long rpush(String key ,String...strs){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.rpush(key, strs);
} catch (Exception e) {
logger.error("RedisClient rpush :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list
* @param key
* @param where LIST_POSITION
* @param pivot list value
* @param value value
* @return
*/
public Long linsert(String key, BinaryClient.LIST_POSITION where,
String pivot, String value){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.linsert(key, where, pivot, value);
} catch (Exception e) {
logger.error("RedisClient linsert :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value
* list value
* @param key
* @param index 0
* @param value
* @return OK
*/
public String lset(String key ,Long index, String value){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.lset(key, index, value);
} catch (Exception e) {
logger.error("RedisClient lset :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list count value
* @param key
* @param count count 0
* @param value
* @return
*/
public Long lrem(String key,long count,String value){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.lrem(key, count, value);
} catch (Exception e) {
logger.error("RedisClient lrem :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list strat end value
* @param key
* @param start
* @param end
* @return OK
*/
public String ltrim(String key ,long start ,long end){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.ltrim(key, start, end);
} catch (Exception e) {
logger.error("RedisClient ltrim :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value, value
* @param key
* @return
*/
public String lpop(String key){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.lpop(key);
} catch (Exception e) {
logger.error("RedisClient lpop :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value,
* @param key
* @return
*/
public String rpop(String key){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.rpop(key);
} catch (Exception e) {
logger.error("RedisClient rpop :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value list , value
* list null
* @param srckey
* @param dstkey
* @return
*/
public String rpoplpush(String srckey, String dstkey){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.rpoplpush(srckey, dstkey);
} catch (Exception e) {
logger.error("RedisClient rpoplpush :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value
* @param key
* @param index
* @return null
*/
public String lindex(String key,long index){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.lindex(key, index);
} catch (Exception e) {
logger.error("RedisClient lindex :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list
* @param key
* @return
*/
public Long llen(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.llen(key);
} catch (Exception e) {
logger.error("RedisClient llen :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key list value
* start 0 end -1 list value
* @param key
* @param start
* @param end
* @return
*/
public List lrange(String key,long start,long end){
Jedis jedis = null;
List res = null;
try {
jedis = getJedis();
res = jedis.lrange(key, start, end);
} catch (Exception e) {
logger.error("RedisClient lrange :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value
* @param key
* @param members String String
* @return
*/
public Long sadd(String key,String...members){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sadd(key, members);
} catch (Exception e) {
logger.error("RedisClient sadd :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value
* @param key
* @param members String String
* @return
*/
public Long srem(String key,String...members){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.srem(key, members);
} catch (Exception e) {
logger.error("RedisClient srem :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value
* @param key
* @return
*/
public String spop(String key){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.spop(key);
} catch (Exception e) {
logger.error("RedisClient spop :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set
* set
* @param keys string set value string
* @return
*/
public Set sdiff(String...keys){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.sdiff(keys);
} catch (Exception e) {
logger.error("RedisClient sdiff :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set key
* set
* @param dstkey key
* @param keys string set value string
* @return
*/
public Long sdiffstore(String dstkey,String... keys){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sdiffstore(dstkey, keys);
} catch (Exception e) {
logger.error("RedisClient sdiffstore :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set
* @param keys string string
* @return
*/
public Set sinter(String...keys){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.sinter(keys);
} catch (Exception e) {
logger.error("RedisClient sinter :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set set
* @param dstkey
* @param keys string string
* @return
*/
public Long sinterstore(String dstkey,String...keys){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sinterstore(dstkey, keys);
} catch (Exception e) {
logger.error("RedisClient sinterstore :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set
* @param keys string string
* @return
*/
public Set sunion(String... keys){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.sunion(keys);
} catch (Exception e) {
logger.error("RedisClient sunion :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set , set
* @param dstkey
* @param keys string string
* @return
*/
public Long sunionstore(String dstkey,String...keys){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sunionstore(dstkey, keys);
} catch (Exception e) {
logger.error("RedisClient sunionstore :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value set
* @param srckey
* @param dstkey
* @param member set value
* @return
*/
public Long smove(String srckey, String dstkey, String member){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.smove(srckey, dstkey, member);
} catch (Exception e) {
logger.error("RedisClient smove :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value
* @param key
* @return
*/
public Long scard(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.scard(key);
} catch (Exception e) {
logger.error("RedisClient scard :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key value set
* @param key
* @param member
* @return
*/
public Boolean sismember(String key,String member){
Jedis jedis = null;
Boolean res = null;
try {
jedis = getJedis();
res = jedis.sismember(key, member);
} catch (Exception e) {
logger.error("RedisClient sismember :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value,
* @param key
* @return
*/
public String srandmember(String key){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.srandmember(key);
} catch (Exception e) {
logger.error("RedisClient srandmember :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key set value
* @param key
* @return
*/
public Set smembers(String key){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.smembers(key);
} catch (Exception e) {
logger.error("RedisClient smembers :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key
* @param key
* @param start
* @param end
* @return
*/
public Long zremrangeByRank(String key ,long start, long end){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.zremrangeByRank(key, start, end);
} catch (Exception e) {
logger.error("RedisClient zremrangeByRank :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* key score
* @param key
* @param start
* @param end
* @return
*/
public Long zremrangeByScore(String key,double start,double end){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.zremrangeByScore(key, start, end);
} catch (Exception e) {
logger.error("RedisClient zremrangeByScore :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
/**
* pattern key
* keys(*)
* key
* @param pattern
* @return
*/
public Set keys(String pattern){
Jedis jedis = null;
Set res = null;
try {
jedis = getJedis();
res = jedis.keys(pattern);
} catch (Exception e) {
logger.error("RedisClient keys :{}",e);
} finally {
returnResource(jedis);
}
return res;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.