Redis連線操作工具類
阿新 • • 發佈:2020-11-02
在開發過程中使用Redis,每個人都寫一套,很是不方便,下面奉上RedisUtil工具類,包含獲取連線,獲取資源,釋放資源,String、物件儲存、刪除等等操作
下面上程式碼:
package com.jane.framework.redis;
import com.jane.framework.common.util.ConstantsConfigureBean;
import com.jane.framework.common.util.SpringContextUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import redis.clients.jedis.*;
import java.io.*;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;
public class JedisUtil {
protected final static Logger logger = Logger.getLogger(JedisUtil.class);
private final static String HOST = "redis的連線地址";
// redis連線的埠號
private final static int PORT = 6379;
// 可用連線例項的最大數目,預設值為8
private static int MAX_ACTIVE = 1024;
// 控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項,預設值也是8
private static int MAX_IDLE = 200;
// 等待可用連線的最大時間,單位毫秒,預設值為-1,表示永不超時
// 如果超過等待時間,則直接丟擲JedisConnectionException
private static int MAX_WAIT = 20000;
private static int TIMEOUT = 10000;
// 在borrow一個jedis例項時,是否提前進行validate操作,如果為true,則得到的jedis例項均是可用的
private static boolean TEST_ON_BORROW = true;
// 過期時間 單位:秒
private static int EXPIRE_TIME = 18000;
private static final String TEST = "test";
private static final String REDIS_PASSWORD = "redis的連線密碼";
private static final String SYSTEM_REDIS = "systemRedis";
private static final String TEST_REDIS_ADDRESS = "testRedisAddress";
private static final String TEST_REDIS_PORT = "testRedisPort";
private static final String TEST_REDIS_PASSWORD = "testRedisPassword";
private static volatile JedisPool jedisPool = null;
private JedisUtil() {
}
/**
* 獲取連線池
* @Author <jane>
* @Description
* @Date 2020-6-10上午10:18:24
* @return
* @throws
*/
public static JedisPool getJedisPoolInstance() {
if (null == jedisPool) {
synchronized (JedisUtil.class) {
if (null == jedisPool) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
Properties properties = null;
try {
properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
} catch (IOException e) {
e.printStackTrace(