Redis操作Hash工具类封装,Redis工具类封装
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工具类封装的更多相关文章
- Redis操作hash
来自:http://www.cnblogs.com/alex3714/articles/6217453.html Hash操作 hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强 ...
- c# redis 操作类库推荐:StackExchange.Redis.Extensions
StackExchange是一个优秀的c# redis客户端,但是存在操作略为繁琐的弊端,为了简化操作,使用 StackExchange.Redis.Extensions成为了一个非常值得推荐的选择. ...
- redis 操作 hash 的测试
1>hset setname field value hset stuSet name zhangsan:1 2>hget setname field hget stuset ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- python笔记7:mysql、redis操作
模块安装: 数据操作用到的模块pymysql,需要通过pip install pymysql进行安装. redis操作用的模块是redis,需要通过pip install redis进行安装. 检验是 ...
- Atitit.redis操作总结
Atitit.redis操作总结 1.1. 获取redis所有kv1 1.2. dbsize:返回当前数据库中key的数目 1 1.3. 一起吧所有key列出来1 1.4. Java连接redis ...
- Redis(十二):redis请求转发的实现
请求转发一般的原因为: 1. 该请求自身无法处理,需要转发给对应的服务器处理: 2. 为实现负载均衡,使用路由服务,选择目标实例进行转发: 在集群模式下,请求可以打到任何一台redis服务器上.然而并 ...
随机推荐
- 函数 flst_get_first
/********************************************************************//** Gets list first node addre ...
- bzoj1143 2718
最小可相交路径覆盖 先预处理可到达的点然后转化为最小不相交路径覆盖 type node=record point,next:longint; end; ..] of node; ...
- 六月计划#1A(6.1-6.8)
5/35 数学 BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂) BZOJ_1005_ [HNOI2008]_明明的烦恼_(组合数学+purfe ...
- BZOJ_3282_Tree_(LCT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3282 给出n个点以及权值,四种操作: 0.求x,y路径上的点权值的异或和. 1.连接x,y. ...
- $apply() $digest()
理解Angular中的$apply()以及$digest() <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- 【转】 Java虚拟机内存的堆区(heap),栈区(stack)和静态区(static/method)
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...
- Windows下的.NET+ Memcached安装
转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...
- STL find() ,还是挺重要的
template<class InputIterator, class T> InputIterator find (InputIterator first, InputIterator ...
- linux 系统获取网络ip, mask, gateway, dns信息小程序
net_util.c #define WIRED_DEV "eth0" #define WIRELESS_DEV ...
- lihgtoj 1006
记忆化搜索下即可. #include<cstdio> #include<string> #include<cstring> #include<iostream ...