redis工具类
2019-10-29 本文已影响0人
Mark_ZSQ
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
package com.ltkj.pmc.utility;
import com.ltkj.pmc.model.BaseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.Map;
/**
* RedisUtils redis操作工具类
* @author: zhangshiqiang
* @date: 2019.10.29
*/
@Component("RedisUtil")
public class RedisUtils {
@Autowired
private JedisPool jedisPool;
/**
* 插入或修改数据(字符串)
* @param key,value,flag(redis分区)
* @return
*/
public String setRedis(String key,String value,int flag){
Jedis jedis = null;
String res = "fail";
try {
jedis = jedisPool.getResource();
jedis.select(flag);
jedis.set(key, value);
res = "success";
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return res;
}
/**
* 插入或修改数据(字符串)--10分钟有效时间
* @param key,value,flag(redis分区)
* @return
*/
public String setRedisTime(String key,String value,int flag){
Jedis jedis = null;
String res = "fail";
try {
jedis = jedisPool.getResource();
jedis.select(flag);
jedis.set(key, value, "NX", "EX", BaseConstants.MB_SMS_CODE_TIME);
res = "success";
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return res;
}
/**
* getRedis 获取数据(字符串)
* @param key,flag(redis分区)
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public String getRedis(String key,int flag){
Jedis jedis = null;
String result = "";
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
return result;
}
/**
* delRedis 删除redis
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public void delRedis(String key,int flag){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
}
/**
* existsRedis 判断redis键Key是否存在
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public boolean existsRedis(String key,int flag){
Jedis jedis = null;
Boolean result = false;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return result;
}
/**
* incrRedis 整数(incr:自增;decr:自减)
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public long incrRedis(String key,int flag,String type){
Jedis jedis = null;
String incr = "incr";
String decr = "decr";
long result = 0;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
if(incr.equals(type)){
result = jedis.incr(key);
}else if (decr.equals(decr)) {
result = jedis.decr(key);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return result;
}
/**
* hget 获取redisHash 键名值
* @param keyname
* @param field
* @param section
* @return
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public String hgetKey(String keyname,String field,int section) {
Jedis jedis = null;
String res = "";
try {
jedis = jedisPool.getResource();
jedis.select(section);
res = jedis.hget(keyname, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return res;
}
/**
* hget 获取redisHash 所有值
* @param keyname
* @param section
* @return
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public Map<String, String> hgetKey(String keyname,int section) {
Jedis jedis = null;
Map<String, String> result = new HashMap<>(16);
try {
jedis = jedisPool.getResource();
jedis.select(section);
result = jedis.hgetAll(keyname);
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return result;
}
/**
* hset 设置redisHash值
* @param key
* @param field
* @param value
* @param flag
* @return
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public long setHashRedis(String key,String field,String value,int flag){
Jedis jedis = null;
long result = 0;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
return result;
}
/**
* hset 设置redisHash值 (map集合<String, String>)
* @param key
* @param map <String, String>
* @param flag
* @return
* @author: yanjianghua
* @date: 2018/9/13 14:47
*/
public String setHashRedis(String key,Map<String, String> map,int flag){
Jedis jedis = null;
String result = "";
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.hmset(key, map);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
return result;
}
/**
* delHashRedis 删除redisHash一个或者多个键值对
* @param key
* @param field
* @param flag
* @author: yanjianghua
* @date: 2018/9/13 15:50
*/
public long delHashRedis(String key,String field,int flag){
Jedis jedis = null;
long result = 0;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.hdel(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
return result;
}
/**
* setRedisNx 公共锁(不会被覆盖的)
* @param key
* @param value
* @param flag
* @author: yanjianghua
* @date: 2018/9/13 15:02
*/
public long setRedisNx(String key,String value,int flag){
Jedis jedis = null;
Long result = 0L;
try {
jedis = jedisPool.getResource();
jedis.select(flag);
result = jedis.setnx(key,value);
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
jedis.close();
}
return result;
}
}