memory-sri 1.2.8 → 1.2.11
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 +125 -39
- package/index.d.ts +1 -0
- package/lib/main.js +13 -4
- package/lib/worker.js +9 -5
- package/package.json +10 -9
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Memory Library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Powerful yet simple memory and queue manager for building efficient Node.js apps.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ You can install this package using NPM:
|
|
|
10
10
|
npm install memory-sri
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## Import memory-sri
|
|
14
14
|
```bash
|
|
15
15
|
import Memory from 'memory-sri';
|
|
16
16
|
```
|
|
@@ -19,6 +19,28 @@ import Memory from 'memory-sri';
|
|
|
19
19
|
```bash
|
|
20
20
|
const Memory = require('memory-sri');
|
|
21
21
|
```
|
|
22
|
+
## 🛠 API Reference
|
|
23
|
+
#### Memory store
|
|
24
|
+
```bash
|
|
25
|
+
Memory.set(key, value, ttlSeconds?)
|
|
26
|
+
Memory.get(key)
|
|
27
|
+
Memory.ttl(key)
|
|
28
|
+
Memory.del(key)
|
|
29
|
+
Memory.multiple()
|
|
30
|
+
Memory.delall()
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### Task queue
|
|
34
|
+
```bash
|
|
35
|
+
Memory.process(name, fn) → Register a processor for a task type
|
|
36
|
+
Memory.addtask(name, data, retries?) → Enqueue a job
|
|
37
|
+
Memory.runtask(name) → Manually run a queued job
|
|
38
|
+
```
|
|
39
|
+
## ⚠️ Notes
|
|
40
|
+
```bash
|
|
41
|
+
This is in-memory only (no persistence). Data clears when the process restarts.
|
|
42
|
+
Use in production only for lightweight or temporary jobs.
|
|
43
|
+
```
|
|
22
44
|
|
|
23
45
|
## Example of Full Usage:
|
|
24
46
|
### function has three parameters: Identifier, Data, Expire
|
|
@@ -42,26 +64,26 @@ Memory.set('5', "not set expire");
|
|
|
42
64
|
#### set
|
|
43
65
|
```bash
|
|
44
66
|
// for set it take job to stored data
|
|
45
|
-
Memory.set(
|
|
46
|
-
Memory.set(
|
|
47
|
-
Memory.set(
|
|
48
|
-
Memory.set(
|
|
67
|
+
Memory.set(1, 'a1', 20);
|
|
68
|
+
Memory.set(2, { b: 2 }, 30);
|
|
69
|
+
Memory.set(3, [2, 3], 40);
|
|
70
|
+
Memory.set(4, [{ a: 'a1', b: 'b1' }], 50);
|
|
49
71
|
Memory.set('5', "not set expire");
|
|
50
72
|
```
|
|
51
73
|
|
|
52
74
|
#### get
|
|
53
75
|
```bash
|
|
54
76
|
// for get data realtime
|
|
55
|
-
let get1 = Memory.get(1); //
|
|
77
|
+
let get1 = Memory.get(1); // 'a1'
|
|
56
78
|
let get2 = Memory.get(2); // { b: 2 }
|
|
57
|
-
let get3 = Memory.get(3); // [
|
|
58
|
-
let get4 = Memory.get(4); // [ { a:
|
|
79
|
+
let get3 = Memory.get(3); // [ 2, 3 ]
|
|
80
|
+
let get4 = Memory.get(4); // [ { a: 'a1', b: 'b1' } ]
|
|
59
81
|
```
|
|
60
82
|
|
|
61
83
|
#### get timeout
|
|
62
84
|
```bash
|
|
63
85
|
// for get timeout data realtime
|
|
64
|
-
let ttl1 = Memory.ttl(1); //
|
|
86
|
+
let ttl1 = Memory.ttl(1); // 20000
|
|
65
87
|
```
|
|
66
88
|
|
|
67
89
|
#### multiple
|
|
@@ -73,10 +95,11 @@ let getall = Memory.multiple();
|
|
|
73
95
|
### Output:
|
|
74
96
|
```bash
|
|
75
97
|
[
|
|
76
|
-
{ id:
|
|
77
|
-
{ id:
|
|
78
|
-
{ id:
|
|
79
|
-
{ id:
|
|
98
|
+
{ id: '1', data: 'a1' },
|
|
99
|
+
{ id: '2', data: { b: 2 } },
|
|
100
|
+
{ id: '3', data: [ 2, 3 ] },
|
|
101
|
+
{ id: '4', data: [ { a: 'a1', b: 'b1' } ] }
|
|
102
|
+
{ id: '5', data: 'not set expire' }
|
|
80
103
|
]
|
|
81
104
|
```
|
|
82
105
|
|
|
@@ -102,47 +125,111 @@ const plus = function (a, b) {
|
|
|
102
125
|
return a + b;
|
|
103
126
|
};
|
|
104
127
|
|
|
128
|
+
// register processor
|
|
105
129
|
Memory.process('test', async function () {
|
|
130
|
+
console.log("Running queued task with:");
|
|
106
131
|
console.log(this.data);
|
|
107
|
-
|
|
108
|
-
let
|
|
109
|
-
|
|
110
|
-
let a = get3[0];
|
|
111
|
-
let b = get3[1];
|
|
132
|
+
// do your background work
|
|
133
|
+
let a = this.data.price;
|
|
134
|
+
let b = this.data.value;
|
|
112
135
|
let c = plus(a, b);
|
|
113
|
-
console.log(`
|
|
114
|
-
let getall = Memory.multiple();
|
|
115
|
-
console.log(getall);
|
|
116
|
-
let deleted = Memory.delall();
|
|
117
|
-
return c;
|
|
136
|
+
console.log(`total: ${c}`);
|
|
118
137
|
});
|
|
138
|
+
```
|
|
119
139
|
|
|
140
|
+
|
|
141
|
+
#### Task usage
|
|
142
|
+
```bash
|
|
143
|
+
// import queue to index.js or app.js for side effects
|
|
144
|
+
const queue = require('./queue.service');
|
|
120
145
|
```
|
|
121
146
|
|
|
122
147
|
#### Task usage
|
|
123
148
|
```bash
|
|
124
149
|
// index.js
|
|
125
150
|
import express from 'express';
|
|
126
|
-
|
|
151
|
+
|
|
152
|
+
// import queue for side effects
|
|
153
|
+
const queue = require('./queue.service');
|
|
154
|
+
|
|
155
|
+
// import Memory for in-memory data storage
|
|
127
156
|
const Memory = require('memory-sri');
|
|
128
157
|
|
|
129
158
|
const app = express();
|
|
130
159
|
app.use(express.json());
|
|
131
160
|
|
|
132
|
-
//
|
|
133
|
-
app.get('/', async (req, res) => {
|
|
161
|
+
// Example route to demonstrate Memory usage
|
|
162
|
+
app.get('/memory', async (req, res) => {
|
|
163
|
+
try {
|
|
164
|
+
// set key with TTL (seconds)
|
|
165
|
+
Memory.set(1, 'a1', 20);
|
|
166
|
+
Memory.set(2, { b: 2 }, 30);
|
|
167
|
+
Memory.set(3, [2, 3], 40);
|
|
168
|
+
Memory.set(4, [{ a: 'a1', b: 'b1' }], 50);
|
|
169
|
+
|
|
170
|
+
// set key with no expiry
|
|
171
|
+
Memory.set(5, 'not set expire');
|
|
172
|
+
|
|
173
|
+
// get value
|
|
174
|
+
console.log(Memory.get(5)); // { b: 2 }
|
|
175
|
+
|
|
176
|
+
// check TTL
|
|
177
|
+
console.log(Memory.ttl(5)); // remaining seconds
|
|
178
|
+
|
|
179
|
+
// get all keys
|
|
180
|
+
let getAll = await Memory.multiple();
|
|
181
|
+
|
|
182
|
+
let result = [];
|
|
183
|
+
for (let i = 0; i < getAll.length; i++) {
|
|
184
|
+
let key = i + 1;
|
|
185
|
+
let ttl = Memory.ttl(key);
|
|
186
|
+
result.push({ id: getAll[i].id, data: getAll[i].data, ttl: ttl });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// delete a key
|
|
190
|
+
Memory.del(1);
|
|
191
|
+
|
|
192
|
+
// delete all keys
|
|
193
|
+
Memory.delall();
|
|
194
|
+
|
|
195
|
+
res.json({ data: result });
|
|
196
|
+
} catch (error) {
|
|
197
|
+
console.error('Retrieve Error:', error);
|
|
198
|
+
res.status(500).json({ message: 'Internal Server Error' });
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Example route to demonstrate Memory usage
|
|
203
|
+
app.get('/run', async (req, res) => {
|
|
134
204
|
try {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
205
|
+
// run the task processor
|
|
206
|
+
let testFunction = function (a, b) {
|
|
207
|
+
console.log(`result from task function: ${a + b}`);
|
|
208
|
+
return a + b;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// run task with arguments
|
|
212
|
+
let result = await Memory.runtask(testFunction, [2, 4]);
|
|
213
|
+
|
|
214
|
+
res.json({ id: result.id });
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error('Retrieve Error:', error);
|
|
217
|
+
res.status(500).json({ message: 'Internal Server Error' });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Example route to demonstrate Memory usage
|
|
222
|
+
app.get('/queue', async (req, res) => {
|
|
223
|
+
try {
|
|
224
|
+
let dataQueue = {
|
|
225
|
+
user: 1,
|
|
226
|
+
value: Math.random(),
|
|
227
|
+
price: Math.random() * 100,
|
|
228
|
+
};
|
|
229
|
+
// enqueue a job
|
|
230
|
+
let result = Memory.addtask('test', dataQueue, 3);
|
|
231
|
+
|
|
232
|
+
res.json({ id: result.id, data: result.data });
|
|
146
233
|
} catch (error) {
|
|
147
234
|
console.error('Retrieve Error:', error);
|
|
148
235
|
res.status(500).json({ message: 'Internal Server Error' });
|
|
@@ -154,7 +241,6 @@ const PORT = 3000;
|
|
|
154
241
|
app.listen(PORT, () => {
|
|
155
242
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
156
243
|
});
|
|
157
|
-
|
|
158
244
|
```
|
|
159
245
|
|
|
160
246
|
### Key Features:
|
package/index.d.ts
CHANGED
package/lib/main.js
CHANGED
|
@@ -12,7 +12,7 @@ let memory = module.exports;
|
|
|
12
12
|
/**
|
|
13
13
|
* version
|
|
14
14
|
*/
|
|
15
|
-
memory.version = "1.2.
|
|
15
|
+
memory.version = "1.2.11";
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Memory
|
|
@@ -41,17 +41,17 @@ memory.multiple = function getDataAll() {
|
|
|
41
41
|
memory.set = function setData(identifier, data = [], time = 259200) {
|
|
42
42
|
const MAX_TTL = 604800;
|
|
43
43
|
const limittime = Math.min(time, MAX_TTL);
|
|
44
|
-
return Task.addTaskBE(setTask(identifier, data, limittime), data);
|
|
44
|
+
return Task.addTaskBE(setTask(identifier, data, limittime), data, 0);
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
memory.del = function delData(
|
|
48
48
|
identifier = "a501cf48-9e8a-497e-a158-f87081503425"
|
|
49
49
|
) {
|
|
50
|
-
return Task.addTaskBE(delTask(identifier), identifier);
|
|
50
|
+
return Task.addTaskBE(delTask(identifier), identifier, 0);
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
memory.delall = function delAllData() {
|
|
54
|
-
return Task.addTaskBE(delAllTask(), "Delete All");
|
|
54
|
+
return Task.addTaskBE(delAllTask(), "Delete All", 0);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
/**
|
|
@@ -64,6 +64,15 @@ memory.delall = function delAllData() {
|
|
|
64
64
|
|
|
65
65
|
memory.addtask = Task.addTask;
|
|
66
66
|
memory.process = Task.process;
|
|
67
|
+
memory.runtask = function addTaskWorker(taskFn, args = []) {
|
|
68
|
+
if (Array.isArray(args)) {
|
|
69
|
+
return Task.addTaskBE(() => taskFn(...args), args, 1000);
|
|
70
|
+
} else if (args !== undefined && args !== null) {
|
|
71
|
+
return Task.addTaskBE(() => taskFn(args), args, 1000);
|
|
72
|
+
} else {
|
|
73
|
+
return "No arguments provided for the task";
|
|
74
|
+
}
|
|
75
|
+
};
|
|
67
76
|
|
|
68
77
|
/**
|
|
69
78
|
* private util functions
|
package/lib/worker.js
CHANGED
|
@@ -32,14 +32,16 @@ Task.addTask = function (name = "temp", data = {}, retries = 1) {
|
|
|
32
32
|
return { id: task.id, data: data };
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
Task.addTaskBE = function addTaskService(taskFn, data) {
|
|
35
|
+
Task.addTaskBE = function addTaskService(taskFn, data, timeout) {
|
|
36
36
|
if (typeof taskFn !== "function") {
|
|
37
37
|
logData(`Task must be a function`);
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
if(timeout !== 0){
|
|
41
|
+
taskIdBE += 1;
|
|
42
|
+
}
|
|
41
43
|
tasksBE.push({ id: taskIdBE, run: taskFn });
|
|
42
|
-
processTasks();
|
|
44
|
+
processTasks(timeout);
|
|
43
45
|
return {
|
|
44
46
|
id: taskIdBE,
|
|
45
47
|
data: data,
|
|
@@ -83,8 +85,10 @@ async function runTask(name) {
|
|
|
83
85
|
}
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
async function processTasks() {
|
|
87
|
-
|
|
88
|
+
async function processTasks(timeout) {
|
|
89
|
+
if (timeout !== 0) {
|
|
90
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
91
|
+
}
|
|
88
92
|
if (tasksBE.length === 0) return;
|
|
89
93
|
|
|
90
94
|
let attempts = 0;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "memory-sri",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.11",
|
|
4
|
+
"description": "Powerful yet simple memory and queue manager for building efficient Node.js apps.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npx jest ---coverage=false --detectOpenHandles --forceExit",
|
|
7
7
|
"exit": "echo \"No tests specified\" && exit 0",
|
|
@@ -16,15 +16,16 @@
|
|
|
16
16
|
"lib"
|
|
17
17
|
],
|
|
18
18
|
"keywords": [
|
|
19
|
-
"
|
|
19
|
+
"queue",
|
|
20
|
+
"data-structure",
|
|
20
21
|
"memory",
|
|
21
|
-
"
|
|
22
|
-
"flexible",
|
|
23
|
-
"node",
|
|
24
|
-
"utility",
|
|
25
|
-
"integration"
|
|
22
|
+
"node"
|
|
26
23
|
],
|
|
27
24
|
"main": "./index",
|
|
28
25
|
"types": "./index.d.ts",
|
|
29
|
-
"typings": "./index.d.ts"
|
|
26
|
+
"typings": "./index.d.ts",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/Santipap-Eiamsamlee/memory.git"
|
|
30
|
+
}
|
|
30
31
|
}
|