拷贝

 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(示例)的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  3. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  4. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  5. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  6. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  9. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

随机推荐

  1. (原)用WebBrowser浏览Office Web Apps Server,除去“下载”按钮

    对,没错,如果你按关键字找到这篇随笔了,相信一定知道背景,以及我所说的是什么. 上一段子代码. private void timerHideButton_Tick(object sender, Eve ...

  2. Top N的MapReduce程序MapReduce for Top N items

    In this post we'll see how to count the top-n items of a dataset; we'll again use the flatland book ...

  3. xp局域网共享设置

    xp在局域网内的每一台机子做以下一些设置:1.启用Guest(来宾)帐户:控制面板--用户帐户或者在管理工具--计算机管理--本地用户和组--右键Guest属性--去掉帐户已停用 前的勾.2.允许Gu ...

  4. 第九章 LinkedBlockingQueue源码解析

    1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2.创建 Node节点内部类与LinkedBlockingQueue的一些属性 static ...

  5. c#逐行分元素读取记事本txt数据写进数据库

    其实这里最关键的一个方法是 StreamReader类里的 ReadLine();这个方法可以逐行读取txt流里面的数据.写了个简单的demo,已经加上了详细的注释说明.   ok,好了,不废话,下面 ...

  6. 升级iOS10后http网页定位失效解决方案

    最近我们在做项目时遇到这样一个新问题,用户在升级 iOS10 后,在 http 下使用 geolocation api 会报错,控制台输出 [blocked] Access to geolocatio ...

  7. svn 错误集锦续

    4,svn出错:Error: File or directory '.' is out of date; try updating 出错原因:SVN服务器端的版本比你的版本要新,不允许提交. 解决方案 ...

  8. Ngxtop-Nginx日志实时分析利器

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  9. zypper命令使用示例

    导读 Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zyppe ...

  10. 通过HTML5 Visibility API检测页面活动状态

    几年前,我们浏览网页的时候是没有选项卡浏览模式的,每一个网页都会是一个浏览器窗口,如果我没有记错,Win7之前我们都是这样浏览网页的.作为一个程序员,我们经常会同时打开10-15个网页,多的时候超过2 ...