redis连接池操作
/**
* @类描述 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);
}
}
}
redis连接池操作的更多相关文章
- java操作redis redis连接池
redis作为缓存型数据库,越来越受到大家的欢迎,这里简单介绍一下java如何操作redis. 1.java连接redis java通过需要jedis的jar包获取Jedis连接. jedis-2.8 ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- Django day35 redis连接池,redis-list操作,django中使用redis,支付宝支付
一:redis连接池, 二:redis-list操作, 三:django中使用redis, 四:支付宝支付
- 三:Redis连接池、JedisPool详解、Redisi分布式
单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...
- Redis连接池
package com.lee.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; impor ...
- redis连接池 jedis-2.9.0.jar+commons-pool2-2.4.2.jar
java使用Redis连接池 jar包为 jedis-2.9.0.jar+commons-pool2-2.4.2.jar jar下载地址 package com.test; import redis ...
- redis 连接池
redis是一个key-value存储系统,和memcached类似,支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set-有 ...
- python 基础 10.0 nosql 简介--redis 连接池及管道
一. NOSQL 数据库简介 NoSQL 泛指非关系型的数据库.非关系型数据库与关系型数据库的差别 非关系型数据库的优势: 1.性能NOSQL 是基于键值对的,可以想象成表中的主键和值的对应关系,而且 ...
- golang开发:类库篇(二) Redis连接池的使用
为什么要使用连接池 一个数据库服务器只拥有有限的连接资源,一旦所有的连接资源都在使用,那么其它需要连接的资源就只能等待释放连接资源.所以,在连接资源有限的情况下,提高单位时间的连接的使用效率,缩短连接 ...
随机推荐
- Hadoop基本命令详解
调用文件系统(FS)Shell命令应使用bin/hadoop fs <args>的形式.所有的的FS shell命令使用URI路径作为参数.URI路径详解点击这里. 1.cat 说明:将路 ...
- javascript实现页面右侧在线客服始终跟随鼠标滚动而上下滚动且始终位于屏幕中间
效果如图,右侧的联系一栏始终位于页面的中间位置,且随着页面的上下滚动而滚动跟随 css的话没什么好说的,看图 代码 window.onload=window.onresize=window.onscr ...
- Vultr新加坡机房速度怎么样?值得购买吗?最新评测!
2016年9月,Vultr vps开通了新加坡Singapore机房线路.与知名的竞争对手Digitalocean和Linode一样,新加坡机房对亚洲速度友好,是讨用户欢心的一个进步. 但是,vult ...
- Linux学习初步
centOS 6.5关闭防火墙步骤 关闭命令: service iptables stop 永久关闭防火墙:chkconfig iptables off两个命令同时运行,运行完成后 ...
- android log4j日志管理的使用
以下为log4j1的日志管理,在android 6.0 一下能正常使用,时候更加高级的胃log4j2,持续跟新 android中的log4j日志文件使用需要两个包,我们不需要进行配置文件的配置,一切都 ...
- redis写shell与ssh免密码登陆
redis-cli参数:-h :指定要连接的主机IP或域名-p :指定连接的端口-a :指定密码-r :执行指定的命令-n :数据库名-x :将最后一个参数输出为value redis写shell- ...
- 【Time系列一】datetime的妙用
今天在弄个自动关机小脚本的时候,遇到了时间转换的问题,也难怪,以前没学过, 不能怪我不会哦! 首先,先学会打印出当前时间的几种方式 参考开源社区: http://my.oschina.net/u/1 ...
- Excel 复制Sql查询结果错位
SELECT TOP 30000 REPLACE(REPLACE(T1.ReceiverName,CHAR(10),' '),CHAR(13),' ') AS ReceiverName, REPLAC ...
- C#发送邮件类
一.定义邮件发送类 /// <summary> /// 发送邮件 /// </summary> public class MailHelper { #region 私有变量 p ...
- linux内存管理(repost)
一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...