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

  

node操作 windows的appdata本地缓存文件的更多相关文章

  1. Windows下搭建本地SVN服务器【转】

    转自:http://www.linuxidc.com/Linux/2015-01/111563.htm 本文介绍Windows下搭建本地SVN服务器的方法,网上资料比较少也比较旧,大都介绍的是旧版本S ...

  2. 强势解决:windows 不能在本地计算机中起动Tomcat参考特定错误代码1

    Tomcat添加系统服务:service.bat install 启动本服务的时候却提示“windows 不能在本地计算机中起动 Apache Tomcat参考特定错误代码1,若不是Microsoft ...

  3. Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2

    Windows 不能在 本地计算机 启动 OracleDBConsoleorcl.有关更多信息,查阅系统事件日志.如果这是非 Microsoft 服务,请与服务厂商联系,并参考特定服务错误代码 2. ...

  4. Windows 不能在 本地计算机 启动 SQL Server 服务 错误代码126

    本文转自:http://www.cnblogs.com/yuerdongni/archive/2012/08/18/2645140.html 在使用SQL2005(或2008)是可能会遇到错误提示: ...

  5. windows不能在本地计算机启动SQL Server(MSSQLSERVER)

    windows不能在本地计算机启动sql server 在登录数据库的时候,发现数据库不能登录,提示[无法连接到实例],很明显这是因为数据库服务没有启动导致的,我们打开[服务]启动相应的SQL数据库服 ...

  6. 【转】解决Windows不能在本地计算机启动apache tomcat

    http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...

  7. winreg操作windows注册表详解示例

    #coding:utf-8 #=====================================================================#=====本程序演示了WINR ...

  8. C语言操作WINDOWS系统存储区数字证书相关函数详解及实例

     C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...

  9. C# 操作windows服务[启动、停止、卸载、安装]

    主要宗旨:不已命令形式操作windows服务 static void Main(string[] args) { var path = @"E:\开发辅助项目\WCF\WCF.Test\WC ...

随机推荐

  1. log4j组件的用法(log4j1)

    在实际的项目开发和维护中,日志是经常用到的一个内容.遇到问题的时候,经常需要通过日志去查出问题的所在并解决问题. 通常我们会用: System.out.println(xxx); 来打印运行中所需要的 ...

  2. elasticsearch与solr区别

    solr:优点1.Solr有一个更大.更成熟的用户.开发和贡献者社区.2.支持添加多种格式的索引,如:HTML.PDF.微软 Office 系列软件格式以及 JSON.XML.CSV 等纯文本格式.3 ...

  3. .NET Core开发日志——Middleware

    熟悉ASP.NET架构的开发者一定对于HTTP Modules与HTTP Handlers不陌生.两者的作用主要是对网络请求执行特定的处理工作.而在.NET Core中,它们都被Middleware( ...

  4. 进程池的同步方法 pool.apply

    from multiprocessing import Pool,Process def f1(n): print(n) return n*n if __name__ == "__main_ ...

  5. Codeforces 670F - Restore a Number - [字符串]

    题目链接:https://codeforces.com/contest/670/problem/F 题意: 有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去:例如 $6510$,加上位数 ...

  6. 知识驱动对话-Learning to Select Knowledge for Response Generation in Dialog Systems-阅读笔记

    今日看了一篇文章<Learning to Select Knowledge for Response Generation in Dialog Systems>,以知识信息.对话目标.对话 ...

  7. 【紫书】uva489 Hangman Judge 做了很久Orz

    题目链接:https://vjudge.net/problem/UVA-489 题意:给出两行字符串,第一行是标准答案,第二行是玩家猜的串.玩家每次猜一个,猜对一个,标准答案中所有该字符都算被猜到.猜 ...

  8. Haproxy的三种保持客户端会话保持方式

    2017-03-25 15:41:41   haproxy 三种保持客户端Seesion; 一.源地址hash(用户IP识别) haroxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类 ...

  9. ASP_NET实现界面无刷新的DropdownList两级联动效果

    所谓DropdownList联动,也就是在选一个DropdownList的时候使另外一个DropdownList的内容更新(如选省份时显示所属城市),按常规的方法那就是在第一个DropdownList ...

  10. lua--openresty--

    lua-openresty---docker-- https://blog.csdn.net/boling_cavalry/article/details/79290944 lua-practice ...