Redis操作Hash工具类封装,Redis工具类封装

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2016年9月26日 16:28:23 星期一

http://fanshuyao.iteye.com/

Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

Redis操作Set工具类封装:http://fanshuyao.iteye.com/blog/2327228

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。

Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿)。

注:下面的代码只是方法封装,缺少一部分,因为是【Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221】的延续,把下面的代码增加到之前代码后面就可以了。

/**************************** redis Hash start***************************/
/***Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。***/ /**
* 设置Hash的属性
* @param key
* @param field
* @param value
* @return
*/
public static boolean hset(String key, String field, String value){
if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
return false;
}
Jedis jedis = jedisPool.getResource();
//If the field already exists, and the HSET just produced an update of the value, 0 is returned,
//otherwise if a new field is created 1 is returned.
Long statusCode = jedis.hset(key, field, value);
jedis.close();
if(statusCode > -1){
return true;
}
return false;
} /**
* 批量设置Hash的属性
* @param key
* @param fields
* @param values
* @return
*/
public static boolean hmset(String key, String[] fields, String[] values){
if(StrUtils.isBlank(key) || StrUtils.isEmptyArray(fields) || StrUtils.isEmptyArray(values)){
return false;
}
Jedis jedis = jedisPool.getResource();
Map<String, String> hash = new HashMap<String, String>();
for (int i=0; i<fields.length; i++) {
hash.put(fields[i], values[i]);
}
String statusCode = jedis.hmset(key, hash);
jedis.close();
if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
return true;
}
return false;
} /**
* 批量设置Hash的属性
* @param key
* @param map Map<String, String>
* @return
*/
public static boolean hmset(String key, Map<String, String> map){
if(StrUtils.isBlank(key) || StrUtils.isEmptyMap(map)){
return false;
}
Jedis jedis = jedisPool.getResource();
String statusCode = jedis.hmset(key, map);
jedis.close();
if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
return true;
}
return false;
} /**
* 仅当field不存在时设置值,成功返回true
* @param key
* @param field
* @param value
* @return
*/
public static boolean hsetNX(String key, String field, String value){
if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
return false;
}
Jedis jedis = jedisPool.getResource();
//If the field already exists, 0 is returned,
//otherwise if a new field is created 1 is returned.
Long statusCode = jedis.hsetnx(key, field, value);
jedis.close();
if(SUCCESS_STATUS_LONG == statusCode){
return true;
}
return false;
} /**
* 获取属性的值
* @param key
* @param field
* @return
*/
public static String hget(String key, String field){
if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
return null;
}
Jedis jedis = jedisPool.getResource();
String value = jedis.hget(key, field);
jedis.close();
return value;
} /**
* 批量获取属性的值
* @param key
* @param fields String...
* @return
*/
public static List<String> hmget(String key, String... fields){
if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){
return null;
}
Jedis jedis = jedisPool.getResource();
List<String> values = jedis.hmget(key, fields);
jedis.close();
return values;
} /**
* 获取在哈希表中指定 key 的所有字段和值
* @param key
* @return Map<String, String>
*/
public static Map<String, String> hgetAll(String key){
if(StrUtils.isBlank(key)){
return null;
}
Jedis jedis = jedisPool.getResource();
Map<String, String> map = jedis.hgetAll(key);
jedis.close();
return map;
} /**
* 删除hash的属性
* @param key
* @param fields
* @return
*/
public static boolean hdel(String key, String... fields){
if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){
return false;
}
Jedis jedis = jedisPool.getResource();
jedis.hdel(key, fields);
jedis.close();
//System.out.println("statusCode="+statusCode);
return true;
} /**
* 查看哈希表 key 中,指定的字段是否存在。
* @param key
* @param field
* @return
*/
public static boolean hexists(String key, String field){
if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
return false;
}
Jedis jedis = jedisPool.getResource();
boolean result = jedis.hexists(key, field);
jedis.close();
return result;
} /**
* 为哈希表 key 中的指定字段的整数值加上增量 increment 。
* @param key
* @param field
* @param increment 正负数、0、正整数
* @return
*/
public static long hincrBy(String key, String field, long increment){
Jedis jedis = jedisPool.getResource();
long result = jedis.hincrBy(key, field, increment);
jedis.close();
return result;
} /**
* 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。(注:如果field不存在时,会设置新的值)
* @param key
* @param field
* @param increment,可以为负数、正数、0
* @return
*/
public static Double hincrByFloat(String key, String field, double increment){
Jedis jedis = jedisPool.getResource();
Double result = jedis.hincrByFloat(key, field, increment);
jedis.close();
return result;
} /**
* 获取所有哈希表中的字段
* @param key
* @return Set<String>
*/
public static Set<String> hkeys(String key){
Jedis jedis = jedisPool.getResource();
Set<String> result = jedis.hkeys(key);
jedis.close();
return result;
} /**
* 获取哈希表中所有值
* @param key
* @return List<String>
*/
public static List<String> hvals(String key){
Jedis jedis = jedisPool.getResource();
List<String> result = jedis.hvals(key);
jedis.close();
return result;
} /**
* 获取哈希表中字段的数量,当key不存在时,返回0
* @param key
* @return
*/
public static Long hlen(String key){
Jedis jedis = jedisPool.getResource();
Long result = jedis.hlen(key);
jedis.close();
return result;
} /**
* 迭代哈希表中的键值对。
* @param key
* @param cursor
* @return ScanResult<Entry<String, String>>
*/
public static ScanResult<Entry<String, String>> hscan(String key, String cursor){
Jedis jedis = jedisPool.getResource();
ScanResult<Entry<String, String>> scanResult = jedis.hscan(key, cursor);
jedis.close();
//System.out.println(scanResult.getResult());
return scanResult;
} /**************************** redis Hash end***************************/

Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2016年9月26日 16:28:23 星期一

