memory-sri 1.2.2 → 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 +160 -29
- package/index.d.ts +1 -0
- package/lib/main.js +15 -6
- package/lib/worker.js +68 -29
- 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
|
|
@@ -41,74 +63,183 @@ Memory.set('5', "not set expire");
|
|
|
41
63
|
|
|
42
64
|
#### set
|
|
43
65
|
```bash
|
|
44
|
-
|
|
45
|
-
Memory.set('
|
|
46
|
-
Memory.set(
|
|
47
|
-
Memory.set(
|
|
66
|
+
// for set it take job to stored data
|
|
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);
|
|
48
71
|
Memory.set('5', "not set expire");
|
|
49
72
|
```
|
|
50
73
|
|
|
51
74
|
#### get
|
|
52
75
|
```bash
|
|
53
|
-
|
|
54
|
-
let
|
|
55
|
-
let
|
|
56
|
-
let
|
|
76
|
+
// for get data realtime
|
|
77
|
+
let get1 = Memory.get(1); // 'a1'
|
|
78
|
+
let get2 = Memory.get(2); // { b: 2 }
|
|
79
|
+
let get3 = Memory.get(3); // [ 2, 3 ]
|
|
80
|
+
let get4 = Memory.get(4); // [ { a: 'a1', b: 'b1' } ]
|
|
57
81
|
```
|
|
58
82
|
|
|
59
83
|
#### get timeout
|
|
60
84
|
```bash
|
|
61
|
-
|
|
85
|
+
// for get timeout data realtime
|
|
86
|
+
let ttl1 = Memory.ttl(1); // 20000
|
|
62
87
|
```
|
|
63
88
|
|
|
64
89
|
#### multiple
|
|
65
90
|
```bash
|
|
66
|
-
|
|
91
|
+
// for get data all realtime
|
|
92
|
+
let getall = Memory.multiple();
|
|
67
93
|
```
|
|
68
94
|
|
|
69
95
|
### Output:
|
|
70
96
|
```bash
|
|
71
97
|
[
|
|
72
|
-
{ id:
|
|
73
|
-
{ id:
|
|
74
|
-
{ id:
|
|
75
|
-
{ 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' }
|
|
76
103
|
]
|
|
77
104
|
```
|
|
78
105
|
|
|
79
106
|
#### delete
|
|
80
107
|
```bash
|
|
108
|
+
// for delete it take job to delete data from stored
|
|
81
109
|
Memory.del(1);
|
|
82
110
|
```
|
|
83
111
|
|
|
84
112
|
#### delete all
|
|
85
113
|
```bash
|
|
114
|
+
// for delete all it take job to delete all data from stored
|
|
86
115
|
Memory.delall();
|
|
87
116
|
```
|
|
88
117
|
|
|
89
118
|
### Task
|
|
90
|
-
####
|
|
119
|
+
#### Main page for process
|
|
91
120
|
```bash
|
|
92
|
-
|
|
121
|
+
// queue.service.js
|
|
122
|
+
const Memory = require('memory-sri');
|
|
123
|
+
|
|
124
|
+
const plus = function (a, b) {
|
|
93
125
|
return a + b;
|
|
94
|
-
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// register processor
|
|
129
|
+
Memory.process('test', async function () {
|
|
130
|
+
console.log("Running queued task with:");
|
|
131
|
+
console.log(this.data);
|
|
132
|
+
// do your background work
|
|
133
|
+
let a = this.data.price;
|
|
134
|
+
let b = this.data.value;
|
|
135
|
+
let c = plus(a, b);
|
|
136
|
+
console.log(`total: ${c}`);
|
|
137
|
+
});
|
|
95
138
|
```
|
|
96
139
|
|
|
140
|
+
|
|
97
141
|
#### Task usage
|
|
98
142
|
```bash
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
// retry 3 time, maximum 5 time
|
|
102
|
-
Task.addtask("test", { user: 1 }, 3);
|
|
143
|
+
// import queue to index.js or app.js for side effects
|
|
144
|
+
const queue = require('./queue.service');
|
|
103
145
|
```
|
|
104
146
|
|
|
147
|
+
#### Task usage
|
|
105
148
|
```bash
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
149
|
+
// index.js
|
|
150
|
+
import express from 'express';
|
|
151
|
+
|
|
152
|
+
// import queue for side effects
|
|
153
|
+
const queue = require('./queue.service');
|
|
154
|
+
|
|
155
|
+
// import Memory for in-memory data storage
|
|
156
|
+
const Memory = require('memory-sri');
|
|
157
|
+
|
|
158
|
+
const app = express();
|
|
159
|
+
app.use(express.json());
|
|
160
|
+
|
|
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) => {
|
|
204
|
+
try {
|
|
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 });
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.error('Retrieve Error:', error);
|
|
235
|
+
res.status(500).json({ message: 'Internal Server Error' });
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Start Server
|
|
240
|
+
const PORT = 3000;
|
|
241
|
+
app.listen(PORT, () => {
|
|
242
|
+
console.log(`Server running on http://localhost:${PORT}`);
|
|
112
243
|
});
|
|
113
244
|
```
|
|
114
245
|
|
package/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export function multiple(): any;
|
|
|
4
4
|
export function set(identifier: string, data?: any, time?: number): any;
|
|
5
5
|
export function del(identifier: string): any;
|
|
6
6
|
export function delall(): any;
|
|
7
|
+
export function process(name?: any, taskFn?: any): any;
|
|
7
8
|
export function addtask(name?: any, data?: any, retries?: any): any;
|
|
8
9
|
export function runtask(name?: any, taskFn?: any): any;
|
package/lib/main.js
CHANGED
|
@@ -12,7 +12,7 @@ let memory = module.exports;
|
|
|
12
12
|
/**
|
|
13
13
|
* version
|
|
14
14
|
*/
|
|
15
|
-
memory.version = "1.
|
|
15
|
+
memory.version = "1.2.11";
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Memory
|
|
@@ -41,21 +41,21 @@ 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
|
|
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
|
|
50
|
+
return Task.addTaskBE(delTask(identifier), identifier, 0);
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
memory.delall = function delAllData() {
|
|
54
|
-
return
|
|
54
|
+
return Task.addTaskBE(delAllTask(), "Delete All", 0);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Task
|
|
59
59
|
*
|
|
60
60
|
* @param {Fuction} taskFn
|
|
61
61
|
* @param {Object} [args]
|
|
@@ -63,7 +63,16 @@ memory.delall = function delAllData() {
|
|
|
63
63
|
*/
|
|
64
64
|
|
|
65
65
|
memory.addtask = Task.addTask;
|
|
66
|
-
memory.
|
|
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
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
*/
|
|
4
4
|
let Task = module.exports;
|
|
5
5
|
let tasks = [];
|
|
6
|
-
let isProcessing = false;
|
|
7
6
|
let taskId = 0;
|
|
8
7
|
let registeredFns = {};
|
|
8
|
+
let tasksBE = [];
|
|
9
|
+
let taskIdBE = 0;
|
|
10
|
+
/**
|
|
11
|
+
* Processor for a task name
|
|
12
|
+
*/
|
|
13
|
+
Task.process = function (name, fn) {
|
|
14
|
+
registeredFns[name] = fn;
|
|
15
|
+
};
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
18
|
* Add a job into the queue (before the processor is defined)
|
|
@@ -13,7 +20,6 @@ let registeredFns = {};
|
|
|
13
20
|
Task.addTask = function (name = "temp", data = {}, retries = 1) {
|
|
14
21
|
taskId += 1;
|
|
15
22
|
retries = !retries ? 1 : retries > 5 ? 5 : retries;
|
|
16
|
-
|
|
17
23
|
const task = {
|
|
18
24
|
id: taskId,
|
|
19
25
|
name,
|
|
@@ -21,50 +27,83 @@ Task.addTask = function (name = "temp", data = {}, retries = 1) {
|
|
|
21
27
|
attempts: 0,
|
|
22
28
|
maxAttempts: retries,
|
|
23
29
|
};
|
|
24
|
-
|
|
25
30
|
tasks.push(task);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return { id: task.id };
|
|
31
|
+
runTask(name);
|
|
32
|
+
return { id: task.id, data: data };
|
|
29
33
|
};
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
* Register a processor for a task name
|
|
33
|
-
*/
|
|
34
|
-
Task.runTask = function (name, taskFn) {
|
|
35
|
+
Task.addTaskBE = function addTaskService(taskFn, data, timeout) {
|
|
35
36
|
if (typeof taskFn !== "function") {
|
|
36
|
-
|
|
37
|
+
logData(`Task must be a function`);
|
|
38
|
+
return null;
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
if(timeout !== 0){
|
|
41
|
+
taskIdBE += 1;
|
|
42
|
+
}
|
|
43
|
+
tasksBE.push({ id: taskIdBE, run: taskFn });
|
|
44
|
+
processTasks(timeout);
|
|
45
|
+
return {
|
|
46
|
+
id: taskIdBE,
|
|
47
|
+
data: data,
|
|
48
|
+
};
|
|
40
49
|
};
|
|
41
50
|
|
|
42
51
|
/**
|
|
43
52
|
* private util functions
|
|
44
53
|
*/
|
|
45
|
-
async function
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
async function runTask(name) {
|
|
55
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
56
|
+
const index = tasks.findIndex((t) => t.name === name);
|
|
57
|
+
/**
|
|
58
|
+
* no tasks found
|
|
59
|
+
*/
|
|
60
|
+
if (index === -1) return null;
|
|
61
|
+
/**
|
|
62
|
+
* remove from queue
|
|
63
|
+
*/
|
|
64
|
+
const task = tasks.splice(index, 1)[0];
|
|
65
|
+
const fn = registeredFns[task.name];
|
|
66
|
+
if (!fn) throw new Error(`No processor registered for task "${task.name}"`);
|
|
48
67
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
68
|
+
try {
|
|
69
|
+
task.attempts += 1;
|
|
70
|
+
const result = await fn.call({
|
|
71
|
+
data: task.data,
|
|
72
|
+
id: task.id,
|
|
73
|
+
name: task.name,
|
|
74
|
+
});
|
|
52
75
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
return result;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (task.attempts < task.maxAttempts) {
|
|
79
|
+
/**
|
|
80
|
+
* requeue
|
|
81
|
+
*/
|
|
82
|
+
tasks.push(task);
|
|
57
83
|
}
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
58
87
|
|
|
88
|
+
async function processTasks(timeout) {
|
|
89
|
+
if (timeout !== 0) {
|
|
90
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
91
|
+
}
|
|
92
|
+
if (tasksBE.length === 0) return;
|
|
93
|
+
|
|
94
|
+
let attempts = 0;
|
|
95
|
+
const currentTasks = tasksBE.splice(0, tasksBE.length);
|
|
96
|
+
for (const taskObj of currentTasks) {
|
|
59
97
|
try {
|
|
60
|
-
|
|
61
|
-
await
|
|
98
|
+
attempts += 1;
|
|
99
|
+
await taskObj.run();
|
|
62
100
|
} catch (err) {
|
|
63
|
-
if (
|
|
64
|
-
|
|
101
|
+
if (attempts < 3) {
|
|
102
|
+
/**
|
|
103
|
+
* requeue
|
|
104
|
+
*/
|
|
105
|
+
await taskObj.run();
|
|
65
106
|
}
|
|
66
107
|
}
|
|
67
108
|
}
|
|
68
|
-
|
|
69
|
-
isProcessing = false;
|
|
70
109
|
}
|
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
|
}
|