RedisUtil-发起redis常用操作工具类
Posted wcis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RedisUtil-发起redis常用操作工具类相关的知识,希望对你有一定的参考价值。
工具类
@Component
public class RedisHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${auth.redis.host}")
private String host;
@Value("${auth.redis.port}")
private int port;
@Value("${auth.redis.dbIndex}")
private int dbIndex = 0;
@Value("${auth.redis.password}")
private String password;
private JedisPool pool;
public RedisHandler() {
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setDbIndex(int dbIndex) {
this.dbIndex = dbIndex;
}
public void setPassword(String password) {
this.password = password;
}
public void reinit() {
this.init();
}
public void allotinit() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50000);
config.setMaxIdle(100);
config.setMaxWaitMillis(10000L);
config.setTestOnBorrow(true);
this.pool = new JedisPool(config, this.host, this.port, 3000, this.password, 30);
}
public void init() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50000);
config.setMaxIdle(100);
config.setMaxWaitMillis(10000L);
config.setTestOnBorrow(true);
this.pool = new JedisPool(config, this.host, this.port, 3000, this.password, this.dbIndex);
}
public void returnJedis(Jedis redis) {
if (redis != null) {
redis.close();
}
}
public Jedis getJedis() {
Jedis j = this.pool.getResource();
return j;
}
public String getString(String key) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
String var3 = redis.get(key);
return var3;
} catch (Exception var7) {
this.logger.error(String.format("从redis取数据失败,key:%s!", key), var7);
} finally {
this.returnJedis(redis);
}
return null;
}
public List<String> getList(String key) throws Exception {
Jedis redis = null;
List var3;
try {
redis = this.getJedis();
var3 = redis.lrange(key, 0L, -1L);
} catch (Exception var7) {
throw var7;
} finally {
this.returnJedis(redis);
}
return var3;
}
public void setString(String key, String value) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
redis.set(key, value);
} catch (Exception var8) {
throw var8;
} finally {
this.returnJedis(redis);
}
}
public void setList(String key, List<Long> values) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
Iterator var4 = values.iterator();
while(var4.hasNext()) {
Long val = (Long)var4.next();
redis.lpush(key, new String[]{String.valueOf(val)});
}
} catch (Exception var9) {
throw var9;
} finally {
this.returnJedis(redis);
}
}
public <V> void mset(List<String> keys, List<V> values) throws Exception {
this.mset(keys, values, (Integer)null);
}
public <V> void mset(List<String> keys, List<V> values, Integer aliveTime) throws Exception {
if (!CollectionUtil.isEmpty(keys) && !CollectionUtil.isEmpty(values)) {
if (keys.size() != values.size()) {
throw new Exception("[RedisHandler.mset]:keys.size() != values.size()");
} else {
Jedis redis = null;
try {
List<byte[]> keysList = Lists.newArrayList();
redis = this.getJedis();
byte[][] bytesKVArray = new byte[2 * keys.size()][];
for(int i = 0; i < keys.size(); ++i) {
bytesKVArray[2 * i] = this.keyToBytes((String)keys.get(i));
bytesKVArray[2 * i + 1] = this.objectToBytes(values.get(i));
keysList.add(bytesKVArray[2 * i]);
}
redis.mset(bytesKVArray);
if (null != aliveTime && 0 < aliveTime) {
Iterator var14 = keysList.iterator();
while(var14.hasNext()) {
byte[] bKey = (byte[])var14.next();
redis.expire(bKey, aliveTime);
}
}
} catch (Exception var12) {
throw var12;
} finally {
this.returnJedis(redis);
}
}
}
}
public <T> List<T> mget(List<String> keys) throws Exception {
return CollectionUtil.isEmpty(keys) ? Collections.emptyList() : this.mget((String[])keys.toArray(new String[keys.size()]));
}
public <T> List<T> mget(String... keys) throws Exception {
List<T> list = Lists.newArrayList();
Jedis redis = null;
try {
redis = this.getJedis();
byte[][] bytesKeyArray = new byte[keys.length][];
for(int i = 0; i < keys.length; ++i) {
String key = keys[i];
bytesKeyArray[i] = this.keyToBytes(key);
}
List<byte[]> bytesList = redis.mget(bytesKeyArray);
if (null == bytesList || 0 >= bytesList.size()) {
List var16 = Collections.emptyList();
return var16;
}
Iterator var15 = bytesList.iterator();
while(var15.hasNext()) {
byte[] item = (byte[])var15.next();
if (item != null) {
Object value = this.bytesToObject(item);
list.add((T) value);
}
}
} catch (Exception var12) {
throw var12;
} finally {
this.returnJedis(redis);
}
return list;
}
public Object get(String key) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
byte[] bytes = redis.get(this.keyToBytes(key));
Object var4 = this.bytesToObject(bytes);
return var4;
} catch (Exception var8) {
;
} finally {
this.returnJedis(redis);
}
return null;
}
public void set(String key, Object value) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
redis.set(this.keyToBytes(key), this.objectToBytes(value));
} catch (Exception var8) {
var8.printStackTrace();
throw var8;
} finally {
this.returnJedis(redis);
}
}
public void set(String key, Object value, int seconds) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
redis.set(this.keyToBytes(key), this.objectToBytes(value));
redis.expire(this.keyToBytes(key), seconds);
} catch (Exception var9) {
throw var9;
} finally {
this.returnJedis(redis);
}
}
public void deleteString(String key) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
if (null != key) {
redis.del(key);
}
} catch (Exception var7) {
var7.printStackTrace();
} finally {
this.returnJedis(redis);
}
}
public void delete(String key) throws Exception {
Jedis redis = null;
try {
redis = this.getJedis();
if (null != key) {
redis.del(this.keyToBytes(key));
}
} catch (Exception var7) {
var7.printStackTrace();
} finally {
this.returnJedis(redis);
}
}
public void publish(String channel, byte[] message) throws Exception {
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.publish(this.keyToBytes(channel), message);
} catch (Exception var8) {
throw var8;
} finally {
this.returnJedis(jedis);
}
}
public Long setnx(String key, String value) {
Jedis jedis = null;
try {
jedis = this.getJedis();
return jedis.setnx(key, value);
} catch (Exception var9) {
throw var9;
} finally {
this.returnJedis(jedis);
}
}
public Long incr(String key) {
Jedis jedis = null;
try {
jedis = this.getJedis();
return jedis.incr(key);
} catch (Exception var10) {
;
} finally {
this.returnJedis(jedis);
}
return null;
}
public void expire(String key, int time) {
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.expire(key, time);
} catch (Exception var11) {
;
} finally {
this.returnJedis(jedis);
}
}
public void del(String key) {
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.del(key);
} catch (Exception var12) {
throw var12;
} finally {
this.returnJedis(jedis);
}
}
public void setHashMap(String form,Map<String, String> hash) {
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.hmset(form,hash);
} catch (Exception var19) {
throw var19;
} finally {
this.returnJedis(jedis);
}
}
public List<String> hmget(String key, String... fields) {
Jedis jedis = null;
try {
jedis = this.getJedis();
return jedis.hmget(key, fields);
} catch (Exception var24) {
throw var24;
} finally {
this.returnJedis(jedis);
}
}
public Long hlen(String form) {
Jedis jedis = null;
try {
jedis = this.getJedis();
return jedis.hlen(form);
} catch (Exception var20) {
throw var20;
} finally {
this.returnJedis(jedis);
}
}
public void saveSortHashMap(List<AllotOrderInfoVO> list,String form) {
Jedis jedis = null;
try {
jedis = this.getJedis();
for( int i = 1 ; i <=list.size() ; i++) {
// 初始化索引 SortSet
jedis.zadd("InvoiceTopicId", i, "allotId:"+i);
// 初始化数据 Hash
jedis.hset(form,"allotId"+i, JsonUtil.toJson(list.get(i)));
}
} catch (Exception var21) {
throw var21;
} finally {
this.returnJedis(jedis);
}
}
public List<String> lrange(String userid,Long curPage,Long pageSize){
Jedis jedis = null;
try {
jedis = this.getJedis();
return jedis.lrange(userid,curPage,pageSize);
} catch (Exception var21) {
throw var21;
} finally {
this.returnJedis(jedis);
}
}
public Long getRedisTime() {
Jedis jedis = null;
try {
jedis = this.getJedis();
return Long.parseLong(jedis.time().get(0)) * 1000;
} catch (Exception var12) {
throw var12;
} finally {
this.returnJedis(jedis);
}
}
private byte[] keyToBytes(String key) {
return key.getBytes();
}
private byte[] objectToBytes(Object obj) throws Exception {
if (null == obj) {
return null;
} else {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOutputStream);
byte[] var5;
try {
objectOut.writeObject(obj);
byte[] objByte = byteOutputStream.toByteArray();
var5 = objByte;
} catch (Exception var9) {
throw var9;
} finally {
byteOutputStream.close();
objectOut.close();
}
return var5;
}
}
private Object bytesToObject(byte[] bytes) throws Exception {
if (null == bytes) {
return null;
} else {
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectIn = new ObjectInputStream(byteInputStream);
Object var5;
try {
Object obj = objectIn.readObject();
byteInputStream.close();
objectIn.close();
var5 = obj;
} catch (Exception var9) {
throw var9;
} finally {
byteInputStream.close();
objectIn.close();
}
return var5;
}
}
public static void main(String[] args) throws Exception {
RedisHandler redis = new RedisHandler();
redis.reinit();
redis.set("k1", 1, TradeNoConstants.DEFAULT_CACHE_SECOND);
System.out.println(redis.get("k1"));
}
}
以上是关于RedisUtil-发起redis常用操作工具类的主要内容,如果未能解决你的问题,请参考以下文章