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. 结构体 fseg_inode_t;

    typedef byte fseg_inode_t;

  2. Bat 中特殊符号

    批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @\\隐藏命令的回显. ~\\在for中表示使用增强的变量扩展:在set中表示使用扩展环境变量指定位置的字符串:在set ...

  3. 使用Action、Func和Lambda表达式

    使用Action.Func和Lambda表达式 在.NET在,我们经常使用委托,委托的作用不必多说,在.NET 2.0之前,我们在使用委托之前,得自定义一个委托类型,再使用这个自定义的委托类型定义一个 ...

  4. App.config提示错误“配置系统未能初始化”

    解决: "如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素." 所以它前面如果有 ...

  5. 使用C#调用Python脚本,带参数列表 z

    static void Main(string[] args) { string[] strArr;//参数列表 string sArguments = @"Pythons.py" ...

  6. jQuery掷骰子

    网上找的jQuery掷骰子效果,测试兼容IE7及以上浏览器,IE6没有测试 js代码如下: $(function(){ var dice = $("#dice"); dice.cl ...

  7. Linux Kernel Schduler History And Centos7.2's Kernel Resource Analysis

    本文分为概述.历史.el7.2代码架构图解三部分. 解决的问题: a.Kernel调度发展过程: b.以架构图的方式,详解el7.2具体调度实现.内核线程模型.调度时间片计算,以及探究整个Kernel ...

  8. 2015年9月28日JQuery提前预习预热笔记

    visual studio下载2010 2010与2008不一样,2008需要添加补丁,采用调用对象.2010可以直接用. JQuery=$ 是函数是方法是对象 念J快儿,念doler 开发人员工具( ...

  9. HDU 1754 I Hate It 线段树 单点更新 区间最大值

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  10. test-from

    title header1 hahhahjl header2 adfkljasd $$a^2$$ point 1 point 2 | table1 | table2 | | ---- | ---- | ...