Redisson用法

用法一

//注入
@Autowired
private RedissonClient redissonClient;
//初始化创建一个RMap
private RMap<String, Object> rMap;

static final String R_MAP = "rMap";
//在执行spring注入后执行
 @PostConstruct
 private void init() {
     rMap = redissonClient.getMapCache(R_MAP);
 }

//--------------------如果不进行全局初始化的话 可以每次自己创建

//创建一个RList
RList<Object> flushCacheHistory = redissonClient.getList("名称");
//创建一个RMap
RMap<String, Object> flushMap = redissonClient.getMap("名称");


用法二 原文链接:https://www.lovecto.cn/20190125/420.html

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.redisson.api.BatchOptions;
import org.redisson.api.RBatch;
import org.redisson.api.RMap;
import org.redisson.api.RMapAsync;
import org.redisson.api.RMapCache;
import org.redisson.api.RMapCacheAsync;
import org.redisson.api.RSetCache;
import org.redisson.api.RSetCacheAsync;
import org.redisson.api.RedissonClient;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;


public class CacheMap {

    /** 空值缓存时间10分钟 */
    private final static int NULL_VALUE_KEY_CACHE_MILLISECONDS = 10 * 60 * 1000;

    /** redisson map */
    private RMap rMap;
    /** 集合用于缓存空值 */
    private RSetCache rSet;
    /** redisson */
    private RedissonClient redisson;
    /** 批量操作选项 */
    private BatchOptions options = BatchOptions.defaults();
    /** 是否有ttl控制 */
    private boolean ttlControl;
    /** 缓存名称 */
    private String name;
    /** 集合名称 */
    private String setName;
    /** 是否缓存null */
    private boolean cacheNull;

    /**
     * 静态构造方法
     * 
     * @param redisson
     *            redisson客户端
     * @param name
     *            缓存名称
     * @param ttlControl
     *            是否有缓存时间控制
     * @return
     */
    public static CacheMap of(RedissonClient redisson, String name,
            boolean ttlControl, boolean cacheNull) {
        return new CacheMap(redisson, name, ttlControl, cacheNull);
    }

    /**
     * 
     * @param redisson
     * @param name
     * @param ttlControl
     */
    private CacheMap(RedissonClient redisson, String name,
            boolean ttlControl, boolean cacheNull) {
        this.name = name;
        this.setName = name + "_null_value_key_set";
        if (ttlControl) {
            this.rMap = redisson.getMapCache(this.name);
        } else {
            this.rMap = redisson.getMap(this.name);
        }
        if (cacheNull) {
            this.rSet = redisson.getSetCache(this.setName);
        }
        this.ttlControl = ttlControl;
        // 批量操作
        this.redisson = redisson;
        this.cacheNull = cacheNull;
    }

    /**
     * 判断是否存在key
     * 
     * @param key
     * @return
     */
    public boolean containsKey(Object key) {
        boolean r = rMap.containsKey(key);
        if (!r) {
            if(cacheNull){
                r = rSet.contains(key);
            }
        }
        return r;
    }

    /**
     * 清除缓存
     */
    public void clear() {
        rMap.clear();
        if (cacheNull) {
            rSet.clear();
        }
    }

    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public Map get(Object key) {
        Map map = Maps.newHashMapWithExpectedSize(1);
        Object r = rMap.get(key);
        if ((r == null && cacheNull && rSet.contains(key)) || r != null) {
            map.put(key, r);
        }
        return map;
    }

