SpingBoot集成MapDb

MapDb是一个基于jvm内存的轻量级数据库,可以持久化map数据,有点类似nosql,不需要配置连接,在项目中集成就行

一、配置

pom文件依赖和仓库地址:


    org.mapdb
    mapdb
    3.1.0-SNAPSHOT

    
        sonatype-snapshots
        https://oss.sonatype.org/content/repositories/snapshots
    

说明一下:

mapdb的依赖包引入的话,springboot项目同时依赖了2.9.0版本的springfox-swagger2和springfox-swagger-ui这两个包有可能启动项目会报错,只要把springfox-swagger2这个包版本改成2.8.0就能解决

 二、封装

操作连接后一定要commit或者rollback,否则下次再调用会报文件被锁的问题,我这没有写close方法是因为连接消耗资源,如果频繁的连接关闭会影响性能

@Slf4j
public class DataCache {

    public static void set(String key, String value){
        MapDbConnection.set(key,value);
    }

    public static ConcurrentNavigableMap getMap(){
        ConcurrentNavigableMap map = MapDbConnection.getMap();
        return map;
    }

    public static DB getConnection(){
        return MapDbConnection.getConection();
    }

    public static void  commit(){
        MapDbConnection.db.commit();
    }

    public static void rollback(){
        MapDbConnection.db.rollback();
    }

    /**
     * mapdb连接类
     */
    private static final class MapDbConnection{
        /**
         * db存储文件名
         */
        private static final String DB_FILE_NAME = "monitorDb";

        /**
         * 存储设备信息
         */
        private static ConcurrentNavigableMap DEVICE_MSG_MAP;

        /**
         * 序列化key值
         */
        private final static String MSG_KEY = "msgKey";

        private static DB db;

        static {
            //初始化连接
            init();
            db.commit();
        }

        private static void init(){
            log.debug("开始连接mapdb......");
            //开启事务,开启jvm关闭时同时关闭db,开启mmap
            db = DBMaker.fileDB(new File(DB_FILE_NAME))
                    .fileMmapEnableIfSupported()
                    .fileMmapPreclearDisable()
                    .transactionEnable()
                    .closeOnJvmShutdown()
                    .make();
            DEVICE_MSG_MAP = db.treeMap(MSG_KEY)
                    .keySerializer(Serializer.STRING)
                    .valueSerializer(Serializer.STRING)
                    .createOrOpen()
            ;
            if(db != null){
                log.debug("连接mapdb成功......");
            }
        }

        /**
         * 获取连接
         */
        private synchronized static DB getConection(){
            if(db.isClosed() || db == null){
                init();
                if(db.isClosed() || db == null){
                    throw new NullPointerException("mapdb connection faild");
                }
            }
            return db;
        }

        /**
         * 获取map
         * @return
         */
        private static ConcurrentNavigableMap getMap(){
            getConection();
            return DEVICE_MSG_MAP;
        }

        /**
         * 存值
         * @param key
         * @param value
         */
        private synchronized static boolean set(String key,String value){
            try{
                final DevTaskMsg parse = (DevTaskMsg) JSONObject.parse(value);
                if(parse == null){
                    throw new ClassCastException();
                }
                DEVICE_MSG_MAP.put(key,value);
            } catch (Exception e) {
                db.rollback();
                log.error("set map faild",e);
                return false;
            }
            return true;
        }
    }
}

你可能感兴趣的:(springboot,mapdb,java)