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

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

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



import com.github.wzc789376152.springboot.config.redis.IRedisService;
import com.github.wzc789376152.utils.JSONUtils;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;

import java.util.*;

/**
 * spring redis 工具类
 *
 * @author jackw
 **/
public class RedisPlatformService extends RedisBaseService implements IRedisService {

    private RedisTemplate redisTemplate;

    public RedisPlatformService(RedisTemplate redisTemplate) {
        super(redisTemplate);
        this.redisTemplate = redisTemplate;
    }

    public  T getCacheObject(final String key, Class tClass) {
        ValueOperations operation = redisTemplate.opsForValue();
        Object result = operation.get(key);
        if (result != null) {
            return JSONUtils.parse(result, tClass);
        }
        return null;
    }

    public  List getCacheObjectList(final String key, Class tClass) {
        ValueOperations operation = redisTemplate.opsForValue();
        Object result = operation.get(key);
        if (result != null) {
            return JSONUtils.parseArray(result, tClass);
        }
        return null;
    }

    /**
     * 缓存List数据
     *
     * @param key      缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    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, Class tClass) {
        List jsonObjectList = getCacheList(key);
        if (jsonObjectList == null) {
            return null;
        }
        return JSONUtils.parseArray(jsonObjectList, tClass);
    }

    public  long removeCacheList(final String key, final List dataList) {
        long count = 0;
        for (T value : dataList) {
            count += redisTemplate.opsForList().remove(key, 1, value);
        }
        return count;
    }

    public  long removeCacheList(final String key, final T value) {
        return redisTemplate.opsForList().remove(key, 1, value);
    }

    /**
     * 缓存Set
     *
     * @param key     缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     */
    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, Class tClass) {
        Set setObject = getCacheSet(key);
        if (setObject == null) {
            return null;
        }
        return new HashSet<>(JSONUtils.parseArray(setObject, tClass));
    }

    public  long removeCacheSet(final String key, final T data) {
        SetOperations setOperation = redisTemplate.opsForSet();
        if (data != null && key != null) {
            return setOperation.remove(key, data);
        }
        return 0L;
    }

    public  long removeCacheSet(final String key, final Set dataSet) {
        SetOperations setOperation = redisTemplate.opsForSet();
        if (dataSet != null && key != null) {
            return setOperation.remove(key, dataSet.toArray());
        }
        return 0L;
    }

    /**
     * 缓存Map
     *
     * @param key key
     * @param dataMap dataMap
     */
    public  void setCacheMap(final String key, final Map dataMap) {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    /**
     * 获得缓存的Map
     *
     * @param key key
     * @return Map
     */
    public  Map getCacheMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public  Map getCacheMap(final String key, Class tClass) {
        Map mapObj = getCacheMap(key);
        if (mapObj == null) {
            return null;
        }
        Map map = new HashMap<>();
        for (String mapKey : mapObj.keySet()) {
            map.put(mapKey, JSONUtils.parse(mapObj.get(mapKey), tClass));
        }
        return map;
    }

    public  long removeCacheMap(final String key, Map dataMap) {
        return redisTemplate.opsForHash().delete(key, dataMap.keySet().toArray());
    }

    public long removeCacheMap(final String key, final String mapKey) {
        return redisTemplate.opsForHash().delete(key, mapKey);
    }

    @Override
    public  T getCacheMapValue(String key, String hKey, Class tClass) {
        Object object = this.getCacheMapValue(key, hKey);
        return JSONUtils.parse(object, tClass);
    }
}