最全的 基於 RedisTemplate 封裝的 RedisUtils 工具類

RedisTemplate 功能請參考 : RedisTemplate用法詳解

SpringUtils.java  獲取 RedisTemplate bean 對象使用

/**
 * spring工具類 方便在非spring管理環境中獲取bean
 * 
 * @author Lion Li
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor
{
    /** Spring應用上下文環境 */
    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

    /**
     * 獲取對象
     *
     * @param name
     * @return Object 一個以所給名字註冊的bean的實例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 獲取類型爲requiredType的對象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException
    {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

}

RedisUtils.java  redis工具類  參考網上寫的做了一些補全

/**
 * Redis工具類,使用之前請確保RedisTemplate成功注入
 *
 * @author Lion Li
 * @version 2019-12-06 09:05:38
 */
public class RedisUtils {

    private RedisUtils() {
    }

    @SuppressWarnings("unchecked")
    private static RedisTemplate<String, Object> redisTemplate = SpringUtils.getBean(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 delKey(final String key) {

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

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

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

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

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

    // 存儲普通對象操作

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

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

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

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

    // 存儲Hash操作

    /**
     * 確定哈希hashKey是否存在
     *
     * @param key 鍵
     * @param hkey hash鍵
     * @return true=存在;false=不存在
     */
    public static boolean hasHashKey(final String key,String hkey) {

        Boolean ret = redisTemplate.opsForHash().hasKey(key,hkey);
        return ret != null && ret;
    }

    /**
     * 往Hash中存入數據
     *
     * @param key Redis鍵
     * @param hKey Hash鍵
     * @param value 值
     */
    public static void hashPut(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 hashPutAll(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 hashGet(final String key, final String hKey) {

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

    /**
     * 獲取Hash中的數據
     *
     * @param key Redis鍵
     * @return Hash對象
     */
    public static Map<Object, Object> hashGetAll(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

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

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

    /**
     * 刪除Hash中的數據
     *
     * @param key Redis鍵
     * @param hKeys Hash鍵集合
     * @return Hash對象集合
     */
    public static long hashDeleteKeys(final String key, final Collection<Object> hKeys) {
        return redisTemplate.opsForHash().delete(key,hKeys);
    }

    // 存儲Set相關操作

    /**
     * 往Set中存入數據
     *
     * @param key Redis鍵
     * @param values 值
     * @return 存入的個數
     */
    public static long setSet(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 setDel(final String key, final Object... values) {
        Long count = redisTemplate.opsForSet().remove(key, values);
        return count == null ? 0 : count;
    }

    /**
     * 獲取set中的所有對象
     *
     * @param key Redis鍵
     * @return set集合
     */
    public static  Set<Object> getSetAll(final String key) {
        return redisTemplate.opsForSet().members(key);
    }

    // 存儲ZSet相關操作

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

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

    // 存儲List相關操作

    /**
     * 往List中存入數據
     *
     * @param key Redis鍵
     * @param value 數據
     * @return 存入的個數
     */
    public static long listPush(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 listPushAll(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 listPushAll(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> listGet(final String key, final int start, final int end) {
        return redisTemplate.opsForList().range(key, start, end);
    }
}

 

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