Redis工具類封裝RedisUtils

SpringBoot項目集成Redis至關簡單,只須要pom中加入對應依賴java

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

yml中,配置好spring.redis.host,spring.redis.port便可。redis

具體如何封裝,直接上代碼。spring

/**
 * Redis工具類,使用以前請確保RedisTemplate成功注入
 *
 * @author ye17186
 * @version 2019/2/22 10:48
 */
public class RedisUtils {

    private RedisUtils() {
    }

    @SuppressWarnings("unchecked")
    private static RedisTemplate<String, Object> redisTemplate = SpringUtils
        .getBean("redisTemplate", RedisTemplate.class);

    /**
     * 設置有效時間
     *
     * @param key Redis鍵
     * @param timeout 超時時間
     * @return true=設置成功;false=設置失敗
     */
    public static boolean expire(final String key, final long timeout) {

        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 設置有效時間
     *
     * @param key Redis鍵
     * @param timeout 超時時間
     * @param unit 時間單位
     * @return true=設置成功;false=設置失敗
     */
    public static boolean expire(final String key, final long timeout, final TimeUnit unit) {

        Boolean ret = redisTemplate.expire(key, timeout, unit);
        return ret != null && ret;
    }

    /**
     * 刪除單個key
     *
     * @param key 鍵
     * @return true=刪除成功;false=刪除失敗
     */
    public static boolean del(final String key) {

        Boolean ret = redisTemplate.delete(key);
        return ret != null && ret;
    }

    /**
     * 刪除多個key
     *
     * @param keys 鍵集合
     * @return 成功刪除的個數
     */
    public static long del(final Collection<String> keys) {

        Long ret = redisTemplate.delete(keys);
        return ret == null ? 0 : ret;
    }

    /**
     * 存入普通對象
     *
     * @param key Redis鍵
     * @param value 值
     */
    public static void set(final String key, final Object value) {

        redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES);
    }

    // 存儲普通對象操做

    /**
     * 存入普通對象
     *
     * @param key 鍵
     * @param value 值
     * @param timeout 有效期,單位秒
     */
    public static void set(final String key, final Object value, final long timeout) {

        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 獲取普通對象
     *
     * @param key 鍵
     * @return 對象
     */
    public static Object get(final String key) {

        return redisTemplate.opsForValue().get(key);
    }

    // 存儲Hash操做

    /**
     * 往Hash中存入數據
     *
     * @param key Redis鍵
     * @param hKey Hash鍵
     * @param value 值
     */
    public static void hPut(final String key, final String hKey, final Object value) {

        redisTemplate.opsForHash().put(key, hKey, value);
    }

    /**
     * 往Hash中存入多個數據
     *
     * @param key Redis鍵
     * @param values Hash鍵值對
     */
    public static void hPutAll(final String key, final Map<String, Object> values) {

        redisTemplate.opsForHash().putAll(key, values);
    }

    /**
     * 獲取Hash中的數據
     *
     * @param key Redis鍵
     * @param hKey Hash鍵
     * @return Hash中的對象
     */
    public static Object hGet(final String key, final String hKey) {

        return redisTemplate.opsForHash().get(key, hKey);
    }

    /**
     * 獲取多個Hash中的數據
     *
     * @param key Redis鍵
     * @param hKeys Hash鍵集合
     * @return Hash對象集合
     */
    public static List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {

        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    // 存儲Set相關操做

    /**
     * 往Set中存入數據
     *
     * @param key Redis鍵
     * @param values 值
     * @return 存入的個數
     */
    public static long sSet(final String key, final Object... values) {
        Long count = redisTemplate.opsForSet().add(key, values);
        return count == null ? 0 : count;
    }

    /**
     * 刪除Set中的數據
     *
     * @param key Redis鍵
     * @param values 值
     * @return 移除的個數
     */
    public static long sDel(final String key, final Object... values) {
        Long count = redisTemplate.opsForSet().remove(key, values);
        return count == null ? 0 : count;
    }

    // 存儲List相關操做

    /**
     * 往List中存入數據
     *
     * @param key Redis鍵
     * @param value 數據
     * @return 存入的個數
     */
    public static long lPush(final String key, final Object value) {
        Long count = redisTemplate.opsForList().rightPush(key, value);
        return count == null ? 0 : count;
    }

    /**
     * 往List中存入多個數據
     *
     * @param key Redis鍵
     * @param values 多個數據
     * @return 存入的個數
     */
    public static long lPushAll(final String key, final Collection<Object> values) {
        Long count = redisTemplate.opsForList().rightPushAll(key, values);
        return count == null ? 0 : count;
    }

    /**
     * 往List中存入多個數據
     *
     * @param key Redis鍵
     * @param values 多個數據
     * @return 存入的個數
     */
    public static long lPushAll(final String key, final Object... values) {
        Long count = redisTemplate.opsForList().rightPushAll(key, values);
        return count == null ? 0 : count;
    }

    /**
     * 從List中獲取begin到end之間的元素
     *
     * @param key Redis鍵
     * @param start 開始位置
     * @param end 結束位置(start=0,end=-1表示獲取所有元素)
     * @return List對象
     */
    public static List<Object> lGet(final String key, final int start, final int end) {
        return redisTemplate.opsForList().range(key, start, end);
    }
}

這樣就能夠在代碼中隨時隨地的使用了app

@GetMapping("/test")
    public ApiResp test() {
        RedisUtils.set("key", "value");
        RedisUtils.get("key");
        return ApiResp.retOK();
    }

使用該工具類有一個小前提,SpringUtils中必須完成ApplicationContext的注入,注入方式有不少,此處就不表了spring-boot