• 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();
    

      

  • 相关阅读:
    [转] Vue + Webpack 组件式开发(练习环境)
    [转] 从零构建 vue2 + vue-router + vuex 开发环境到入门,实现基本的登录退出功能
    [转] Redux入门教程(快速上手)
    [转] 前端数据驱动的价值
    [转] React风格的企业前端技术
    [转] 对Array.prototype.slice.call()方法的理解
    [转] webpack之plugin内部运行机制
    [转] 静态资源的分布对网站加载速度的影响/浏览器对同一域名下并发加载资源数量
    Mysql 版本号、存储引擎、索引查询
    linux 查看CPU、内存、磁盘信息命令
  • 原文地址:https://www.cnblogs.com/muamaker/p/10041200.html
Copyright © 2020-2023  润新知