http://fanshuyao.iteye.com/

Redis操作Hash工具类封装,Redis工具类封装的更多相关文章

  1. Redis操作hash

    来自:http://www.cnblogs.com/alex3714/articles/6217453.html Hash操作 hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强 ...

  2. c# redis 操作类库推荐:StackExchange.Redis.Extensions

    StackExchange是一个优秀的c# redis客户端,但是存在操作略为繁琐的弊端,为了简化操作,使用 StackExchange.Redis.Extensions成为了一个非常值得推荐的选择. ...

  3. redis 操作 hash 的测试

    1>hset setname field value hset stuSet name zhangsan:1        2>hget setname field hget stuset ...

  4. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  5. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  6. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  7. python笔记7:mysql、redis操作

    模块安装: 数据操作用到的模块pymysql,需要通过pip install pymysql进行安装. redis操作用的模块是redis,需要通过pip install redis进行安装. 检验是 ...

  8. Atitit.redis操作总结

    Atitit.redis操作总结 1.1. 获取redis所有kv1 1.2. dbsize:返回当前数据库中key的数目 1 1.3. 一起吧所有key列出来1 1.4. Java连接redis   ...

  9. Redis(十二):redis请求转发的实现

    请求转发一般的原因为: 1. 该请求自身无法处理,需要转发给对应的服务器处理: 2. 为实现负载均衡,使用路由服务,选择目标实例进行转发: 在集群模式下,请求可以打到任何一台redis服务器上.然而并 ...

随机推荐

  1. eclipse 使用指南

    eclipse使用指南 eclipse下载地址: 1.eclipse快捷键 2.将eclipse新建项目的默认编码GBK改为UTF-8 3.Java 编程下 Eclipse 如何设置单行代码显示的最大 ...

  2. [原创][下载]Senparc.Weixin.MP-微信公众平台SDK(C#) - 已支持微信6.x API

    因为正在计划做一个微信机器人,需要用ASP.NET,找了一下只有PHP的SDK,没有C#的,于是动手写了一个,已经全面支持微信6.x所有接口,包括多客服.卡券.微信支付等. 微信公众平台地址:http ...

  3. oracle删掉重复数据的语法

    --查询重复数据-- ) --删掉重复数据-- ) );

  4. Visual Studio 2012 Professional 密钥

    Visual Studio 2012 Professional 密钥 4D974-9QX42-9Y43G-YJ7JG-JDYBP

  5. 【原】Spark中Job的提交源码解读

    版权声明:本文为原创文章,未经允许不得转载. Spark程序程序job的运行是通过actions算子触发的,每一个action算子其实是一个runJob方法的运行,详见文章 SparkContex源码 ...

  6. 【原】 Spark中Worker源码分析(二)

    继续前一篇的内容.前一篇内容为: Spark中Worker源码分析(一)http://www.cnblogs.com/yourarebest/p/5300202.html 4.receive方法, r ...

  7. Tsinsen A1517. 动态树 树链剖分,线段树,子树操作

    题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s   内存限制:1.0GB    总提交次数:227   AC次数:67   平均分:49. ...

  8. vim recording

    大家是否有这种经验,“不知道为什么按出recording状态,按ESC貌似无法直接退掉”的情况,个人已经有过好几次了.与其出来烦人还不如了解它,昨天我就花了点时间学习recording.怎么说,还是有 ...

  9. shell脚本应用(3)--语法结构

    判断语句 条件判断 test expression [ expression ] 条件表达式中常用的判断 数值-eq -ne -gt -lt -ge -le[equal not greater tha ...

  10. 【转】终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件

    感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...