多服務Redis工具類

多服務Redis工具類


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${redis.userName:}")
    private String cacheName;   //用來區分各個服務

    /**
     * 批量刪除對應的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(cacheName + key);
        }
    }

    /**
     * 批量刪除key,這裏暫時先不改
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 刪除對應的value
     *
     * @param key
     */
    @SuppressWarnings("unchecked")
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(cacheName + key);
        }
    }

    /**
     * 判斷緩存中是否有對應的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(cacheName + key);
    }

    /**
     * 讀取緩存
     *
     * @param key
     * @return
     */
    public <T> T get(final String key) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        return (T) operations.get(cacheName + key);
    }

    /**
     * 寫入緩存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(cacheName + key, value);
        return true;
    }

    /**
     * 寫入緩存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        redisTemplate.opsForValue().set(key, value);
        if(expireTime>0){
            redisTemplate.expire(cacheName + key, expireTime, TimeUnit.SECONDS);
        }
        return true;
    }

    public boolean hashSet(final String key, Map<String, Object> value, Long expireTime) {
        HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
        operations.putAll(cacheName + key, value);
        if(expireTime>0){
            redisTemplate.expire(cacheName + key, expireTime, TimeUnit.SECONDS);
        }
        return true;
    }

    public boolean hashSet(final String key, String field, Object value, Long expireTime) {
        HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
        operations.put(cacheName + key, field, value);
        if(expireTime>0){
            redisTemplate.expire(cacheName + key, expireTime, TimeUnit.SECONDS);
        }
        return true;
    }

    public Long hashDel(final String key, String... fields) {
        HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
        return operations.delete(cacheName + key, fields);
    }

    public <T> T hashGet(final String key, String field) {
        HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
        return (T) operations.get(key, field);
    }

    public <V> List<V> hashGet(final String key, Collection<String> fields) {
        HashOperations<String, String, V> operations = redisTemplate.opsForHash();
        return operations.multiGet(key, fields);
    }

    public Long listRightPushAll(final String key, Collection<Object> values, Long expireTime) {
        ListOperations<String, Object> operations = redisTemplate.opsForList();
        if(expireTime>0){
            redisTemplate.expire(cacheName + key, expireTime, TimeUnit.SECONDS);
        }
        return operations.rightPushAll(key, values);
    }

    public Long listLeftPushAll(final String key, Collection<Object> values, Long expireTime) {
        ListOperations<String, Object> operations = redisTemplate.opsForList();
        if(expireTime>0){
            redisTemplate.expire(cacheName + key, expireTime, TimeUnit.SECONDS);
        }
        return operations.leftPushAll(key, values);
    }

    // list將用於一些隊列的場景,這裏可以不用進行一些操作,這裏不用緩存超時時間的設置
    public <V> V listRightPop(final String key) {
        ListOperations<String, V> operations = redisTemplate.opsForList();
        return operations.rightPop(key);
    }

    public <V> V listLeftPop(final String key) {
        ListOperations<String, V> operations = redisTemplate.opsForList();
        return operations.leftPop(key);
    }

    /**
     *
     * @param key
     * @param timeout  獲取數據的根據超時時間
     * @param <V>
     * @return
     */
    public <V> V listRightPop(final String key, Long timeout) {
        ListOperations<String, V> operations = redisTemplate.opsForList();
        return operations.rightPop(key, timeout, TimeUnit.MILLISECONDS);
    }
    /**
     *
     * @param key
     * @param timeout  獲取數據的根據超時時間
     * @param <V>
     * @return
     */
    public <V> V listLeftPop(final String key, Long timeout) {
        ListOperations<String, V> operations = redisTemplate.opsForList();
        return operations.leftPop(key, timeout, TimeUnit.MILLISECONDS);
    }



}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章