JedisClient(示例)
拷贝
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; public class JedisClient{ private JedisCluster pool; public String get(String key) {
return get(-1,key, new TypeReference<String>(){});
} public String get(int db, String key) {
return get(db, key, new TypeReference<String>(){});
} public <T> T get(String key, TypeReference<T> tr) {
return get(-1, key, tr);
} public <T> T get(int db, String key, TypeReference<T> tr) { if(StringUtils.isBlank(key)){
return null;
} T result = null; JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } String value = redis.get(key); if(StringUtils.isBlank(value)){
return null;
} return (T) JSON.parseObject(value, tr); } catch (Exception e) {
borrowOrOprSuccess = false;
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } } return result;
} public <T> boolean set(String key, T value) {
return set(key, 0, value);
} public <T> boolean set(String key, int TTL, T value) {
return set(-1, key, TTL, value);
} public <T> boolean set(int db, String key, int TTL, T value) { if(value == null){
return false;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } if(TTL == 0){
redis.set(key, JSON.toJSONString(value));
}else {
redis.setex(key, TTL, JSON.toJSONString(value));
} } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } return false; } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return true;
} public boolean delete(String key) {
return delete(-1,key);
} public boolean delete(int db, String key) { if(StringUtils.isBlank(key)){
return false;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
redis.del(key); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } return false; } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return true; } public boolean expire(String key, int TTL) {
return expire(-1, key, TTL);
} public boolean expire(int db,String key, int TTL) {
JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
redis.expire(key, TTL); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return true;
} public String hget(int db,String key, String field) { if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
return null;
} String result = null; JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hget(key, field); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return result;
} public String hget(String key, String field) {
return hget(-1, key,field);
} public byte[] hget(int db, byte[] key, byte[] field) { if (key == null || key.length == 0 || field == null || field.length == 0) {
return null;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hget(key, field); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return null;
} public byte[] hget(byte[] key, byte[] field) {
return hget(-1, key, field);
} public int hsetnx(int db,String key, String field, String value) { if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
return 0;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hsetnx(key, field, value).intValue(); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return 0;
} public int hsetnx(String key, String field, String value) {
return hsetnx(-1, key, field, value);
} public int hset(int db, byte[] key, byte[] field, byte[] value) { if (key == null || key.length ==0 || field == null || field.length == 0 || value == null || value.length == 0) {
return -1;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hset(key, field, value).intValue(); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return -1;
} public int hset(byte[] key, byte[] field, byte[] value) {
return hset(-1, key, field, value);
} public Map<String, String> hgetAll(String key) { return hgetAll(-1, key);
} public Map<String, String> hgetAll(int db,String key) { if (StringUtils.isBlank(key)) {
return null;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hgetAll(key); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } } return null;
} public Map<byte[],byte[]> hgetAll(int db,byte[] key) { if (key == null || key.length == 0) {
return null;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool; // if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// } return redis.hgetAll(key); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return null;
} public Map<byte[],byte[]> hgetAll(byte[] key) { return hgetAll(-1, key);
} public int hset(String key, String field, String value) {
return hset(-1, key, field, value);
} public int hset(int db,String key, String field, String value) { if (StringUtils.isBlank(key) || StringUtils.isBlank(field)
|| StringUtils.isBlank(value)) {
return -1;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
return redis.hset(key, field, value).intValue();
} catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
return -1; } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
} public Set<String> hkeys(String key) {
return hkeys(-1,key);
} public Set<String> hkeys(int db,String key) { if (StringUtils.isBlank(key)) {
return new HashSet<String>();
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
return redis.hkeys(key);
} catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
return new HashSet<String>(); } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
} public boolean hdel(int db,String key, String field) { if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
return false;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
redis.hdel(key, field); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
return false; } finally {
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} return true; } public boolean hdel(String key, String field) {
return hdel(-1, key, field);
} public boolean hexists(String key, String field) {
return hexists(-1, key, field);
} public boolean hexists(int db,String key, String field) { if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
return false;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
return redis.hexists(key, field); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
return false; } finally {
if (borrowOrOprSuccess){
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
} public int setnx(String key, String value) {
return setnx(-1, key, value);
} public int setnx(int db,String key, String value) { if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
return -1;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
return redis.setnx(key, value).intValue(); } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } return 0; } finally {
if (borrowOrOprSuccess){
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
} public Set<String> keys(String pattern) {
return keys(-1, pattern);
} public Set<String> keys(int db,String pattern) {
JedisCluster redis = null;
Set<String> keysSet = new HashSet<String>(); boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
Map<String, JedisPool> clusterNodes = redis.getClusterNodes();
for(String k : clusterNodes.keySet()){
JedisPool jp = clusterNodes.get(k);
Jedis connection = jp.getResource();
try {
keysSet.addAll(connection.keys(pattern));
} catch(Exception e){
} finally{
connection.close();//用完一定要close这个链接!!!
}
} } catch (Exception e) {
borrowOrOprSuccess = false;
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
} finally {
if (borrowOrOprSuccess){
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
} return keysSet;
} // public long publish(String channel, String message){
// Jedis redis = null;
//
// boolean borrowOrOprSuccess = true;
//
// try {
// redis = pool.getResource();
// return redis.publish(channel, message);
//
// } catch (Exception e) {
// borrowOrOprSuccess = false;
// if (redis != null)
// pool.returnBrokenResource(redis);
// } finally {
// if (borrowOrOprSuccess)
// pool.returnResource(redis);
// }
//
// return -1;
// }
//
// public void subscribe(JedisPubSub pubSub, String... channel){
// Jedis redis = null;
//
// boolean borrowOrOprSuccess = true;
//
// try {
// redis = pool.getResource();
// redis.subscribe(pubSub, channel);
// } catch (Exception e) {
// borrowOrOprSuccess = false;
// if (redis != null)
// pool.returnBrokenResource(redis);
// } finally {
// if (borrowOrOprSuccess)
// pool.returnResource(redis);
// }
// }
//
// public void psubscribe(JedisPubSub pubSub, String... patterns){
// Jedis redis = null;
//
// boolean borrowOrOprSuccess = true;
//
// try {
// redis = pool.getResource();
// redis.psubscribe(pubSub, patterns);
// } catch (Exception e) {
// borrowOrOprSuccess = false;
// if (redis != null)
// pool.returnBrokenResource(redis);
// } finally {
// if (borrowOrOprSuccess)
// pool.returnResource(redis);
// }
// } public long ttl(int db, String key) {
if(StringUtils.isBlank(key)){
return 0;
} JedisCluster redis = null; boolean borrowOrOprSuccess = true; try {
redis = pool;
// if(db >= 0){
// redis.select(db);
// }else {
// redis.select(0);
// }
return redis.ttl(key.getBytes()); } catch (Exception e) {
borrowOrOprSuccess = false;
// if (redis != null)
// try {
// pool.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } return 0; } finally {
// if (borrowOrOprSuccess)
// try {
// pool.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
} public void setPool(RedisPool pool) {
this.pool = pool.getCluster();
}
}
JedisClient(示例)的更多相关文章
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
随机推荐
- java环境配置错误集锦
eclipse生成的文件目录 D:\eeworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps 1.java. ...
- C预编译, 预处理, C/C++头文件, 编译控制,
在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的 ...
- Maclean Liu对Oracle Database 12c新特性研究汇总
Maclean Liu关于DB 12c新特性的研究文章如下: [Oracle Database 12c新特性] In-Database Archiving数据库内归档 [Oracle Database ...
- 【Dagger2】 案例大全
只有Inject是不可以的,必须有Component public class Test { @Inject Person person; private void test() { System.o ...
- Quartz2D-二维画图引擎 、自己定义UI控件
// // MyDraw.m // 绘图 #import "MyDraw.h" @implementation MyDraw //Quartz2D 是一个二维绘图引擎 //自己定义 ...
- 25个iptables常用示例
本文将给出25个iptables常用规则示例,这些例子为您提供了些基本的模板,您可以根据特定需求对其进行修改调整以达到期望. 格式 iptables [-t 表名] 选项 [链名] [条件] [-j ...
- kettle利用参数遍历执行指定目录下的所有对象
使用kettle设计ETL设计完成后,我们就需要按照我们业务的需要对我们设计好的ETL程序,ktr或者kjb进行调度,以实现定时定点的数据抽取,或者说句转换工作,我们如何实现调度呢? 场景:在/wor ...
- 【Python】理想论坛帖子读取爬虫1.04版
1.01-1.03版本都有多线程争抢DB的问题,线程数一多问题就严重了. 这个版本把各线程要添加数据的SQL放到数组里,等最后一次性完成,这样就好些了.但乱码问题和未全部完成即退出现象还在,而且速度上 ...
- 隐马尔可夫模型HMM与维特比Veterbi算法(一)
隐马尔可夫模型HMM与维特比Veterbi算法(一) 主要内容: 1.一个简单的例子 2.生成模式(Generating Patterns) 3.隐藏模式(Hidden Patterns) 4.隐马尔 ...
- (LeetCode 203)Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...