memory-sri 1.2.8 → 1.3.1

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
@@ -1,6 +1,6 @@
1
1
  # Memory Library
2
2
 
3
- A simple and customizable memory data utility for Node.js.
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
- ## import memory-sri
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('1', "a1", 10);
46
- Memory.set('2', { b: 2 }, 20);
47
- Memory.set('3', [ "a1", "a2" ], 30);
48
- Memory.set('4', [ { a: "a1", b: "b1" } ], 40);
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); // "a1"
77
+ let get1 = Memory.get(1); // 'a1'
56
78
  let get2 = Memory.get(2); // { b: 2 }
57
- let get3 = Memory.get(3); // [ "a1", "a2" ]
58
- let get4 = Memory.get(4); // [ { a: "a1", b: "b1" } ]
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); // 10000
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: "1", data: "a1" },
77
- { id: "2", data: { b: 2 } },
78
- { id: "3", data: [ "a1", "a2" ] },
79
- { id: "4", data: [ { a: "a1", b: "b1" } ] }
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,112 @@ const plus = function (a, b) {
102
125
  return a + b;
103
126
  };
104
127
 
105
- Memory.process('test', async function () {
106
- console.log(this.data);
107
- let get3 = Memory.get('3'); // [ 2, 3 ]
108
- let getttl5 = Memory.ttl('5');
109
- Memory.del('1');
110
- let a = get3[0];
111
- let b = get3[1];
128
+ // register processor
129
+ Memory.process('test', async function (data, meta) {
130
+ console.log("Running queued task with:");
131
+ console.log(data);
132
+ console.log(meta);
133
+ // do your background work
134
+ let a = data.price;
135
+ let b = data.value;
112
136
  let c = plus(a, b);
113
- console.log(`result: ${c}`);
114
- let getall = Memory.multiple();
115
- console.log(getall);
116
- let deleted = Memory.delall();
117
- return c;
137
+ console.log(`total: ${c}`);
118
138
  });
139
+ ```
119
140
 
141
+
142
+ #### Task usage
143
+ ```bash
144
+ // import queue to index.js or app.js for side effects
145
+ const queue = require('./queue.service');
120
146
  ```
121
147
 
122
148
  #### Task usage
123
149
  ```bash
124
150
  // index.js
125
151
  import express from 'express';
126
- require('./queue.service');
152
+
153
+ // import queue for side effects
154
+ const queue = require('./queue.service');
155
+
156
+ // import Memory for in-memory data storage
127
157
  const Memory = require('memory-sri');
128
158
 
129
159
  const app = express();
130
160
  app.use(express.json());
131
161
 
132
- // Retrieve Data from Redis
133
- app.get('/', async (req, res) => {
162
+ // Example route to demonstrate Memory usage
163
+ app.get('/memory', async (req, res) => {
164
+ try {
165
+ // set key with TTL (seconds)
166
+ Memory.set(1, 'a1', 20);
167
+ Memory.set(2, { b: 2 }, 30);
168
+ Memory.set(3, [2, 3], 40);
169
+ Memory.set(4, [{ a: 'a1', b: 'b1' }], 50);
170
+
171
+ // set key with no expiry
172
+ Memory.set(5, 'not set expire');
173
+
174
+ // get value
175
+ console.log(Memory.get(5)); // { b: 2 }
176
+
177
+ // check TTL
178
+ console.log(Memory.ttl(5)); // remaining seconds
179
+
180
+ // get all keys
181
+ let getAll = await Memory.multiple();
182
+
183
+ let result = [];
184
+ for (let i = 0; i < getAll.length; i++) {
185
+ let key = i + 1;
186
+ let ttl = Memory.ttl(key);
187
+ result.push({ id: getAll[i].id, data: getAll[i].data, ttl: ttl });
188
+ }
189
+
190
+ // delete a key
191
+ Memory.del(1);
192
+
193
+ // delete all keys
194
+ Memory.delall();
195
+
196
+ res.json({ data: result });
197
+ } catch (error) {
198
+ console.error('Retrieve Error:', error);
199
+ res.status(500).json({ message: 'Internal Server Error' });
200
+ }
201
+ });
202
+
203
+ // Example route to demonstrate Memory usage
204
+ app.get('/run', async (req, res) => {
134
205
  try {
135
- Memory.set('1', 'a1', 10);
136
- Memory.set('2', { b: 2 }, 20);
137
- Memory.set('3', [2, 3], 30);
138
- Memory.set('4', [{ a: 'a1', b: 'b1' }], 40);
139
- Memory.set('5', 'not set expire');
140
-
141
- // "test" is name of task same process
142
- // { user: 1 } is data send to task
143
- // retry 3 time, maximum 5 time
144
- let result = Memory.addtask('test', { user: 1 }, 3);
145
- res.json({ id: result.id, value: result.data });
206
+ // run the task processor
207
+ let testFunction = function (a, b) {
208
+ console.log(`result from task function: ${a + b}`);
209
+ return a + b;
210
+ };
211
+
212
+ // run task with arguments
213
+ let result = await Memory.runtask(testFunction, [2, 4]);
214
+
215
+ res.json({ id: result.id });
216
+ } catch (error) {
217
+ console.error('Retrieve Error:', error);
218
+ res.status(500).json({ message: 'Internal Server Error' });
219
+ }
220
+ });
221
+
222
+ // Example route to demonstrate Memory usage
223
+ app.get('/queue', async (req, res) => {
224
+ try {
225
+ let dataQueue = {
226
+ user: 1,
227
+ value: Math.random(),
228
+ price: Math.random() * 100,
229
+ };
230
+ // enqueue a job
231
+ let result = Memory.addtask('test', dataQueue, 3);
232
+
233
+ res.json({ id: result.id, data: result.data });
146
234
  } catch (error) {
147
235
  console.error('Retrieve Error:', error);
148
236
  res.status(500).json({ message: 'Internal Server Error' });
@@ -154,7 +242,6 @@ const PORT = 3000;
154
242
  app.listen(PORT, () => {
155
243
  console.log(`Server running on http://localhost:${PORT}`);
156
244
  });
157
-
158
245
  ```
159
246
 
160
247
  ### Key Features:
package/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export function del(identifier: string): any;
6
6
  export function delall(): any;
7
7
  export function process(name?: any, taskFn?: any): any;
8
8
  export function addtask(name?: any, data?: any, retries?: any): any;
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.2.8";
15
+ memory.version = "1.3.1";
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
- taskIdBE += 1;
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,
@@ -65,8 +67,7 @@ async function runTask(name) {
65
67
 
66
68
  try {
67
69
  task.attempts += 1;
68
- const result = await fn.call({
69
- data: task.data,
70
+ const result = await fn(task.data, {
70
71
  id: task.id,
71
72
  name: task.name,
72
73
  });
@@ -83,8 +84,10 @@ async function runTask(name) {
83
84
  }
84
85
  }
85
86
 
86
- async function processTasks() {
87
- await new Promise((resolve) => setTimeout(resolve, 1000));
87
+ async function processTasks(timeout) {
88
+ if (timeout !== 0) {
89
+ await new Promise((resolve) => setTimeout(resolve, timeout));
90
+ }
88
91
  if (tasksBE.length === 0) return;
89
92
 
90
93
  let attempts = 0;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "memory-sri",
3
- "version": "1.2.8",
4
- "description": "A simple and customizable memory data per 1 loadbalance",
3
+ "version": "1.3.1",
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
- "temp",
19
+ "queue",
20
+ "data-structure",
20
21
  "memory",
21
- "minimal",
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
  }