fkg 1.0.6 → 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.
Files changed (3) hide show
  1. package/README.md +5 -5
  2. package/lib/index.js +41 -23
  3. package/package.json +5 -2
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
  ```
@@ -33,9 +34,8 @@ user.set('id_2',{account:"wangwu",emai:"1001@qq.com",phone:"13800138002",name:"
33
34
  **5. Get user data**
34
35
 
35
36
  ```javascript
36
- user.list(10); //默认100条数据
37
- user.list(50); //默认100条数据
38
- ```
37
+ user.list(); //默认100条数据
38
+ user.list(50);
39
39
 
40
40
  **6. Get list data**
41
41
 
package/lib/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
  const {
3
3
  isDir,
4
- isFile,
5
4
  link,
6
5
  addDir,
7
6
  addJson,
@@ -9,18 +8,29 @@ const {
9
8
  delFile,
10
9
  readdirSync
11
10
  } = require('./utils/fs');
11
+ const {md5,ekey,dkey} = require('rrid');
12
12
  const {str,obj,arr} = require('./utils/type');
13
+ const getTime = ()=>{ return new Date().getTime();}
13
14
  class fkg {
14
15
  constructor(arg) {
15
- this.data = {};
16
- this.dirs = {};
17
- this.dir = null;
18
- if(arg&&obj(arg)) {
16
+ this.close();
17
+ if(arg&&obj(arg)) {
19
18
  if(arg.dir&&isDir(arg.dir)){
20
19
  this.dir = link(arg.dir,'./data');
21
20
  if(!isDir(this.dir)) addDir(this.dir);
22
21
  }
22
+ if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
23
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);
24
34
  for (let i = 0;i < arg.dbName.length;i++) {
25
35
  const n = arg.dbName[i];
26
36
  this.data[n] = {};
@@ -31,9 +41,14 @@ class fkg {
31
41
  }else{
32
42
  let list = readdirSync(this.dirs[n]);
33
43
  for (let i = 0;i < list.length;i++) {
34
- const value = readJson(link(this.dirs[n],'./'+list[i]));
35
- 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
+ }
36
50
  };
51
+ list = null;
37
52
  }
38
53
  }
39
54
  };
@@ -41,18 +56,16 @@ class fkg {
41
56
  }
42
57
  }
43
58
  close(){
59
+ this.lock = {};
60
+ this.data = {};
61
+ this.dirs = {};
44
62
  this.dir = null;
45
- this.data = null;
46
- this.dirs = null;
47
63
  }
48
64
  table(n) {
49
65
  const that = this;
50
66
  if(!n||!str(n)) return;
51
67
  if(!that.data[n]) that.data[n] = {};
52
- if(that.dir&&!that.dirs[n]) {
53
- that.dirs[n] = link(that.dir,'./'+n);
54
- addDir(that.dirs[n]);
55
- }
68
+ if(!that.lock[n]) that.lock[n] = {};
56
69
  return {
57
70
  list(page){
58
71
  function getOBJ(obj, n) {
@@ -65,34 +78,39 @@ class fkg {
65
78
  return getOBJ(that.data[n],page>0?page:100);
66
79
  },
67
80
  clear(){
68
- if(that.dir){
81
+ if(that.dirs[n]){
69
82
  let list = readdirSync(that.dirs[n]);
70
83
  for (let i = 0;i < list.length;i++) {
71
84
  delFile(link(that.dirs[n],'./'+list[i]));
72
85
  };
73
86
  list = null;
74
87
  }
75
- that.data[n] = {};
88
+ delete that.data[n];
89
+ delete that.lock[n];
76
90
  return true;
77
91
  },
78
92
  get(id){
79
- if(that.dir&&!that.data[n][id]) {
80
- const url = link(that.dirs[n],'./'+id);
81
- if(isFile(url)){
82
- const res = readJson(url);
83
- if(res) that.data[n][id] = res;
84
- }
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
+ }
85
101
  }
86
102
  return that.data[n][id];
87
103
  },
88
104
  set(id,d){
105
+ that.lock[n][id] = 1;
89
106
  that.data[n][id] = d;
90
- if(that.dir) addJson(link(that.dirs[n],'./'+id),d);
107
+ if(that.dirs[n]) that.setList[n+id] = {n,id};
108
+ delete that.lock[n][id];
91
109
  return true;
92
110
  },
93
111
  del(id){
94
112
  delete that.data[n][id];
95
- if(that.dir) delFile(link(that.dirs[n],'./'+id));
113
+ if(that.dirs[n]) delFile(link(that.dirs[n],'./'+id));
96
114
  return true;
97
115
  }
98
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fkg",
3
- "version": "1.0.6",
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
  }