MapDB工具类

参考:http://www.mapdb.org/doc/

package com.gardenplus.gardencase.api.util;

import com.alibaba.fastjson.JSON;
import com.mango.common.utils.basic.StrUtil;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;

import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * mapDB缓存工具类
 * @author alen
 * @create 2018-01-22 15:10
 **/
public class MapDBUtil {

    /*
    缓存回收策略/基于容量
    expireMaxSize:设置缓存的容量,当超出expireMaxSize时,按照LRU进行缓存回收。
    缓存回收策略/基于时间
    expireAfterCreate/expireAfterUpdate:设置TTL,缓存数据在给定的时间内没有写(创建/覆盖)时,则被回收。即定期的会回收缓存数据。
    expireAfterGet:设置TTI, 缓存数据在给定的时间内没有读/写时,则被回收。每次访问时都会更新它的TTI,从而如果该缓存是非常热的数据,则将一直不过期,
    可能会导致脏数据存在很长的时间(因此,建议要设置expireAfterCreate/expireAfterUpdate)
    SECONDS          秒
    MINUTES     分钟
    HOURS      小时
  */

    private static final ConcurrentMap defaultMap = DBMaker.memoryDB().make()
            .hashMap("CaseCache", Serializer.STRING, Serializer.STRING)
            .expireMaxSize(10000)
            .expireAfterCreate(10, TimeUnit.MINUTES)
            .expireAfterUpdate(10, TimeUnit.MINUTES)
            .createOrOpen();

    private static final DB db = DBMaker.memoryDB().make();


    public static void put(String key, Object value, String... strings) {
        save(key, value, null, strings);
    }

    public static void put(String key, Object value, Integer expireTime, String... strings) {
        save(key, value, expireTime, strings);
    }


    /**
     * 返回集合
     *
     * @param key
     * @param type
     * @param <D>
     * @return
     */
    public static <D> D get(String key, Type type, String... strings) {

        return loadType(key, type, strings);
    }


    /**
     * 返回对象
     *
     * @param key
     * @param D
     * @param <D>
     * @return
     */
    public static <D> D get(String key, Class<D> D, String... strings) {
        return load(key, D, strings);
    }

    public static Object get(String key, String... strings) {
        String mapName = null;
        if (strings != null && strings.length > 0) {
            mapName = strings[0];
        }
        return load(key, mapName);
    }

    private static void save(String key, Object value, Integer expireTime, String... strings) {
        if (StrUtil.isNotBlank(key) && value != null) {
            if (strings != null && strings.length > 0) {
                String mapName = strings[0];
                ConcurrentMap concurrentMap = null;
                if (expireTime != null) {
                    concurrentMap = db.hashMap(mapName, Serializer.STRING, Serializer.STRING)
                            .expireMaxSize(10000)
                            .expireAfterCreate(expireTime, TimeUnit.MINUTES)
                            .expireAfterUpdate(expireTime, TimeUnit.MINUTES)
                            .createOrOpen();
                } else {
                    concurrentMap = db.hashMap(mapName, Serializer.STRING, Serializer.STRING).createOrOpen();
                }
                concurrentMap.put(key, JSON.toJSONString(value));
                return;
            }
            defaultMap.put(key, JSON.toJSONString(value));
        }
    }

    private static Object load(String key, String mapName) {
        if (StrUtil.isBlank(key)) {
            return null;
        }
        if (StrUtil.isNotBlank(mapName)) {
            ConcurrentMap concurrentMap = db.hashMap(mapName, Serializer.STRING, Serializer.STRING).createOrOpen();
            if (concurrentMap != null) {
                return concurrentMap.get(key);
            }
        }
        return defaultMap.get(key);
    }

    private static <D> D load(String key, Class<D> D, String... strings) {
        if (StrUtil.isBlank(key) || D == null) {
            return null;
        }
        String keyStr = null;
        if (strings != null && strings.length > 0) {
            String mapName = strings[0];
            ConcurrentMap concurrentMap = db.hashMap(mapName, Serializer.STRING, Serializer.STRING).createOrOpen();
            if (concurrentMap != null) {
                keyStr = (String) concurrentMap.get(key);
                return JSON.parseObject(keyStr, D);
            }
        }
        keyStr = (String) defaultMap.get(key);
        if (StrUtil.isBlank(keyStr)) {
            return null;
        }
        return JSON.parseObject(keyStr, D);
    }

    private static <D> D loadType(String key, Type type, String... strings) {
        if (StrUtil.isBlank(key) || type == null) {
            return null;
        }
        String keyStr = null;
        if (strings != null && strings.length > 0) {
            String mapName = strings[0];
            ConcurrentMap concurrentMap = db.hashMap(mapName, Serializer.STRING, Serializer.STRING).createOrOpen();
            if (concurrentMap != null) {
                keyStr = (String) concurrentMap.get(key);
                return JSON.parseObject(keyStr, type);
            }
        }
        keyStr = (String) defaultMap.get(key);
        if (StrUtil.isBlank(keyStr)) {
            return null;
        }
        return JSON.parseObject(keyStr, type);
    }

    public static void main(String[] args) {
//        List<GardenSettingBO> list = new ArrayList<>();
//        GardenSettingBO gardenSettingBO = new GardenSettingBO();
//        gardenSettingBO.setCode("11");
//        gardenSettingBO.setId(111);
//        gardenSettingBO.setValue("yuuyu");
//        list.add(gardenSettingBO);
//        MapDBUtil.put("style", 1111);
//        MapDBUtil.put("style", list);
//        MapDBUtil.put("gardenSettingBO", gardenSettingBO);
//        List<GardenSettingBO> list2 = MapDBUtil.get("style", new TypeReference<List<GardenSettingBO>>() {
//        }.getType());
//        GardenSettingBO gardenSettingBO2 = list2.get(0);
//        System.out.println("-----" + gardenSettingBO2.getId());
//
//        MapDBUtil.put("style", 1111, "testmap");
//        System.out.println("-----" + MapDBUtil.get("style", Integer.TYPE, "testmap"));
//        System.out.println("-----" + MapDBUtil.get("style", "testmap"));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值