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

时间:2021-01-26 00:10:38

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/