import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class MyJedisPool { private final static Logger logger = LoggerFactory.getLogger(MyJedisPool.class); private static JedisPool readPool = null;
private static JedisPool writePool = null; //静态代码初始化池配置
static {
try{
Properties props = new Properties();
InputStream in = MyJedisPool.class.getResourceAsStream("/redis.properties");
props.load(in); //创建jedis池配置实例
JedisPoolConfig config = new JedisPoolConfig(); //设置池配置项值
config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle")));
config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn"))); //根据配置实例化jedis池
readPool = new JedisPool(config, props.getProperty("redisReadURL"), Integer.valueOf(props.getProperty("redisReadPort")));
writePool = new JedisPool(config, props.getProperty("redisWriteURL"), Integer.valueOf(props.getProperty("redisWritePort"))); }catch (IOException e) {
logger.info("redis连接池异常",e);
}
} /**获得jedis对象*/
public static Jedis getReadJedisObject(){
return readPool.getResource();
}
/**获得jedis对象*/
public static Jedis getWriteJedisObject(){
return writePool.getResource();
} /**归还jedis对象*/
public static void returnJedisOjbect(Jedis jedis){
if (jedis != null) {
jedis.close();
}
} }

  

import redis.clients.jedis.Jedis;

import java.util.Set;

public class RedisUtils {

    /**
* 获取hash表中所有key
* @param name
* @return
*/
public static Set<String> getHashAllKey(String name){
Jedis jedis = null;
try {
jedis = MyJedisPool.getReadJedisObject();
return jedis.hkeys(name);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 从redis hash表中获取
* @param hashName
* @param key
* @return
*/
public static String getHashKV(String hashName,String key){
Jedis jedis = null;
try {
jedis = MyJedisPool.getReadJedisObject();
return jedis.hget(hashName, key);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 删除hash表的键值对
* @param hashName
* @param key
*/
public static Long delHashKV(String hashName,String key){
Jedis jedis = null;
try {
jedis = MyJedisPool.getWriteJedisObject();
return jedis.hdel(hashName,key);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 存放hash表键值对
* @param hashName
* @param key
* @param value
*/
public static Long setHashKV(String hashName,String key,String value){
Jedis jedis = null;
try {
jedis = MyJedisPool.getWriteJedisObject();
return jedis.hset(hashName,key,value);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 删除键值对
* @param k
* @return
*/
public static Long delKV(String k){
Jedis jedis = null;
try {
jedis = MyJedisPool.getWriteJedisObject();
return jedis.del(k);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 放键值对
* 永久
* @param k
* @param v
*/
public static String setKV(String k, String v)
{
Jedis jedis = null;
try {
jedis = MyJedisPool.getWriteJedisObject();
return jedis.set(k, v);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 放键值对
*
* @param k
* @param v
*/
public static String setKV(String k,int second, String v)
{
Jedis jedis = null;
try {
jedis = MyJedisPool.getWriteJedisObject();
return jedis.setex(k,second, v);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} /**
* 根据key取value
*
* @param k
* @return
*/
public static String getKV(String k)
{
Jedis jedis = null;
try {
jedis = MyJedisPool.getReadJedisObject();
return jedis.get(k);
}catch (Exception e){
e.printStackTrace();
}finally {
MyJedisPool.returnJedisOjbect(jedis);
}
return null;
} }

  

最新文章

  1. Win7 64位ORACLE取数字乱码的解决
  2. Centos6.5 下安装PostgreSQL9.4数据库
  3. 成都Uber优步司机奖励政策(3月5日)
  4. Java面向对象的概念以及OOP思想的优点
  5. 设置MySQL数据表主键
  6. C期未考试参考答案题1
  7. c# 操作 MongoDB 的 第三方类库 MongoRepository
  8. JAVA短信验证登录
  9. Centos 6.5开启rsync同步
  10. 【转载】CANoe 入门 Step by step系列(一)基础应用
  11. Asp.Net Boilerplate Project 使用swagger调试api
  12. MYSQL 表大小限制
  13. 2.18比赛(T2,T3留坑)
  14. java.lang.UnsupportedClassVersionError: xxx/xxxClass : Unsupported major.minor version 51.0【转】
  15. skearn/pandas
  16. 侯捷STL课程及源码剖析学习2: allocator
  17. 微信小程序自定义模态框(字体图标)
  18. (6)time&amp;datetime(时间模块)
  19. Python处理PDF及生成多层PDF
  20. cocos2d-x 数据存储

热门文章

  1. HDU3652:B-number——题解
  2. bzoj2083: [Poi2010]Intelligence test(二分+vector)
  3. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
  4. poj3177 BZOJ1718 Redundant Paths
  5. SDWebImage的使用说明
  6. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集
  7. 使用Android Studio调试UiAutomator过程中遇到的问题
  8. JAVA 枚举单例模式
  9. 网页导出excel文件
  10. vijos 1448 校门外的树 树状数组