1. 程式人生 > >jedis操作redis中的hash(封裝)

jedis操作redis中的hash(封裝)

一、由來

之前的redis pool的文章,講解了如何封裝一個redis pool以及操作redis中的字串,部落格地址如下:

還有一篇文章封裝了一些 操作redis中的list 。。。。

還有一篇文章封裝了一些 操作redis中的set 。。。。

關於 hash相關操作命令可以參考runoob,連線地址如下:

本文主要是封裝一些 操作 redis中 hash的相關方法。。。。。。

二、程式碼

package me.ele.redis;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

/**
 * 操作redis中的hash
 *
 * @author LZJ
 * @create 2018-08-29 13:20
 **/
public class HashRedisOperator {


    /**
     * 新增一條記錄 如果map_key存在 則更新value
     * hset 如果雜湊表不存在,一個新的雜湊表被建立並進行 HSET 操作。
     * 如果欄位已經存在於雜湊表中,舊值將被覆蓋
     *
     * @param pool
     * @param key
     * @param field
     * @param value
     * @return
     */
    public static long set(MyRedisPool pool, String key, String field, String value) {
        Jedis jedis = pool.borrowJedis();
        long returnStatus = jedis.hset(key, field, value);
        pool.returnJedis(jedis);
        return returnStatus;
    }

    /**
     * 批量新增記錄
     * hmset 同時將多個 field-value (域-值)對設定到雜湊表 key 中。
     * 此命令會覆蓋雜湊表中已存在的域。
     * 如果 key 不存在,一個空雜湊表被建立並執行 HMSET 操作。
     *
     * @param pool
     * @param key
     * @param map
     * @return
     */
    public static String setAll(MyRedisPool pool, String key, Map<String, String> map) {
        Jedis jedis = pool.borrowJedis();
        String result = jedis.hmset(key, map);
        pool.returnJedis(jedis);
        return result;
    }

    /**
     * 刪除hash中 field對應的值
     * hdel 刪除雜湊表 key 中的一個或多個指定域,不存在的域將被忽略
     *
     * @param pool
     * @param key
     * @param field
     * @return
     */
    public static long delete(MyRedisPool pool, String key, String... field) {
        Jedis jedis = pool.borrowJedis();
        long returnStatus = jedis.hdel(key, field);
        pool.returnJedis(jedis);
        return returnStatus;
    }

    /**
     * 獲取hash中 指定的field的值
     * hmget 返回雜湊表 key 中,一個或多個給定域的值。
     * 如果給定的域不存在於雜湊表,那麼返回一個 nil 值。
     * 不存在的 key 被當作一個空雜湊表來處理,所以對一個不存在的 key 進行 HMGET 操作將返回一個只帶有 nil 值的表。
     *
     * @param pool
     * @param key
     * @param field
     * @return
     */
    public static List<String> get(MyRedisPool redisPool, String key, String... field) {
        Jedis jedis = redisPool.borrowJedis();
        List<String> result = jedis.hmget(key, field);
        redisPool.returnJedis(jedis);
        return result;
    }

    /**
     * 獲取hash中 所有的field value
     *
     * @param redisPool
     * @param key
     * @return 在返回值裡,緊跟每個欄位名(field name)之後是欄位的值(value),所以返回值的長度是雜湊表大小的兩倍。
     */
    public static Map<String, String> getAll(MyRedisPool redisPool, String key) {
        Jedis jedis = redisPool.borrowJedis();
        Map<String, String> result = jedis.hgetAll(key);
        redisPool.returnJedis(jedis);
        return result;
    }

    /**
     * 判斷hash中 指定的field是否存在
     *
     * @param redisPool
     * @param key
     * @param field
     * @return 如果雜湊不包含欄位或key不存在 返回0,如果雜湊包含欄位 返回1
     */
    public static boolean ifExist(MyRedisPool redisPool, String key, String field) {
        Jedis jedis = redisPool.borrowJedis();
        boolean result = jedis.hexists(key, field);
        redisPool.returnJedis(jedis);
        return result;
    }

    /**
     * 獲取hash 的size
     * hlen 獲取雜湊表中欄位的數量
     *
     * @param redisPool
     * @param key
     * @return
     */
    public static long size(MyRedisPool redisPool, String key) {
        Jedis jedis = redisPool.borrowJedis();
        long size = jedis.hlen(key);
        redisPool.returnJedis(jedis);
        return size;
    }


}