1. 程式人生 > >【redis快取】Redis封裝類

【redis快取】Redis封裝類

Redis封裝Hash&String

對redisTemplate簡單封裝,方便使用

  1. Hash型別的
public class HashRedisService<K, HK, HV> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 設定有效期
     *
     * @param key
     * @param timeout
     * @param unit
     */
    public void expire(String key, long timeout, TimeUnit unit) {
        if (unit == null) {
            unit = TimeUnit.SECONDS;
        }
        redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 獲取所有匹配成功的key set集合
     *
     * @param pattern
     * @return
     */
    public Set<K> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    /**
     * 刪除key
     *
     * @param key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 向名稱為key的hash中新增元素hashKey
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hSet(K key, HK hashKey, HV value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 向名稱為key的hash中新增元素hashKey, 若hashKey對應value有值則不覆蓋
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hSetIfAbsent(K key, HK hashKey, HV value) {
        redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
    }

    /**
     * 返回名稱為key的hash中hashKey對應的value
     *
     * @param key
     * @param hashKey
     * @return
     */
    public HV hGet(K key, HK hashKey) {
        return (HV) redisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 向名稱為key的hash中新增元素hashMap
     *
     * @param key
     * @param hashMap
     */
    public void hMSet(K key, Map<? extends HK, ? extends HV> hashMap) {
        redisTemplate.opsForHash().putAll(key, hashMap);
    }

    /**
     * 返回名稱為key的hash中hashKeys i對應的value 鍵值對
     *
     * @param key
     * @param hashKeys
     * @return
     */
    public Map<HK, HV> hMGet(K key, Collection<HK> hashKeys) {
        Map<HK, HV> hashMap = new HashMap<HK, HV>();
        List<HV> valueList = redisTemplate.opsForHash().multiGet(key, hashKeys);

        int i = 0;
        for (HK hashKey : hashKeys) {
            hashMap.put(hashKey, valueList.get(i));
            i++;
        }
        return hashMap;
    }

    /**
     * 返回名稱為key的hash中hashKeys i對應的value 集合
     *
     * @param key
     * @param hashKeys
     * @return
     */
    public List<HV> hMGet2List(final K key, final Collection<HK> hashKeys) {
        return redisTemplate.opsForHash().multiGet(key, hashKeys);
    }

    /**
     * 返回名稱為key的hash中所有鍵對應的value 鍵值對
     *
     * @param key
     * @return
     */
    public Map<HK, HV> hVals(K key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 返回名稱為key的hash中所有鍵對應的value 集合
     *
     * @param key
     * @return
     */
    public List<HV> hVals2List(K key) {
        return redisTemplate.opsForHash().values(key);
    }

    /**
     * 刪除名稱為key的hash中鍵為hashKey的域
     *
     * @param key
     * @param hashKeys
     */
    public void hDel(K key, HK... hashKeys) {
        redisTemplate.opsForHash().delete(key, hashKeys);
    }

    /**
     * 返回名稱為key的hash中元素個數
     *
     * @param key
     * @return
     */
    public Long hLen(K key) {
        return redisTemplate.opsForHash().size(key);
    }

    /**
     * 名稱為key的hash中是否存在鍵為hashKey的域
     *
     * @param key
     * @param hashKey
     * @return
     */
    public boolean hasKey(K key, HK hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    /**
     * 名稱為key的hash是否存在
     *
     * @param key
     * @return
     */
    public boolean hasKey(K key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 返回名稱為key的hash中所有鍵
     *
     * @param key
     * @return
     */
    public Set<HK> hKeys(K key) {
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     * 將名稱為key的hash中hashKey的value增加Long
     *
     * @param key
     * @param hashKey
     * @param delta 變數增量
     * @return
     */
    public Long hIncrBy(K key, HK hashKey, Long delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    /**
     * 將名稱為key的hash中hashKey的value增加Double
     *
     * @param key
     * @param hashKey
     * @param stepSize
     * @return
     */
    public Double hIncrBy(K key, HK hashKey, Double stepSize) {
        return redisTemplate.opsForHash().increment(key, hashKey, stepSize);
    }


}
  1. String型別的資料
public class StringRedisService {

    @Autowired
    private RedisTemplate<String, String> stringRedisTemplate;

    /**
     * 設定有效期
     *
     * @param key
     * @param timeout
     * @param unit
     */
    public void expire(String key, long timeout, TimeUnit unit) {
        if (unit == null) {
            unit = TimeUnit.SECONDS;
        }
        stringRedisTemplate.expire(key, timeout, unit);
    }

    /**
     * 獲取所有匹配成功的key set集合
     *
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern) {
        return stringRedisTemplate.keys(pattern);
    }

    /**
     * 刪除key
     *
     * @param key
     */
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 插入字串key value
     *
     * @param key
     * @param val
     */
    public void set(String key, String val) {
        stringRedisTemplate.opsForValue().set(key, val);
    }

    /**
     * 插入key, 並設定有效期(單位s秒)
     *
     * @param key
     * @param val
     * @param timeout
     */
    public void set(String key, String val, long timeout) {
        stringRedisTemplate.opsForValue().set(key, val, timeout, TimeUnit.SECONDS);
    }

    /**
     * 插入json格式的物件
     *
     * @param key
     * @param val
     */
    public void setJsonObj(String key, Object val) throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val));
    }

    /**
     * 插入json格式的物件, 並設定有效期(單位s秒)
     *
     * @param key
     * @param val
     * @param timeout
     */
    public void setJsonObj(String key, Object val, long timeout) throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val), timeout, TimeUnit.SECONDS);
    }

    /**
     * 獲取字串, by key
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 獲取json字串, 並轉換為引數指定的物件
     *
     * @param key
     * @param clazz
     * @param <T>
     */
    public <T> T getJson(String key, Class<T> clazz) {
        String val = stringRedisTemplate.opsForValue().get(key);
        return JsonUtils.fromJson(val, clazz);
    }

    /**
     * 獲取key 並且擷取字串
     * @param key
     * @param start
     * @param end
     * @return
     */
    public String substr(String key, Long start, Long end){
        return stringRedisTemplate.opsForValue().get(key, start,end);
    }

    /**
     * 獲取老的值並設定新的值
     * @param key
     * @param val
     * @return
     */
    public String getAndSet(String key, String val){
        return stringRedisTemplate.opsForValue().getAndSet(key, val);
    }

    public List<String> multiGet(Collection<String> keys){
        return stringRedisTemplate.opsForValue().multiGet(keys);
    }

    public void setIfAbsent(String key, String val){
        stringRedisTemplate.opsForValue().setIfAbsent(key,val);
    }

    public Long strlen(String key){
        return stringRedisTemplate.opsForValue().size(key);
    }

    public void append(String key, String val){
        stringRedisTemplate.opsForValue().append(key,val);
    }
}