拷贝

 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. 求助关于jquery easyUI中的treegrid组件,请各位帮忙给个思路,谢谢啦

    现在项目中用到jquery easyUI中的treegrid组件,已经可以正常显示了.但是在保存的时候遇到问题,页面上参照官网的例子可以在页面更新,但是怎么获取编辑后的数据进而保存到数据库呢?

  2. Action过滤器使用实例(一)

    1.实例一 /// <summary> /// 需要用户登陆的 action,执行提前验证 /// </summary> public class LoginFilterAtt ...

  3. 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition

    Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...

  4. PASCAL VOC数据集The PASCAL Object Recognition Database Collection

    The PASCAL Object Recognition Database Collection News 04-Apr-07: The VOC2007 challenge development ...

  5. poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重

    挺不错的题目,很锻炼代码能力和调试能力~ 题意:初始格子状态固定,给你移动后格子的状态,问最少需要多少步能到达,如果步数大于30,输出-1. 由于单向搜索状态太多,搜到二十几就会爆了,所以应该想到双向 ...

  6. Linux,Windows和UNIX的进程调度的分析

    摘要 : 本文以Linux ,Unix ,Windows 操作系统为例,分析其进程调度策略,以期对进程调度过程有更深层次的认识     关键词 : 进程调度 优先级 时间片轮转 实时进程 分时技术   ...

  7. [Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined、vuejs路由使用的问题Error in render function

    1.[Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined 注意,只要出现Error i ...

  8. 理解JavaScript私有作用域

    私有作用域:跟外界的变量方法毫不冲突,豪无关系 var str ="javascript"; (function(){ alert(str); //undefined var st ...

  9. 火速提升Android仿真器的运行速度 ——仿真器Genymotion

    一.问题概述 Android开发中会使用仿真器测试应用,但不管你使用Eclispe ADT还是Android Studio仿真器都是基于arm架构的,运行起来都很慢,光启动就要花费很多时间,都不知道它 ...

  10. VS2008:Failed to return new Code Element

    VS2008添加自动化类,报错:   [解决方法1] This can be fixed by installing SP1. Please see  https://connect.microsof ...