1、连接redis工具类
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class Connection {
//服务器IP
private static String IP = “192.168.0.78”;
//端口号
private static int PORT = 6379;
//密码
private static String PWD = "1996@qq.com";
private static int MAX_POOL = 1000;
private static int MIN_POOL = 200;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
private static int MAX_WAIT = 10000;
//连接超时的时间
private static int TIMEOUT = 10000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_POOL);
config.setMaxTotal(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
jedisPool = new JedisPool(config, IP, PORT, TIMEOUT, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public Jedis getJedis() {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
}
/***
*
* 释放资源
*/
public void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
2、jedis操作
public class RedisUtil{
private static Jedis jedis=null;
private Connection conn=new Connection();
/**
* 从redis中通过模糊查询获得所有的key
*
* @param key 键值
* @param object 数据
* @return
*/
public Set keys(String keyPattern) {
try {
jedis = conn.getJedis();
return jedis.keys(keyPattern);
} catch (Exception e) {
System.out.println(e.printStackTrace);
} finally {
conn.close(jedis);
}
}
/**
* 向redis中存入数据
*
* @param key 键值
* @param object 数据
* @param seconds 过期时间以秒为单位
* 如果 key 已经存在, SETEX 命令将覆写旧值
* @return
*/
public <T> boolean set(String key, T object, int seconds) {
try {
jedis = conn.getJedis();
if (seconds > 0) {
jedis.setex(key.getBytes(), seconds, ConvertUtil.serialize(object));
} else {
jedis.set(key.getBytes(), ConvertUtil.serialize(object));
}
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
return true;
}
/**
* list操作 插入数据
* 将一个或多个值 value 插入到列表 key 的表尾(最右边)。
* 如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表尾。
* 如果 key 不存在,一个空列表会被创建并执行 RPUSH 操作。
*/
public Long rpush(String key, String value) {
Jedis jedis = conn.getJedis();
try {
return jedis.rpush(key, value);
} finally {
conn.close(jedis);
}
}
/**
* key中的域field的值设为object
*
* @param key 键值
* @param fieldName 域
* @param object 数据
* @param seconds 过期时间以秒为单位
* @return
*/
public <T> boolean hset(String key, String fieldName, T object, int seconds) {
try {
jedis = conn.getJedis();
jedis.hset(key.getBytes(), fieldName.getBytes(), ConvertUtil.serialize(object));
if (seconds > 0)
jedis.expire(key.getBytes(), seconds);
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
return true;
}
/**
* 为哈希表中的字段值加上指定增量值
*如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。
*如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。
* @param key 键
* @param field 字段名
* @param value 自增量
* @return
*/
public Long hincrby(String key, String field, long value) {
try {
jedis = conn.getJedis();
return jedis.hincrBy(key.getBytes(), field.getBytes(), value);
} catch (Exception e) {
System.out.println(e.printStackTrace);
return 0L;
} finally {
conn.close(jedis);
}
}
/**
* hash判断 给定域 field 是否存在。
*
* @return
*/
public Boolean hexists(String key, String field) {
try {
jedis = conn.getJedis();
return jedis.hexists(key.getBytes(), field.getBytes());
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
}
/**
* 删除一个或多个哈希表字段
*
* @param key 键值
* @param fieldName 字段名
* @return
*/
public boolean hdel(String key, String fieldName) {
try {
jedis = conn.getJedis();
Long num= jedis.hdel(key.getBytes(), fieldName.getBytes());
if(num !=0){
return true;
}else{
return false;
}
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
}
/**
* 返回哈希表 key 中给定域 field 的值。
*/
public String hget(String key, String field) {
Jedis jedis = conn.getJedis();
try {
return jedis.hget(key, field);
} finally {
conn.close(jedis);
}
}
/**
*同时将多个 field-value (域-值)对设置到哈希表 key 中
*/
public String hmset(String key, Map<String, String> hash) {
Jedis jedis = conn.getJedis();
try {
return jedis.hmset(key, hash);
} finally {
conn.close(jedis);
}
}
/**
* 获取数据
*
* @param key
* @return
*/
public <T> T get(String key, Class<T> clazz) {
try {
jedis = conn.getJedis();
byte[] value = jedis.get(key.getBytes());
if (value != null) {
return ConvertUtil.unserialize(value, clazz);
} else {
return null;
}
} catch (Exception e) {
System.out.println(e.printStackTrace);
return null;
} finally {
conn.close(jedis);
}
}
/**
* 获取key的剩余过期时间,单位:秒
*
* @param key
* @return
*/
public Long getExpireSeconds(String key) {
try {
jedis = conn.getJedis();
return jedis.ttl(key.getBytes());
} catch (Exception e) {
System.out.println(e.printStackTrace);
return null;
} finally {
conn.close(jedis);
}
}
/**
* 删除redis中key对应数据
*
* @param key 键值
* @return 成功\失败
*/
public boolean del(String key) {
try {
jedis = conn.getJedis();
jedis.del(key);
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
return true;
}
/**
* 删除redis中key对应数据
*
* @param key
* @return 删除的条数
*/
public long deleteRegEx(String key) {
long count = 0;
try {
jedis = conn.getJedis();
Set<String> keys = jedis.keys(key);
if (keys == null || keys.isEmpty()) {
return 0;
}
for (String sigleKey : keys) {
jedis.del(sigleKey);
count++;
}
return count;
} catch (Exception e) {
return -1;
} finally {
conn.close(jedis);
}
}
/**
* 指定的 key 不存在时,为 key 设置指定的值。
*
* @param key key
* @return true:存在,false:不存在
*/
public boolean setnx(String key, Object object, int seconds) {
try {
jedis = conn.getJedis();
Long res = jedis.setnx(key, object.toString());
if (new Long(1L).equals(res)) {
// 设定过期时间,
jedis.expire(key, seconds);
return true;
}
return false;
} catch (Exception e) {
System.out.println(e.printStackTrace);
return false;
} finally {
conn.close(jedis);
}
}
/**
* 自增
*
* @param key key
* @return 0:失败,非0:成功
*/
public Long incr(String key) {
try {
jedis = conn.getJedis();
return jedis.incr(key);
} catch (Exception e) {
System.out.println(e.printStackTrace);
return 0L;
} finally {
conn.close(jedis);
}
}
/**
* 自减
*
* @param key key
* @return 0:失败,非0:成功
*/
public Long decr(String key) {
try {
jedis = conn.getJedis();
return jedis.decr(key);
} catch (Exception e) {
System.out.println(e.printStackTrace);
return 0L;
} finally {
conn.close(jedis);
}
}
}