fkg 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,8 +16,9 @@ npm install fkg
16
16
  ```javascript
17
17
  const fkg = require('fkg');
18
18
  const db = new fkg({
19
- dir: 'C:\\work', //数据储存位置,为空时不进行存储
20
- dbName: ['user'] //需要数据储存时,数据表名不可为空
19
+ key:'f31d0353cdf', //key不为空时,储存的数据会被加密
20
+ dir: 'C:\\work', //数据储存位置,为空时不进行存储
21
+ dbName: ['user'] //需要数据储存时,数据表名不可为空
21
22
  });
22
23
  const user = db.table('user');
23
24
  ```
package/lib/index.js CHANGED
@@ -8,16 +8,29 @@ const {
8
8
  delFile,
9
9
  readdirSync
10
10
  } = require('./utils/fs');
11
+ const {md5,ekey,dkey} = require('rrid');
11
12
  const {str,obj,arr} = require('./utils/type');
13
+ const getTime = ()=>{ return new Date().getTime();}
12
14
  class fkg {
13
15
  constructor(arg) {
14
16
  this.close();
15
- if(arg&&obj(arg)) {
17
+ if(arg&&obj(arg)) {
16
18
  if(arg.dir&&isDir(arg.dir)){
17
19
  this.dir = link(arg.dir,'./data');
18
20
  if(!isDir(this.dir)) addDir(this.dir);
19
21
  }
22
+ if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
20
23
  if(arg.dbName&&arr(arg.dbName)&&arr.length>0){
24
+ this.setList = {};
25
+ setInterval(() => {
26
+ for (const keys in this.setList) {
27
+ const {n,id} = this.setList[keys];
28
+ let d = this.data[n][id];
29
+ if(this.key) d = ekey(JSON.stringify(d),this.key+id);
30
+ addJson(link(this.dirs[n],'./'+id),d);
31
+ delete this.setList[keys];
32
+ }
33
+ }, 3000);
21
34
  for (let i = 0;i < arg.dbName.length;i++) {
22
35
  const n = arg.dbName[i];
23
36
  this.data[n] = {};
@@ -28,8 +41,12 @@ class fkg {
28
41
  }else{
29
42
  let list = readdirSync(this.dirs[n]);
30
43
  for (let i = 0;i < list.length;i++) {
31
- const value = readJson(link(this.dirs[n],'./'+list[i]));
32
- if(value!==null) this.data[n][list[i]] = value;
44
+ const id = list[i];
45
+ let value = readJson(link(this.dirs[n],'./'+id));
46
+ if(value) {
47
+ if(this.key) value = JSON.parse(dkey(value,this.key+id));
48
+ this.data[n][id] = value;
49
+ }
33
50
  };
34
51
  list = null;
35
52
  }
@@ -39,6 +56,7 @@ class fkg {
39
56
  }
40
57
  }
41
58
  close(){
59
+ this.lock = {};
42
60
  this.data = {};
43
61
  this.dirs = {};
44
62
  this.dir = null;
@@ -47,6 +65,7 @@ class fkg {
47
65
  const that = this;
48
66
  if(!n||!str(n)) return;
49
67
  if(!that.data[n]) that.data[n] = {};
68
+ if(!that.lock[n]) that.lock[n] = {};
50
69
  return {
51
70
  list(page){
52
71
  function getOBJ(obj, n) {
@@ -59,27 +78,39 @@ class fkg {
59
78
  return getOBJ(that.data[n],page>0?page:100);
60
79
  },
61
80
  clear(){
62
- if(that.dir&&that.dirs[n]){
81
+ if(that.dirs[n]){
63
82
  let list = readdirSync(that.dirs[n]);
64
83
  for (let i = 0;i < list.length;i++) {
65
84
  delFile(link(that.dirs[n],'./'+list[i]));
66
85
  };
67
86
  list = null;
68
87
  }
69
- that.data[n] = {};
88
+ delete that.data[n];
89
+ delete that.lock[n];
70
90
  return true;
71
91
  },
72
92
  get(id){
93
+ if(that.lock[n][id]){
94
+ const time = getTime();
95
+ while (that.lock[n][id]) {
96
+ // 等待解锁
97
+ if(getTime()-time>3000) {
98
+ delete that.lock[n][id];
99
+ }
100
+ }
101
+ }
73
102
  return that.data[n][id];
74
103
  },
75
104
  set(id,d){
105
+ that.lock[n][id] = 1;
76
106
  that.data[n][id] = d;
77
- if(that.dir&&that.dirs[n]) addJson(link(that.dirs[n],'./'+id),d);
107
+ if(that.dirs[n]) that.setList[n+id] = {n,id};
108
+ delete that.lock[n][id];
78
109
  return true;
79
110
  },
80
111
  del(id){
81
112
  delete that.data[n][id];
82
- if(that.dir&&that.dirs[n]) delFile(link(that.dirs[n],'./'+id));
113
+ if(that.dirs[n]) delFile(link(that.dirs[n],'./'+id));
83
114
  return true;
84
115
  }
85
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fkg",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Read and set memory key!",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [
@@ -8,5 +8,8 @@
8
8
  "cache"
9
9
  ],
10
10
  "author": "weduu",
11
- "license": "MIT"
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "rrid": "^1.0.6"
14
+ }
12
15
  }