redis tool class

method one,

import redis.clients.jedis.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* <p>Description:redis</p>
*
* @Author: Austin
**/
public class JedisUtil implements Serializable {

//Redis server IP
private static String addr;

//Redis port number
private static int port;

//Access password
private static String auth;

//The maximum number of available connection instances, the default value is 8;
//If the value is -1, it means there is no limit; if the pool has allocated maxActive jedis instances, the state of the pool is exhausted at this time.
private static int maxActive;

//Control the maximum number of jedis instances in idle (idle) status in a pool, the default value is also 8.
private static int maxIdle;

//The maximum time to wait for an available connection, in milliseconds, the default value is -1, which means never timeout. If the waiting time is exceeded, JedisConnectionException is thrown directly;
private static int maxWait;

private static int timeOut;

//When borrowing a jedis instance, whether to perform the validate operation in advance; if it is true, the obtained jedis instance is available;
private static boolean testOnBorrow;

public static Jedis jedis;//Non-sliced ​​client connection

public static JedisPool jedisPool;//Non-sliced ​​connection pool

public static ShardedJedis shardedJedis;//Slice client connection

public static ShardedJedisPool shardedJedisPool;//Slice connection pool

/**
* 初始化片池
**/
private static void initialPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
jedisPool = new JedisPool(config, addr, port);
}

/**
* 初始化切片池
**/
private static void initialShardedPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(addr, port, auth));

// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}

public static void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
initialPool();
initialShardedPool();
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
System.out.println("连接shardedJedisPool失败!");
}
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
System.out.println("连接jedisPool失败!");
}
}

public void setAddr(String addr) {
this.addr = addr;
}

public String getAddr() {
return addr;
}

 

public int getPort() {
return port;
}

public void setPort(int port) {
JedisUtil.port = port;
}

public String getAuth() {
return auth;
}

public void setAuth(String auth) {
JedisUtil.auth = auth;
}

public int getMaxActive() {
return maxActive;
}

public void setMaxActive(int maxActive) {
JedisUtil.maxActive = maxActive;
}

public int getMaxIdle() {
return maxIdle;
}

public void setMaxIdle(int maxIdle) {
JedisUtil.maxIdle = maxIdle;
}

public int getMaxWait() {
return maxWait;
}

public void setMaxWait(int maxWait) {
JedisUtil.maxWait = maxWait;
}

public int getTimeOut() {
return timeOut;
}

public void setTimeOut(int timeOut) {
JedisUtil.timeOut = timeOut;
}

public boolean isTestOnBorrow() {
return testOnBorrow;
}

public void setTestOnBorrow(boolean testOnBorrow) {
JedisUtil.testOnBorrow = testOnBorrow;
}
}

Method two,

import redis.clients.jedis.*;

import java.io.Serializable;

/**
* <p>Description:</p>
*
* @Author: Austin
**/
public class RedisUtil implements Serializable {
//Redis服务器IP
private static String addr;

//Redis port number
private static int port;

//Access password
private static String auth;

//The maximum number of available connection instances, the default value is 8;
//If the value is -1, it means there is no limit; if the pool has allocated maxActive jedis instances, the state of the pool is exhausted at this time.
private static int maxActive;

//Control the maximum number of jedis instances in idle (idle) status in a pool, the default value is also 8.
private static int maxIdle;

//The maximum time to wait for an available connection, in milliseconds, the default value is -1, which means never timeout. If the waiting time is exceeded, JedisConnectionException is thrown directly;
private static int maxWait;

private static int timeOut;

//When borrowing a jedis instance, whether to perform the validate operation in advance; if it is true, the obtained jedis instance is available;
private static boolean testOnBorrow;

public static Jedis jedis;//Non-sliced ​​client connection

public static JedisPool jedisPool;//Non-sliced ​​connection pool

public static ShardedJedis shardedJedis;//Slice client connection

public static ShardedJedisPool shardedJedisPool;//Slice connection pool

/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
jedisPool = new JedisPool(config, addr, port, timeOut, auth);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}


public void setAddr(String addr) {
this.addr = addr;
}

public String getAddr() {
return addr;
}

 

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getAuth() {
return auth;
}

public void setAuth(String auth) {
this.auth = auth;
}

public int getMaxActive() {
return maxActive;
}

public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}

public int getMaxIdle() {
return maxIdle;
}

public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}

public int getMaxWait() {
return maxWait;
}

public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}

public int getTimeOut() {
return timeOut;
}

public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}

public boolean isTestOnBorrow() {
return testOnBorrow;
}

public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324925953&siteId=291194637