使用JedisPool
package id;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisStandAlone {
// 声明一个池
private static JedisPool pool = null;
/**
* 根据配置构建redis连接池
*
* @return JedisPool redis连接池
*/
private static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000 * 2);
config.setTestOnBorrow(true);
pool = new JedisPool(config, "127.0.0.1", 6379);
}
return pool;
}
/**
* 回收到连接池
*
* @param pool 连接池
* @param redis 连接实例
*/
private static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.close();
}
}
/**
* 获取数据
*
* @param key 键
* @return 结果
*/
public static String get(String key) {
String result = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.get(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return result;
}
/**
* 设置key的value
*
* @param key 键
* @param value 值
* @return 结果
*/
public static String set(String key, String value) {
String result = "";
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.set(key, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//回收到连接池
returnResource(pool, jedis);
}
return result;
}
}
使用ShardedJedisPool,在spring中注入
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="2"/>
<property name="maxWaitMillis" value="2000"/>
<property name="testOnBorrow" value="true"/>
bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo.default"/>
list>
constructor-arg>
bean>
<bean id="jedis.shardInfo.default" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="127.0.0.1"/>
<constructor-arg index="1" type="int" value="6379"/>
bean>
package id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 分布式Redis操作工具类
*
* @Authur 17040479
*/
@Component("redisUtil")
public class RedisUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
@Autowired
private ShardedJedisPool shardedJedisPool;
/**
* 获取redis客户端连接实例
*
* @return shardJedis
*/
public ShardedJedis getRedisClient() {
ShardedJedis shardJedis = null;
try {
shardJedis = shardedJedisPool.getResource();
return shardJedis;
} catch (Exception e) {
LOGGER.error("[JedisDS] getRedisClent error:" + e.getMessage());
if (null != shardJedis)
shardJedis.close();
}
return null;
}
/**
* 回收实例到连接池
*
* @param shardedJedis 回收实例
*/
public void returnResource(ShardedJedis shardedJedis) {
shardedJedis.close();
}
/**
* 回收实例到连接池
*
* @param shardedJedis 回收实例
* @param broken 是否
*/
public void returnResource(ShardedJedis shardedJedis, boolean broken) {
shardedJedis.close();
}
public String set(String key, String value) {
String result = null;
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
return result;
}
/**
* 获取单个key的值
*
* @param key 键
* @return 值
*/
public String get(String key) {
String result = null;
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.get(key);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
return result;
}
/**
* 执行lua脚本
*
* @param luaScript
* @param key
* @param val
* @param expireSeconds
* @return
*/
public Object eval(final String luaScript, final String key, String val, final long expireSeconds) {
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
throw new RuntimeException("hmset 获取redis连接失败");
}
Jedis jedis = shardedJedis.getShard(key);
boolean broken = false;
try {
// 参数KEY
List keys = new ArrayList<>();
keys.add(key);
keys.add(key);
List args = new ArrayList<>();
args.add(val);
args.add(String.valueOf(expireSeconds));
return jedis.eval(luaScript, keys, args);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
return null;
}
/**
* setnx
*
* @param key
* @param value
* @param nx
* @param px
* @param expire
* @return
*/
public String setnx(String key, String value, String nx, String px, long expire) {
String result = "";
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.set(key, value, nx, px, expire);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
return result;
}
/**
* del
* @param key
*/
public void del(String key) {
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
throw new RuntimeException("del 获取redis连接失败");
}
boolean broken = false;
try {
shardedJedis.del(key);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
}
public void hmset(String key, Map map) {
ShardedJedis shardedJedis = getRedisClient();
if (shardedJedis == null) {
throw new RuntimeException("hmset 获取redis连接失败");
}
boolean broken = false;
try {
shardedJedis.hmset(key, map);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
broken = true;
} finally {
returnResource(shardedJedis, broken);
}
}
}