拷贝

 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. <mvc:annotation-driven />做了什么

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  2. linked-list-random-node

    https://leetcode.com/problems/linked-list-random-node/ // Using Reservoir sampling algorithm // http ...

  3. Python并发编程-redis-3.0.5 源码安装

    1.简介 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像,但它支持存储的 ...

  4. 巧妙利用jQuery和PHP打造类似360安全卫士防火墙功能开关(类似iphone界面)效果

    安全卫士防火墙开启关闭的开关,可以将此功能应用在产品功能的开启和关闭功能上. 准备工作为了更好的演示本例,我们需要一个数据表,记录需要的功能说明及开启状态,表结构如下: CREATE TABLE `p ...

  5. 再谈javascript图片预加载技术

    图片预加载技术的典型应用: 如lightbox方式展现照片,无疑需要提前获得大图的尺寸,这样才能居中定位,由于javascript无法获取img文件头数据,必须等待其加载完毕后才能获取真实的大小然后展 ...

  6. IOS Xib使用——Xib表示局部界面

    Xib文件是一个轻量级的用来描述局部界面的文件,在之前的文章讲解了为控制器添加Xib文件,本节主要讲解一下通过xib文件表示局部界面. <一> 创建Xib文件 Xib文件创建的时候是选择U ...

  7. Android 如何去掉手机中横竖屏切换时的转屏动画?

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  8. 申请红帽企业版Linux开发者订阅

    导读 注册成为开发者计划的成员现在可以得到一套免费的 Red Hat Enterprise Linux 许可证,RHEL 开发套件将为程序员提供一个构建企业应用的稳定发展平台.红帽开发订阅成员还可以免 ...

  9. Typescript declaration: Merge a class and an interface

    参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interfa ...

  10. 王立平-- android:layout_weight

    效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I0 ...