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. Microsoft Internet Explorer内存破坏漏洞(CVE-2013-5052)

    漏洞版本: Microsoft Internet Explorer 6-11 漏洞描述: BUGTRAQ ID: 64126 CVE(CAN) ID: CVE-2013-5052 Internet E ...

  2. jquery 创建 SVG DOM 的处理方法

    使用的是 createElement 方法 这个是无法生成SVG DOM的 可以使用下方的方法生成 var svgns = "http://www.w3.org/2000/svg" ...

  3. jQuery append xmlNode 修改 xml 内容

    jQuery append xmlNode 修改 xml 内容 http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/04/29/jqu ...

  4. Installing EF Power Tools into VS2015

    TLDR: If you don’t want to do the tasks (even though they are so easy) you can download the updated ...

  5. 【CSS3】Advanced9:Transformation

    1.transform:rotate(-10deg) skew(20deg,10deg) scaling(2/1,2) translate/移动(100px,200px) 2.transform:ma ...

  6. javascript设计模式1

    普通写法 function startAnimation(){ ... } function stopAnimation(){ ... } 对象类 /*Anim class*/ var Anim=fu ...

  7. 16道嵌入式C语言面试题

    预处理器(Preprocessor) 1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)         #define SECONDS_PER_YEAR (60 ...

  8. fzu2109--Mountain Number(数位dp)

     Problem Description One integer number x is called "Mountain Number" if: (1) x>0 and x ...

  9. hdoj 1253 胜利大逃亡

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. java 泛型中 T、E ... 和 问号(通配符)的区别

    一.泛型中T.E ...  是泛型类.泛型方法定义时候用的. 1.泛型类定义在类后面 紧跟类名后面 public class TestClassDefine<T>{} 2.泛型方法定义在方 ...