Springboot操作redis setパッケージ

24125 ワード

目標:操作RedissetはHashSetのように便利
次の操作を行います.
RedisSet<String> domainSetCache = new RedisSet<String>(stringRedisTemplate,"web_domain") {};
domainSetCache.add("key1");

インタフェースの定義:
/**
 * create by zzb
 * 2018/10/11
 */
public interface CatcherSet<E> {

    boolean isEmpty();

    boolean contains(E value);

    E add(E value);

    boolean remove(E value);

    int size();

}

実装クラス:
import com.alibaba.fastjson.JSONObject;
import com.google.common.reflect.TypeToken;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * create by zzb
 * 2018/10/12
 */
public abstract class RedisSet<E> implements CatcherSet<E> {

    protected StringRedisTemplate redisEnv;

    private String preKey;
    private Type valueType;

    public RedisSet(StringRedisTemplate redisEnv, String preKey) {
        this.redisEnv = redisEnv;
        this.preKey = preKey;
        getGenType();
    }

    @Override
    public boolean isEmpty() {
        return redisEnv.opsForSet().size(preKey) == 0;
    }

    @Override
    public boolean contains(E value) {
        if(value==null) return false;
        return redisEnv.opsForSet().isMember(preKey, JSONObject.toJSONString(value));
    }

    @Override
    public int size() {
        return redisEnv.opsForSet().size(preKey).intValue();
    }

    @Override
    public E add(E value) {
        if(value==null) return null;
        redisEnv.opsForSet().add(preKey, JSONObject.toJSONString(value));
        return value;
    }

    public void addAll(Set<E> set){
        if(set!=null && set.size() > 0){
            List<String> list = set.stream().map(v->serializeObject(v)).collect(Collectors.toList());
            String[] array = new String[set.size()];
            list.toArray(array);
            redisEnv.opsForSet().add(preKey,array);
        }
    }

    public Set<E> getAll(){
        Set<String> set = redisEnv.opsForSet().members(preKey);
        return set.stream().map(v->parseValue(v)).collect(Collectors.toSet());
    }
    public Set<E> getAll(Supplier<Set<E>> supplier){
        Set<E> result = getAll();
        if(result.size()==0){
            result = supplier.get();
            addAll(result);
        }
        return result;
    }

    @Override
    public boolean remove(E value) {
        if(value==null) return false;
        return redisEnv.opsForSet().remove(preKey, JSONObject.toJSONString(value)) > 0;
    }
    //        
    public void getGenType(){
        this.valueType = new TypeToken<E>(getClass()) {}.getType();
    }

    public E parseValue(Object object){
        if(object == null) return null;
        if(object instanceof String){
            return (E)object;
        }
        return JSONObject.parseObject(object.toString(),valueType);
    }

    private String serializeObject(Object object){
        if(object instanceof String){
            return (String)object;
        }
        return JSONObject.toJSONString(object);
    }
    public void expire(long timeout, TimeUnit unit){
        redisEnv.expire(this.preKey,timeout,unit);
    }
}