zoukankan      html  css  js  c++  java
  • node操作 windows的appdata本地缓存文件

    
    
    const os = require('os');
    const path = require("path");
    const fs = require("fs");
     
    var homedir = os.homedir();
     
    function mkdirs(dirpath) {
        if (!fs.existsSync(path.dirname(dirpath))) {
          mkdirs(path.dirname(dirpath));
        }
        fs.mkdirSync(dirpath);
    }
     
    function createDir(myPath){
        fs.existsSync(myPath) == false && mkdirs(myPath);
    }
     
    
    var _x = Symbol("x");
    
    class AppData{
        constructor(dbnm){
            if(!dbnm){
                throw new Error("the database name is needless");
                return;
            }
            dbnm = dbnm + ".info";
            this.apppath = path.join(homedir,"/AppData/Local/excelMaster/", dbnm);     
            createDir(path.dirname(this.apppath));
     
        }
        connect(cd){
            cd = cd || function(){};
            fs.readFile(this.apppath,"utf-8",function(err,res){
                if(err){
                    this[_x] = {};
                }else{
                    var str = Buffer.from(res, 'base64').toString("utf8");
                    if(str){
                        try{
                            this[_x] = JSON.parse(str);
                        }catch(e){
                            this[_x] = {};
                        }
                    }else{
                        this[_x] = {};
                    }
                }
                cd(err,res);
            });
        }
        connectSync(){
            try{
                var res = fs.readFileSync(this.apppath,"utf-8");
                var str = Buffer.from(res, 'base64').toString("utf8");
                if(str){
                    try{
                        this[_x] = JSON.parse(str);
                    }catch(e){
                        this[_x] = {};
                    }
                }else{
                    this[_x] = {};
                }   
            }catch(e){
                this[_x] = {};
            }
             
        }
        get(k){
            return this[_x][k];
        }
        set(k,val,callback){
            callback = callback || function(){};
            this[_x][k] = val;
            const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
            fs.writeFile(this.apppath,buf.toString('base64'),callback);
        }
        setSync(k,val){
            this[_x][k] = val;
            const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
            fs.writeFileSync(this.apppath,buf.toString('base64'));
        }
        setSyncObj(obj){
        	for(var i in obj){
        		this[_x][i] = obj[i];
        	}
        	const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
            fs.writeFileSync(this.apppath,buf.toString('base64'));
        }
        has(k){
            return k in this[_x];
        }
        keys(){
            return Object.keys(this[_x]);
        }
        values(){
            return Object.values(this[_x]);
        }
        drop(){
            this[_x] = {};
            const buf = Buffer.from(JSON.stringify({}), 'utf8');
            fs.writeFileSync(this.apppath,buf.toString('base64'));
        }
        disconnect(){
            this.apppath = null;
            delete this[_x];
        }
    }
     
    module.exports = AppData;
    

      

    使用方式

    var AppData = require("./appdata");
    
    var p = new AppData("abc");
    
    p.connectSync();
    
    //p.setSync("manny","28");
    
    console.log(p.get("manny"))
    
    p.disconnect();
    

      

  • 相关阅读:
    表数据文件DBF的读取和写入操作
    【转】Hive SQL的编译过程
    Hadoop配置机架感知
    pssh安装和使用
    hive12启动报错org.apache.thrift.server.TThreadPoolServer.<init>(Lorg/apache/thrift/server/TThreadPoolServer$Args;)
    【转】Oozie4.2.0配置安装实战
    【转】别因为要学的太多反而压垮自己
    【转】hadoop2.6 配置lzo压缩
    hive 启动 Specified key was too long; max key length is 767 bytes解决办法
    【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法
  • 原文地址:https://www.cnblogs.com/muamaker/p/10041200.html
Copyright © 2011-2022 走看看