拷贝

 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. 阿里巴巴分布式服务框架 Dubbo

    1.Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo自2011年开源 ...

  2. Guava 源码分析之 Beta, GwtCompatible, GwtIncompatible, Charset, HashCode

    com.google.common.annotations.Beta /** * 表明一个公用API的未来版本是受不兼容变更或删除限制的 * 拥有这个注释标志的API不受任何兼容性保证 * */ @R ...

  3. cocos2d js ScrollView的使用方法

    游戏中非常多须要用到ScrollView的情况,也就是须要滚动一片区域. 这里有两种实现方法,一种是使用cocos studio的方式,另外一种是手写代码.先看第一种 第一种记得在设置滚动区域时选取裁 ...

  4. C/C++ 获取目录下的文件列表信息

    在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent {     long d_ino;                 /* inode number 索引 ...

  5. hdu3117之矩阵快速幂

    Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. Chain of Responsibility 责任链模式 MD

    责任链模式 简介 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链,请求在这个链上[传递],直到链上的某一个对象决定处理此请求.发出这个请求的客户 ...

  7. docker 基本原理及快速入门

    作者地址:青牛 什么是docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来 ...

  8. Bootstrap学习js插件篇之标签页

    简单的标签页 代码: <h1 class="page-header">4.3标签页</h1> <ul class="nav nav-tabs ...

  9. [置顶] Android异步加载数据库和多线程编程

    Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发.尚未有统一中文名称,中国大陆地区较多人使用“安卓” ...

  10. X86-64寄存器和栈帧

    简介 通用寄存器可用于传送和暂存数据,也可参与算术逻辑运算,并保存运算结果.除此之外,它们还各自具有一些特殊功能.通用寄存器的长度取决于机器字长,汇编语言程序员必须熟悉每个寄存器的一般用途和特殊用途, ...