public class RedisClient {
public static Logger log = Logger.getLogger(RedisClient.class);
/**
* jedis 连接池
*/
private static JedisPool jedisPool;
@Resource
public void setJedisPool(JedisPool jedisPool) {
RedisClient.jedisPool = jedisPool;
}
/**
* 释放连接对象
* @Title:closeShardedJedis
* @param jedis
* @return void
* @throws
*/
public static void closeJedis(Jedis jedis) {
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}
/**
* 释放异常的连接对象
* @Title:closeShardedJedis
* @param jedis
* @return void
* @throws
*/
public static void closeBrokenJedis(Jedis jedis) {
if (null != jedis) {
jedisPool.returnBrokenResource(jedis);
}
}
/**
* 获取连接对象
* @Title:getShardedJedis
* @return ShardedJedis
* @throws
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 根据redis的key找到对应的值
* @param jediskey redis键
* @return //返回redis储存信息
*/
public static String getJedisVal(String jediskey) {
Jedis jedis = null;
try {
jedis = RedisClient.getJedis();
String value = jedis.get(jediskey);
return value;
} catch (Exception e) {
log.error("key:" + jediskey + "未能取到redis的值.");
closeBrokenJedis(jedis);
return null;
} finally {
closeJedis(jedis);
}
}
/**
* 存储redis的键值不设置时间
*
* @param jediskey redis键
* @param jedisval redis值
*/
public static void setJedisVal(String jediskey, String jedisval) {
setJedisVal(jediskey, jedisval, 0);
}
/**
* 设置时间存储redis
* @param jediskey redis键
* @param jedisval redis值
* @param seconds 时间(秒)
*/
public static void setJedisVal(String jediskey, String jedisval, int seconds) {
Jedis jedis = null;
try {
jedis = RedisClient.getJedis();
if (seconds > 0) {
jedis.setex(jediskey, seconds, jedisval);
} else {
jedis.set(jediskey, jedisval);
}
} catch (Exception e) {
e.printStackTrace();
closeBrokenJedis(jedis);
} finally {
closeJedis(jedis);
}
}
/**
* 根据redis键值删除redis
* @param jediskey 键值
*/
public static void delJedis(String jediskey) {
Jedis jedis = null;
try {
jedis = RedisClient.getJedis();
jedis.del(jediskey);
} catch (Exception e) {
e.printStackTrace();
closeBrokenJedis(jedis);
} finally {
closeJedis(jedis);
}
}
/**
* 序列化
* @author renzhenjiang
* @date 2014-9-30 下午04:09:03
* @param object
* @return
* @see [类、类#方法、类#成员]
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
//序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
bytes = baos.toByteArray();
} catch (Exception e) {
System.out.println("序列化运行异常");
}
return bytes;
}
/**
* 反序列化
* @author renzhenjiang
* @date 2014-9-30 下午04:10:06
* @param bytes
* @return
* @see [类、类#方法、类#成员]
*/
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
Object obj = null;
try {
//反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
obj = ois.readObject();
} catch (Exception e) {
System.out.println("反序列化异常");
}
return obj;
}
public static void main(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(666300);
config.setMaxIdle(20);
config.setMaxWait(1000);
jedisPool = new JedisPool(config, "192.168.1.250", 6379);
Jedis jedis = jedisPool.getResource();
System.out.println("statistics_18310813001reg1_count === "+jedis.get("statistics_18310813001reg1"));
System.out.println("statistics_18310813001reg2_count === "+jedis.get("statistics_18310813001reg2"));
System.out.println("statistics_18310813001pwd1_count === "+jedis.get("statistics_18310813001pwd1"));
System.out.println("statistics_18310813001pwd2_count === "+jedis.get("statistics_18310813001pwd2"));
//jedis.del("statistics_18310813001reg1");
//System.out.println("statistics_18310813001reg1_count === "+jedis.get("statistics_18310813001reg1"));
}
}
代码笔记 | Redis的使用
最新推荐文章于 2024-09-05 20:28:26 发布