/**
* @类描述 redis 工具
* @功能名 POJO
* @author zxf
* @date 2014年11月25日
*/
public final class RedisUtil {

private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
//加载redis配置文件
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException("[redis.properties] is not found!");
}
int maxActivity = Integer.valueOf(bundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.valueOf(bundle.getString("redis.pool.maxIdle"));
long maxWait = Long.valueOf(bundle.getString("redis.pool.maxWait"));
boolean testOnBorrow = Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"));
boolean onreturn = Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"));

//创建jedis池配置实例
JedisPoolConfig config = new JedisPoolConfig();
//设置池配置项值
config.setMaxActive(maxActivity);
config.setMaxIdle(maxIdle);
config.setMaxWait(maxWait);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(onreturn);
jedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
} 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 String find(String key,String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}

/**
* 查询特定字符串
*/
public String findSubStr(String key,Integer startOffset,Integer endOffset){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 向缓存中设置字符串内容 新增数据|修改
* @param key key
* @param value value
* @return
* @throws Exception
*/
public static int add(String key,String value) throws Exception{
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}finally{
jedisPool.returnResource(jedis);
}
}

/**
* 删除缓存中得对象,根据key
* @param key
* @return
*/
public static int del(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}finally{
jedisPool.returnResource(jedis);
}
}

}

转载于:https://www.cnblogs.com/austinspark-jessylu/p/6088627.html

redis连接池操作相关推荐

  1. python redis连接池获取后关闭_python通过连接池连接redis,操作redis队列

    在每次使用redis都进行连接的话会拉低redis的效率,都知道redis是基于内存的数据库,效率贼高,所以每次进行连接比真正使用消耗的资源和时间还多.所以为了节省资源,减少多次连接损耗,连接池的作用 ...

  2. java操作redis redis连接池

    redis作为缓存型数据库,越来越受到大家的欢迎,这里简单介绍一下java如何操作redis. 1.java连接redis java通过需要jedis的jar包获取Jedis连接. jedis-2.8 ...

  3. Java的Redis连接池代码性能不错

    其实这个是引用自网友http://blog.csdn.net/tuposky/article/details/45340183,有2个版本,差别就是ReentrantLock和synchronized ...

  4. redis专题:redis键值设计、性能优化以及redis连接池配置

    文章目录 1.redis键值设计 ①:key设计规范 ②:value设计规范 2. 命令使用优化 3. redis连接池配置参数设计 4. redis连接池预热 5. redis的key过期删除策略 ...

  5. Java的Redis连接池代码

    2019独角兽企业重金招聘Python工程师标准>>> 其实这个是引用自网友http://blog.csdn.net/tuposky/article/details/45340183 ...

  6. python redis连接池_redis 连接池

    redis是一个key-value存储系统,和memcached类似,支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set-有 ...

  7. SpringBoot 配置 Redis 连接池

    前言 SpringBoot2.0默认采用 Lettuce 客户端来连接 Redis 服务 默认是不使用连接池的,只有配置 redis.lettuce.pool下的属性的时候才可以使用到redis连接池 ...

  8. Redis连接池配置详解

    连接池配置 文章目录 连接池配置 一.Redis连接池 二.jar包准备 三.编写代码配置,创建连接池,并调用处连接 总结 一.Redis连接池 与JDBC中在与数据库进行连接时耗时,从而需要引入连接 ...

  9. Java Redis 连接池 Jedis 工具类,java基础面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

最新文章

  1. 让手机跑SOTA模型快8倍!Facebook AI开源最强全栈视频库:PyTorchVideo!
  2. css实现文字超出显示省略号...
  3. hadoop 1.0.1集群安装及配置
  4. 十大经典算法排序总结对比
  5. linux之awk命令解读
  6. setTimeout和setInterval的区别
  7. Spring 容器(Spring 的上下文)
  8. VisualSVN安装图解
  9. f2fs学习笔记 - 9. f2fs 写文件
  10. 机器学习与深度学习常见面试题
  11. 1.3-----Simplify 3D切片软件简单设置
  12. HS100B音频芯片
  13. matlab 交互效应三维图,【MATLAB】使用MATLAB绘制心理学中的交互作用图
  14. echarts数字云
  15. 操作系统期末知识点浓缩总结复习
  16. IDEA 解决插件页面转圈问题
  17. Linux访问外网,有图和命令,简单易懂
  18. Java ByteBuffer用法
  19. android fastboot 命令集
  20. 多级分销系统(代理商佣金管理模块)设计概要(要求和数据库设计)

热门文章

  1. KVM脚本批量添加删除虚拟机
  2. RunJs使用评测(IE9)
  3. NDK集成libjpeg和libpng
  4. Python in worker has different version 3.7 than that in driver 3.6
  5. xfce4设置屏保/锁屏时间
  6. kaggle 相关知识汇总(转载+自己整理)
  7. Micro-CMS v2(持续更新中)
  8. pytorch 构建神经网络模型总结
  9. 线性代数导论4——A的LU分解
  10. http协议实现web服务器,http协议实现web服务器