All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wzc789376152.springboot.config.redis.service.RedisBaseService Maven / Gradle / Ivy

The newest version!
package com.github.wzc789376152.springboot.config.redis.service;

import org.springframework.data.redis.core.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * spring redis 工具类
 *
 * @author jackw
 **/
public class RedisBaseService {

    private RedisTemplate redisTemplate;

    public RedisBaseService(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public Long  increment(final String key, long delta) {
        return redisTemplate.opsForValue().increment(key,delta);
    }


    public Long  decrement(final String key, long delta) {
        return redisTemplate.opsForValue().decrement(key,delta);
    }

    public  void setCacheObject(final String key, final T value) {
        redisTemplate.opsForValue().set(key, value);
    }


    public  void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }


    public  Boolean setNx(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
    }


    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }


    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return Boolean.TRUE.equals(redisTemplate.expire(key, timeout, unit));
    }


    public long getExpire(final String key) {
        Long expire = redisTemplate.getExpire(key);
        return expire == null ? 0 : expire;
    }


    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }


    public  T getCacheObject(final String key) {
        ValueOperations operation = redisTemplate.opsForValue();
        return operation.get(key);
    }


    public boolean deleteObject(final String key) {
        return Boolean.TRUE.equals(redisTemplate.delete(key));
    }


    public long deleteObject(final Collection collection) {
        Long result = redisTemplate.delete(collection);
        return result == null ? 0 : result;
    }


    public  long setCacheList(final String key, final List dataList) {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }


    public  List getCacheList(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }


    public  long cachePush(final String key, final T value) {
        Long count = redisTemplate.opsForList().rightPush(key, value);
        return count == null ? 0 : count;
    }


    public   T cacheProp(final String key,long timeout, TimeUnit unit) {
        ListOperations operation = redisTemplate.opsForList();
        return operation.leftPop(key,timeout,unit);
    }

    public  BoundSetOperations setCacheSet(final String key, final Set dataSet) {
        BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
        Iterator it = dataSet.iterator();
        while (it.hasNext()) {
            setOperation.add(it.next());
        }
        return setOperation;
    }


    public  Set getCacheSet(final String key) {
        return redisTemplate.opsForSet().members(key);
    }


    public  void setCacheMap(final String key, final Map dataMap) {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }


    public  Map getCacheMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public  void setCacheMapValue(final String key, final String hKey, final T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    public  T getCacheMapValue(final String key, final String hKey) {
        HashOperations opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }

    public  List getMultiCacheMapValue(final String key, final Collection hKeys) {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }


    public Collection keys(final String pattern) {
        return redisTemplate.keys(pattern);
    }

    public Long increment(String key, String hashKey, Long delate) {
        return redisTemplate.opsForHash().increment(key, hashKey, delate);
    }
}