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

com.zengtengpeng.operation.RedissonCollection Maven / Gradle / Ivy

package com.zengtengpeng.operation;

import com.zengtengpeng.func.*;
import com.zengtengpeng.properties.RedissonProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.*;
import org.springframework.util.ObjectUtils;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 操作集合
 */
public class RedissonCollection {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedissonProperties redissonProperties;

    /**
     * 获取map集合
     *
     * @param name
     * @param 
     * @param 
     * @return
     */
    public  RMap getMap(String name) {
        return redissonClient.getMap(name);
    }

    /**
     * 获取map集合,如果没有则通过实时数据
     *
     * @param name
     * @param 
     * @param 
     * @return
     */
    public  RMap getMap(String name, RealDataMap realDataMap) {
        return getMap(name, realDataMap, redissonProperties.getDataValidTime());
    }

    /**
     * 获取map集合,如果没有则通过实时数据
     *
     * @param name
     * @param 
     * @param 
     * @return
     */
    public  RMap getMap(String name, RealDataMap realDataMap, Long time) {
        RMap map = redissonClient.getMap(name);
        if (map == null || map.isEmpty()) {
            Map objectObjectMap = realDataMap.get();
            setMapValues(name, objectObjectMap, time);
        }
        return redissonClient.getMap(name);
    }

    /**
     * 获取map集合对应的key值
     *
     * @param name
     * @return
     */
    public  T getMapValue(String name, String key) {
        RMap map = getMap(name);
        return map.get(key);
    }

    /**
     * 先从map集合获取数据,如果没有则从接口获取
     *
     * @param name
     * @return
     */
    public  T getMapValue(String name, String key, RealData realData) {

        return getMapValue(name, key, realData, redissonProperties.getDataValidTime());
    }

    /**
     * 先从map集合获取数据,如果没有则从接口获取
     *
     * @param name
     * @return
     */
    public  T getMapValue(String name, String key, RealData realData, Long time) {
        RMap map = getMap(name);
        T o = map.get(key);
        if (o == null) {
            o = realData.get();
            if (ObjectUtils.isEmpty(o)) {
                map.remove(key);
            } else {
                setMapValue(name, key, o, time);
            }
        }
        return o;
    }

    /**
     * 先从map集合获取数据,如果没有则从接口获取
     *
     * @param name
     * @return
     */
    public  T getMapValue(String name, String key, RealData realData, DataCache dataCache, Long time) {
        RMap map = getMap(name);
        T o = map.get(key);
        if (o == null) {
            o = realData.get();
            if (ObjectUtils.isEmpty(o)) {
                map.remove(key);
            } else {
                Boolean cache = dataCache.isCache(o);
                if (cache) {
                    setMapValue(name, key, o, time);
                }
            }
        }
        return o;
    }

    /**
     * 设置map集合
     *
     * @param name
     * @param data
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setMapValues(String name, Map data, Long time) {
        RMap map = redissonClient.getMap(name);
        map.putAll(data);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            map.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            map.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置 map值
     *
     * @param name
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setMapValue(String name, String key, T value, Long time) {
        RMap map = redissonClient.getMap(name);
        map.put(key, value);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            map.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            map.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置map集合
     *
     * @param name
     * @param data
     * @return
     */
    public  void setMapValues(String name, Map data) {
        setMapValues(name, data, redissonProperties.getDataValidTime());
    }

    /**
     * 设置map集合
     *
     * @param name
     * @return
     */
    public void setMapValue(String name, String key, Object value) {
        setMapValue(name, key, value, redissonProperties.getDataValidTime());
    }

    /**
     * 获取List集合
     *
     * @param name
     * @return
     */
    public  RList getList(String name) {
        return redissonClient.getList(name);
    }

    /**
     * 获取List集合 如果没有则通过实时数据获取
     *
     * @param name
     * @return
     */
    public  RList getList(String name, RealDataList realDataList, Long time) {
        RList list = getList(name);
        if (list == null || list.isEmpty()) {
            List objects = realDataList.get();
            setListValues(name, objects, time);
        }
        return getList(name);
    }

    /**
     * 获取List集合对应的index值
     *
     * @param name
     * @return
     */
    public  RList getList(String name, RealDataList realDataList) {
        return getList(name, realDataList, redissonProperties.getDataValidTime());
    }

    /**
     * 获取List集合对应的index值
     *
     * @param name
     * @return
     */
    public  T getListValue(String name, Integer index) {
        RList list = getList(name);
        return list.get(index);
    }


    /**
     * 设置List集合
     *
     * @param name
     * @param data
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setListValues(String name, List data, Long time) {
        RList list = redissonClient.getList(name);
        list.addAll(data);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            list.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            list.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置List集合
     *
     * @param name
     * @param data
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setListValue(String name, T data, Long time) {
        RList list = redissonClient.getList(name);
        list.add(data);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            list.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            list.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置List集合
     *
     * @param name
     * @param data
     * @return
     */
    public  void setListValues(String name, List data) {
        setListValues(name, data, redissonProperties.getDataValidTime());
    }

    /**
     * 设置List集合
     *
     * @param name
     * @param data
     * @return
     */
    public void setListValue(String name, Object data) {
        setListValue(name, data, redissonProperties.getDataValidTime());
    }

    /**
     * 获取set集合
     *
     * @param name
     * @return
     */
    public  RSet getSet(String name) {
        return redissonClient.getSet(name);
    }

    /**
     * 获取List集合 如果没有则通过实时数据获取
     *
     * @param name
     * @return
     */
    public  RSet getSet(String name, RealDataSet realDataSet, Long time) {
        RSet set = getSet(name);
        if (set == null || set.isEmpty()) {
            Set objects = realDataSet.get();
            setSetValues(name, objects, time);
        }
        return getSet(name);
    }

    /**
     * 获取List集合对应的index值
     *
     * @param name
     * @return
     */
    public  RSet getSet(String name, RealDataSet realDataSet) {
        return getSet(name, realDataSet, redissonProperties.getDataValidTime());
    }

    /**
     * 设置set集合
     *
     * @param name
     * @param data
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setSetValues(String name, Set data, Long time) {
        RSet set = redissonClient.getSet(name);
        set.addAll(data);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            set.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            set.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置set集合
     *
     * @param name
     * @param data
     * @param time 缓存时间,单位毫秒 -1永久缓存
     * @return
     */
    public  void setSetValue(String name, T data, Long time) {
        RSet set = redissonClient.getSet(name);
        set.add(data);
        Long dataValidTime = redissonProperties.getDataValidTime();
        if (time == null) {
            set.expire(Duration.ofMillis(dataValidTime));
        } else if (time != -1) {
            set.expire(Duration.ofMillis(time));
        }
    }

    /**
     * 设置set集合
     *
     * @param name
     * @param data
     * @return
     */
    public  void setSetValues(String name, Set data) {
        setSetValues(name, data, redissonProperties.getDataValidTime());
    }

    /**
     * 设置set集合
     *
     * @param name
     * @param data
     * @return
     */
    public void setSetValues(String name, Object data) {
        setSetValue(name, data, redissonProperties.getDataValidTime());
    }

}