redis操作帮助类
RedisHelper.java
import redis.clients.jedis.*;
import java.util.*;
public class RedisHelper {
private static JedisPool pool;
private static RedisHelper redisHelper = null;
/**
* 通过静态工厂方法来沟通对象,复用对象,避免每次重新产生新对象
*/
public static RedisHelper newInstance(String host, int port, String password, int maxIdle, int maxTotal, long maxWaitMillis) {
if (null != redisHelper)
return redisHelper;
synchronized (RedisHelper.class) {
if (null != redisHelper)
return redisHelper;
redisHelper = new RedisHelper(host, port, password, maxIdle, maxTotal, maxWaitMillis);
return redisHelper;
}
}
private RedisHelper(String host, int port, String password, int maxIdle, int maxTotal, long maxWaitMillis) {
if (null != pool)
return;
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle);//最大空闲连接数
config.setMaxTotal(maxTotal);//最大连接数
config.setTestOnBorrow(true);
config.setTestOnReturn(false);
config.setMaxWaitMillis(maxWaitMillis);
pool = new JedisPool(config, host, port, 10000, password);
}
/**
* 没有特别需求,请不要在外部调用此方法
*/
public Jedis getConnection() {
return pool.getResource();
}
public void returnConnection(Jedis conn) {
//自Jedis3.0版本后jedisPool.returnResource()遭弃用,官方重写了Jedis的close方法用以代替
if (null != conn) {
conn.close();
}
}
public Pipeline pipeline() {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.pipelined();
} finally {
this.returnConnection(conn);
}
}
public Set<String> keys(String pattern) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.keys(pattern);
} finally {
this.returnConnection(conn);
}
}
public void set(String key, String value) {
Jedis conn = null;
try {
conn = this.getConnection();
conn.set(key, value);
} finally {
this.returnConnection(conn);
}
}
public long setNx(String key, String value) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.setnx(key, value);
} finally {
this.returnConnection(conn);
}
}
public String get(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.get(key);
} finally {
this.returnConnection(conn);
}
}
public String getSet(String key, String value) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.getSet(key, value);
} finally {
this.returnConnection(conn);
}
}
public long del(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.del(key);
} finally {
this.returnConnection(conn);
}
}
public long del(String... keys) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.del(keys);
} finally {
this.returnConnection(conn);
}
}
/**
* 若 key 存在,返回 true ,否则返回 false 。
*/
public boolean exists(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.exists(key);
} finally {
this.returnConnection(conn);
}
}
/**
* 设置成功,返回 1, key 不存在或设置失败,返回 0
*/
public long pexpire(String key, long milliseconds) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.pexpire(key, milliseconds);
} finally {
this.returnConnection(conn);
}
}
public long hset(String key, String field, String value) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hset(key, field, value);
} finally {
this.returnConnection(conn);
}
}
/**
* 将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。
* 若域 field 已经存在,该操作无效。
* 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令。
*/
public long hsetnx(String key, String field, String value) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hsetnx(key, field, value);
} finally {
this.returnConnection(conn);
}
}
public long hdel(String key, String... fields) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hdel(key, fields);
} finally {
this.returnConnection(conn);
}
}
public Set<String> hkeys(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hkeys(key);
} finally {
this.returnConnection(conn);
}
}
public Map<String, String> hgetAll(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hgetAll(key);
} finally {
this.returnConnection(conn);
}
}
public String hget(String key, String field) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hget(key, field);
} finally {
this.returnConnection(conn);
}
}
public long hincrBy(String key, String field, Long value) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.hincrBy(key, field, value);
} finally {
this.returnConnection(conn);
}
}
public Long rpush(String key, String... strings) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.rpush(key, strings);
} finally {
this.returnConnection(conn);
}
}
/**
* 返回元素个数
*/
public Long lpush(String key, String... strings) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.lpush(key, strings);
} finally {
this.returnConnection(conn);
}
}
public String lpop(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.lpop(key);
} finally {
this.returnConnection(conn);
}
}
public String rpop(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.rpop(key);
} finally {
this.returnConnection(conn);
}
}
public List<String> lrange(String key, long start, long end) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.lrange(key, start, end);
} finally {
this.returnConnection(conn);
}
}
public long sadd(String key, String... members) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.sadd(key, members);
} finally {
this.returnConnection(conn);
}
}
public Set<String> sinter(String... keys) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.sinter(keys);
} finally {
this.returnConnection(conn);
}
}
public long scard(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.scard(key);
} finally {
this.returnConnection(conn);
}
}
public String spop(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.spop(key);
} finally {
this.returnConnection(conn);
}
}
public long srem(String key, String... members) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.srem(key, members);
} finally {
this.returnConnection(conn);
}
}
public Set<String> smembers(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.smembers(key);
} finally {
this.returnConnection(conn);
}
}
public Set<String> sunion(String... keys) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.sunion(keys);
} finally {
this.returnConnection(conn);
}
}
public boolean sismember(String key, String member) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.sismember(key, member);
} finally {
this.returnConnection(conn);
}
}
public long increment(String key, long amount) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.incrBy(key, amount);
} finally {
this.returnConnection(conn);
}
}
public double increment(String key, double amount) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.incrByFloat(key, amount);
} finally {
this.returnConnection(conn);
}
}
/**
* 列表长度
*/
public long llen(String key) {
Jedis conn = null;
try {
conn = this.getConnection();
return conn.llen(key);
} finally {
this.returnConnection(conn);
}
}
}
redis操作帮助类的更多相关文章
- Redis 操作帮助类
首先从Nuget中添加StackExchange.Redis包 1.Redis连接对象管理帮助类 using Mvc.Base; using Mvc.Base.Log; using StackExch ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- Java的redis 操作类-优化通用版本
java操作redis多节点处理方式;http://blog.itpub.net/29254281/viewspace-1188644/首先maven引入依赖包 <dependency> ...
- 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类
1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...
- spring 的redis操作类RedisTemplate
spring 集成的redis操作几乎都在RedisTemplate内了. 已spring boot为例, 再properties属性文件内配置好 redis的参数 spring.redis.host ...
随机推荐
- python 中文分词:结巴分词
中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词.其基本实现原理有三点: 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG) 采用了动态规 ...
- php7.0.24-nts配置步骤
php-cgi.exe文件运行 php-cgi.exe -b 127.0.0.1:9000 -c D:/myphp2017/php7-nts/php.ini nginx命令 启动 start ngin ...
- OAuth2.0和企业内部统一登录,token验证方式,OAuth2.0的 Authorization code grant 和 Implicit grant区别
统一登录是个很多应用系统都要考虑的问题,多个项目的话最好前期进行统一设计,否则后面改造兼容很麻烦: cas认证的方式:新公司都是老项目,用的是cas认证的方式,比较重而且依赖较多,winform的项目 ...
- CentOS7中添加新硬盘
cp /etc/fstab /etc/fstab.bak echo /dev/vdb1 /mnt/disk1 ext4 defaults 0 0 >> /etc/fstab mkdir ...
- MVC4笔记 @functions @model @using
/////@functions自定义函数////// @helper辅助方法的确可以很方便的完成辅助方法开发,不过却失去了一些弹性,例如,无法在@helper中自定义属性,只能单纯的传入参数,然后格式 ...
- Servlet细节整合
最近在复习Servlet,发现其中有很多细节方面的东西都没有接触到,只是学了大概 1.请求转发和请求重定向的区别 2.输入参数为中文时候的乱码问题 3.Web工程中的目录写法 下面分别阐述 1.请求转 ...
- eclipse 创建 user library 方法
1.Window - Preferences - Java - Build Path - User Libraries 2.新建 UserLibraries 3. 4.重复上一步依次添加需要的jar文 ...
- 算法练习-Palindrome Number
判断回文整数 来源 https://leetcode.com/problems/palindrome-number/ 要求 判断一个整数是不是回文数,尽量减少内存暂用. 思路 可能的情况: 负数的应当 ...
- Linux命令之查看日志等实时文件命令(less 、tail)使用
一.less的使用 1)less 文件名,即可快速打开文件 2)相关查看搜索 3)利用键盘向上向下箭头键盘上的向上和向下箭头,点击一次向下简单,文件内容往下读取一行:点击一次向上箭头,文件内容,往上 ...
- 基于FPGA的VGA显示设计(二)
上一篇:基于FPGA的VGA显示设计(一) 参照 CrazyBingo 的 基于FPGA的VGA可移植模块终极设计代码 的工程代码风格,模块化处理了上一篇的代码,并增加了一点其它图形. 顶层 ...