Redis工具类的封装
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
RedisPool:redis连接池
package com.mark.common;
import com.mark.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by Mark on 2018/4/20.
* Redis连接池:JedisPool
*/
public class RedisPool {
private static JedisPool pool;
private static Integer MaxTotal = 20;//最大连接数
private static Integer MaxIdle = 10;//在jedispool中最大的idle状态(空闲的)的jedis实例的个数
private static Integer MinIdle = 2;//在jedispool中最小的idle状态(空闲的)的jedis实例的个数
private static Boolean testOnBorrow = true;//在borrow一个jedis实例的时候,是否要进行验证操作,如果赋值true。则得到的jedis实例肯定是可以用的。
private static Boolean testReturn = false;//在return一个jedis实例的时候,是否要进行验证操作,如果赋值true。则放回jedispool的jedis实例肯定是可以用的。
private static String redisIp = 127.0.0.1;//redis服务端的ip
private static Integer redisPort = 3679;//redis提供的接口
private static void initPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MaxTotal);
config.setMaxIdle(MaxIdle);
config.setMinIdle(MinIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testReturn);
config.setBlockWhenExhausted(true);//连接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时。默认为true。
pool = new JedisPool(config,redisIp,redisPort,1000*2);
}
static {
initPool();
}
//对外开放的接口
public static Jedis getJedis(){//获取一个Jedis实例
return pool.getResource();
}
public static void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis);
}
public static void returnResource(Jedis jedis){
pool.returnResource(jedis);
}
//测试
public static void main(String[] args) {
Jedis jedis = pool.getResource();
jedis.set("mark","mark");
returnResource(jedis);
pool.destroy();//临时调用,销毁连接池中的所有连接
System.out.println("program is end");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
Jedis 实例API的封装
package com.mark.util;
import com.mark.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
/**
* Created by Mark on 2018/4/21.
* RedisPool封装的API
*/
@Slf4j
public class RedisPoolUtil {
//设置key的有效期
public static Long expire(String key,int seconds){
Jedis jedis = null;
Long result = null;
try {
jedis = RedisPool.getJedis();//从自定义RedisPool连接池获取一个实例
result = jedis.expire(key,seconds);
} catch (Exception e) {
log.error("expire key:{} error",key ,e);
RedisPool.returnBrokenResource(jedis);//发生错误回收
return result;
}
RedisPool.returnResource(jedis);//正常回收
return result;
}
//设置key时间并且设置过期时间
public static String setEx(String key,String value,int seconds){
Jedis jedis = null;
String result = null;
try {
jedis = RedisPool.getJedis();
result = jedis.setex(key,seconds,value);
} catch (Exception e) {
log.error("expire key:{} value:{} error",key ,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
//设置一个key
public static String set(String key,String value){
Jedis jedis = null;
String result = null;
try {
jedis = RedisPool.getJedis();
result = jedis.set(key,value);
} catch (Exception e) {
log.error("expire key:{} value:{} error",key ,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
//获取一个key
public static String get(String key){
Jedis jedis = null;
String result = null;
try {
jedis = RedisPool.getJedis();
result = jedis.get(key);
} catch (Exception e) {
log.error("expire key:{} value:{} error",key ,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
//删除一个key
public static Long del(String key){
Jedis jedis = null;
Long result = null;
try {
jedis = RedisPool.getJedis();
result = jedis.del(key);
} catch (Exception e) {
log.error("expire key:{} value:{} error",key ,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
//调用测试
public static void main(String[] args) {
String result = RedisPoolUtil.set("mark","25");
result = RedisPoolUtil.get("mark");
result = RedisPoolUtil.setEx("mark2","23",20);
Long resultL = RedisPoolUtil.expire("mark",20);
resultL = RedisPoolUtil.del("mark");
resultL = RedisPoolUtil.del("marks");
System.out.println("补充:使用redis缓存数据时候,最好加上过期时间,因为redis缓存需要占用内存空间");
}
}