    /**
     * 存入map
     * 
     * @param key
     * @param value
     * @return
     */
    public Object put(Object key, Object value) {
        if (value == null) {
            if (cacheNull) {
                rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
                        TimeUnit.MILLISECONDS);
            }
        } else {
            rMap.put(key, value);
        }
        return value;
    }

    /**
     * 移除
     * 
     * @param key
     * @return
     */
    public Object remove(Object key) {
        Object o = rMap.remove(key);
        if (cacheNull) {
            rSet.remove(key);
        }
        return o;
    }

    /**
     * 批量存储
     * 
     * @param map
     */
    public void putAll(Map map) {
        RBatch batch = redisson.createBatch(options);
        RMapAsync mapCache = ttlControl ? batch.getMapCache(this.name) : batch
                .getMap(this.name);
        RSetCacheAsync setCache = batch.getSetCache(this.setName);
        Set<Map.Entry> entrySet = map.entrySet();
        Iterator<Map.Entry> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry e = it.next();
            if (e.getValue() == null) {
                if (cacheNull) {// 缓存null
                    setCache.addAsync(e.getKey(),
                            NULL_VALUE_KEY_CACHE_MILLISECONDS,
                            TimeUnit.MILLISECONDS);
                }
            } else {
                mapCache.putAsync(e.getKey(), e.getValue());
            }
        }
        batch.execute();
    }

    /**
     * 批量获取
     * 
     * @param keys
     * @return
     */
    public Map getAll(Set keys) {
        Map map = rMap.getAll(keys);
        if (keys.size() == map.size()) {
            return map;
        } else if (cacheNull) {
            Set set = rSet.readAll();
            for (Object key : keys) {
                if (!map.containsKey(key)) {
                    if (set.contains(key)) {
                        map.put(key, null);
                    }
                }
            }
        }
        return map;
    }

    /**
     * 批量移除
     * 
     * @param keys
     * @return
     */
    public long fastRemove(Object... keys) {
        long r = rMap.fastRemove(keys);
        if (cacheNull) {
            rSet.removeAll(Sets.newHashSet(keys));
        }
        return r;
    }

    /**
     * 快速存
     * 
     * @param key
     * @param value
     * @return
     */
    public boolean fastPut(Object key, Object value) {
        boolean r = true;
        if (value != null) {
            r = rMap.fastPut(key, value);
        } else if (cacheNull) {
            rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
                    TimeUnit.MILLISECONDS);
        }
        return false;
    }

    /**
     * 带过期时间存
     * 
     * @param key
     * @param value
     * @param ttl
     *            单位毫秒
     * @return
     */
    public Object put(Object key, Object value, long ttl) {
        if(ttl <= 0){
            return put(key, value);
        }
        if (value != null) {
            if (ttlControl) {
                ((RMapCache) rMap).put(key, value, ttl, TimeUnit.MILLISECONDS);
            } else {
                throw new IllegalArgumentException("can not support ttl");
            }
        } else if (cacheNull) {
            rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
                    TimeUnit.MILLISECONDS);
        }
        return value;
    }

    /**
     * 带过期时间的putAll
     * 
     * @param map
     * @param ttl
     */
    public void putAll(Map map, long ttl) {
        if(ttl <= 0){
            putAll(map);
            return;
        }
        RBatch batch = redisson.createBatch(options);
        RMapCacheAsync rMapCache = batch.getMapCache(this.name);
        Set<Map.Entry> entrySet = map.entrySet();
        Iterator<Map.Entry> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry e = it.next();
            if (e.getValue() != null) {
                if (ttlControl) {
                    rMapCache.putAsync(e.getKey(), e.getValue(), ttl,
                            TimeUnit.MILLISECONDS);
                } else {
                    throw new IllegalArgumentException("can not support ttl");
                }
            } else if (cacheNull) {
                rSet.add(e.getKey(), NULL_VALUE_KEY_CACHE_MILLISECONDS,
                        TimeUnit.MILLISECONDS);
            }
        }
        // 批量操作
        batch.execute();
    }

    /**
     * 快速存
     * 
     * @param key
     * @param value
     * @param ttl
     * @param ttlUnit
     * @return
     */
    public boolean fastPut(Object key, Object value, long ttl) {
        boolean r = true;
        if (value != null) {
            if (ttlControl) {
                r = ((RMapCache) rMap).fastPut(key, value, ttl,
                        TimeUnit.MILLISECONDS);
            } else {
                throw new IllegalArgumentException("can not support ttl");
            }
        } else if (cacheNull) {
            rSet.add(key, NULL_VALUE_KEY_CACHE_MILLISECONDS,
                    TimeUnit.MILLISECONDS);
        }
        return r;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值