java代码简单封装使用redis
package com.ray.redis;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class RedisUtil {
/**
* 日志记录
*/
protected final Logger log = Logger.getLogger(RedisUtil.class);
// redis服务器IP
private static String ADDR = "127.0.0.1";
// redis的端口号
private static int PORT = 6379;
// 访问密码,因为开机关机,redis会自动清除所有的东西,所以没有必要设置密码
private static String AUTH = "stockea";
// 可用连接实例的最大数目,默认为8
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此
// 时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
// 控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认也是8
private static int MAX_IDLE = 200;
// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
// 如果超过等待时间,则直接抛出JedisConnectionException
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
// 在borrow一个redis实例时,是否提前进行validate操作;
// 如果为true,则得到的jedis实例均是可用的
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池 Jedis的连接池配置需要用到
*
* 所以要引入commons-pool.jar 版本必须是 2+以前
* */
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
// 老版本是setMaxActive
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
// 老版本是maxMaxWait
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);// 有密码的时候传入AUTH
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* */
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资源
* */
@SuppressWarnings("deprecation")
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResourceObject(jedis);
}
}
/**
* 获取List集合
*
* @param key
* 存入的key
* @param begin
* @param end
* @param inseList
* 插入的集合
* @return
*/
public static List<String> getList(String key, int begin, int end) {
Jedis jedis = RedisUtil.getJedis();
List<String> list = new ArrayList<String>();
try {
if (begin < 0) {
throw new IllegalAccessError("遍历lrange 开始位置不能小于0");
}
if (begin == end) {
throw new IllegalAccessError("遍历lrange 开始位置不能等于结束位置");
} else {
list = jedis.lrange(key, begin, end);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
return list;
}
/**
* 保存List集合
*
* @param key
* 存入的key
* @param begin
* @param end
* @param saveList
* 插入的集合
* @return
*/
public static void saveList(String key, List<String> saveList) {
Jedis jedis = RedisUtil.getJedis();
try {
if (saveList.size() == 0) {
throw new IllegalAccessError("保存的集合大小不能为0");
} else {
for (int i = 0; i < saveList.size(); i++) {
jedis.lpush(key, saveList.get(i));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
}
/**
* 根据key来获取对应的value
*
* @param key
* @return
*/
public static String getStringValue(String key) {
Jedis jedis = RedisUtil.getJedis();
String valueString = null;
try {
boolean falg = jedis.exists(key);
if (falg == true) {
valueString = jedis.get(key);
} else {
throw new IllegalAccessError("根据key查询时,key不存在!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
return valueString;
}
/**
* 根据key来保存对应的value
*
* @param key
* @return
*/
public static void saveStringValue(String key,String value) {
Jedis jedis = RedisUtil.getJedis();
try {
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
}
/**
* 判断key是否存在
* 如果存在
* 删除
*
*/
/**
* 根据key来获取对应的value
*
* @param key
* @return
* @return
*/
public static void delteKey(String key) {
Jedis jedis = RedisUtil.getJedis();
try {
boolean falg = jedis.exists(key);
if (falg == true) {
jedis.del(key);
} else {
throw new IllegalAccessError("根据key查询时,key不存在!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
}
/**
* 清空所有的 keys
* 执行时,慎重!
*/
public static void deleteAllKeys(){
Jedis jedis = RedisUtil.getJedis();
try {
jedis.flushAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisUtil.returnResource(jedis);
}
}
}
需要的jar包:
