@bejibun/cache 0.1.11 → 0.1.12
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/CHANGELOG.md +17 -0
- package/builders/CacheBuilder.d.ts +5 -0
- package/builders/CacheBuilder.js +82 -1
- package/config/cache.js +5 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,23 @@ All notable changes to this project will be documented in this file.
|
|
|
3
3
|
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
+
## [v0.1.12](https://github.com/crenata/bejibun-cache/compare/v0.1.11...v0.1.12) - 2025-12-04
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
|
|
10
|
+
### 📖 Changes
|
|
11
|
+
What's New :
|
|
12
|
+
- Adding `local` connection for file schema
|
|
13
|
+
|
|
14
|
+
Now, [@bejibun/cache](https://github.com/crenata/bejibun-cache) has local and redis for cache system.
|
|
15
|
+
|
|
16
|
+
### ❤️Contributors
|
|
17
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
18
|
+
|
|
19
|
+
**Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
6
23
|
## [v0.1.11](https://github.com/crenata/bejibun-cache/compare/v0.1.0...v0.1.11) - 2025-11-23
|
|
7
24
|
|
|
8
25
|
### 🩹 Fixes
|
|
@@ -4,6 +4,11 @@ export default class CacheBuilder {
|
|
|
4
4
|
constructor();
|
|
5
5
|
private get config();
|
|
6
6
|
private key;
|
|
7
|
+
private connection;
|
|
8
|
+
private filePath;
|
|
9
|
+
private file;
|
|
10
|
+
private setFile;
|
|
11
|
+
private getFile;
|
|
7
12
|
remember(key: string, callback: Function, ttl?: number): Promise<any>;
|
|
8
13
|
has(key: string): Promise<boolean>;
|
|
9
14
|
get(key: string): Promise<any>;
|
package/builders/CacheBuilder.js
CHANGED
|
@@ -3,6 +3,7 @@ import Logger from "@bejibun/logger";
|
|
|
3
3
|
import Redis from "@bejibun/redis";
|
|
4
4
|
import { isEmpty, isNotEmpty } from "@bejibun/utils";
|
|
5
5
|
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
6
7
|
import CacheConfig from "../config/cache";
|
|
7
8
|
import CacheException from "../exceptions/CacheException";
|
|
8
9
|
export default class CacheBuilder {
|
|
@@ -24,11 +25,46 @@ export default class CacheBuilder {
|
|
|
24
25
|
return this.conf;
|
|
25
26
|
}
|
|
26
27
|
key(key) {
|
|
27
|
-
|
|
28
|
+
const defaultKey = `${this.prefix}-${key.replaceAll("/", "-").replaceAll(" ", "-")}`;
|
|
29
|
+
return defaultKey;
|
|
30
|
+
/*if (forceDefault) return defaultKey;
|
|
31
|
+
|
|
32
|
+
switch (this.config.connection) {
|
|
33
|
+
case "local":
|
|
34
|
+
return `${Luxon.DateTime.now().toUnixInteger()}-${defaultKey}`;
|
|
35
|
+
default:
|
|
36
|
+
return defaultKey;
|
|
37
|
+
}*/
|
|
38
|
+
}
|
|
39
|
+
connection() {
|
|
40
|
+
return this.config.connections[this.config.connection];
|
|
41
|
+
}
|
|
42
|
+
filePath(key) {
|
|
43
|
+
return path.resolve(this.connection().path, `${this.key(key)}.cache`);
|
|
44
|
+
}
|
|
45
|
+
file(key) {
|
|
46
|
+
return Bun.file(this.filePath(key));
|
|
47
|
+
}
|
|
48
|
+
async setFile(key, data) {
|
|
49
|
+
await fs.promises.mkdir(this.connection().path, { recursive: true });
|
|
50
|
+
return await Bun.write(this.filePath(key), data);
|
|
51
|
+
}
|
|
52
|
+
async getFile(key) {
|
|
53
|
+
const file = this.file(key);
|
|
54
|
+
if (await file.exists())
|
|
55
|
+
return await file.text();
|
|
56
|
+
return null;
|
|
28
57
|
}
|
|
29
58
|
async remember(key, callback, ttl) {
|
|
30
59
|
let data;
|
|
31
60
|
switch (this.config.connection) {
|
|
61
|
+
case "local":
|
|
62
|
+
data = await this.getFile(key);
|
|
63
|
+
if (isEmpty(data)) {
|
|
64
|
+
data = callback();
|
|
65
|
+
await this.setFile(key, data);
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
32
68
|
case "redis":
|
|
33
69
|
data = await Redis.get(this.key(key));
|
|
34
70
|
if (isEmpty(data)) {
|
|
@@ -45,6 +81,9 @@ export default class CacheBuilder {
|
|
|
45
81
|
async has(key) {
|
|
46
82
|
let data;
|
|
47
83
|
switch (this.config.connection) {
|
|
84
|
+
case "local":
|
|
85
|
+
data = await this.getFile(key);
|
|
86
|
+
break;
|
|
48
87
|
case "redis":
|
|
49
88
|
data = await Redis.get(this.key(key));
|
|
50
89
|
break;
|
|
@@ -57,6 +96,9 @@ export default class CacheBuilder {
|
|
|
57
96
|
async get(key) {
|
|
58
97
|
let data;
|
|
59
98
|
switch (this.config.connection) {
|
|
99
|
+
case "local":
|
|
100
|
+
data = await this.getFile(key);
|
|
101
|
+
break;
|
|
60
102
|
case "redis":
|
|
61
103
|
data = await Redis.get(this.key(key));
|
|
62
104
|
break;
|
|
@@ -71,6 +113,9 @@ export default class CacheBuilder {
|
|
|
71
113
|
let data;
|
|
72
114
|
try {
|
|
73
115
|
switch (this.config.connection) {
|
|
116
|
+
case "local":
|
|
117
|
+
data = await this.getFile(key);
|
|
118
|
+
break;
|
|
74
119
|
case "redis":
|
|
75
120
|
data = await Redis.get(this.key(key));
|
|
76
121
|
break;
|
|
@@ -80,6 +125,9 @@ export default class CacheBuilder {
|
|
|
80
125
|
}
|
|
81
126
|
if (isEmpty(data)) {
|
|
82
127
|
switch (this.config.connection) {
|
|
128
|
+
case "local":
|
|
129
|
+
await this.setFile(key, value);
|
|
130
|
+
break;
|
|
83
131
|
case "redis":
|
|
84
132
|
await Redis.set(this.key(key), value, ttl);
|
|
85
133
|
break;
|
|
@@ -102,6 +150,9 @@ export default class CacheBuilder {
|
|
|
102
150
|
let status = true;
|
|
103
151
|
try {
|
|
104
152
|
switch (this.config.connection) {
|
|
153
|
+
case "local":
|
|
154
|
+
await this.setFile(key, value);
|
|
155
|
+
break;
|
|
105
156
|
case "redis":
|
|
106
157
|
await Redis.set(this.key(key), value, ttl);
|
|
107
158
|
break;
|
|
@@ -117,6 +168,14 @@ export default class CacheBuilder {
|
|
|
117
168
|
}
|
|
118
169
|
async forget(key) {
|
|
119
170
|
switch (this.config.connection) {
|
|
171
|
+
case "local":
|
|
172
|
+
try {
|
|
173
|
+
await this.file(key).delete();
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
120
179
|
case "redis":
|
|
121
180
|
await Redis.del(this.key(key));
|
|
122
181
|
break;
|
|
@@ -127,6 +186,17 @@ export default class CacheBuilder {
|
|
|
127
186
|
async increment(key, ttl) {
|
|
128
187
|
let data;
|
|
129
188
|
switch (this.config.connection) {
|
|
189
|
+
case "local":
|
|
190
|
+
data = Number(await this.getFile(key));
|
|
191
|
+
if (isEmpty(data)) {
|
|
192
|
+
data = 1;
|
|
193
|
+
await this.setFile(key, String(data));
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
data++;
|
|
197
|
+
await this.setFile(key, String(data));
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
130
200
|
case "redis":
|
|
131
201
|
data = Number(await Redis.get(this.key(key)));
|
|
132
202
|
if (isEmpty(data)) {
|
|
@@ -147,6 +217,17 @@ export default class CacheBuilder {
|
|
|
147
217
|
async decrement(key, ttl) {
|
|
148
218
|
let data;
|
|
149
219
|
switch (this.config.connection) {
|
|
220
|
+
case "local":
|
|
221
|
+
data = Number(await this.getFile(key));
|
|
222
|
+
if (isEmpty(data)) {
|
|
223
|
+
data = -1;
|
|
224
|
+
await this.setFile(key, String(data));
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
data--;
|
|
228
|
+
await this.setFile(key, String(data));
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
150
231
|
case "redis":
|
|
151
232
|
data = Number(await Redis.get(this.key(key)));
|
|
152
233
|
if (isEmpty(data)) {
|
package/config/cache.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bejibun/cache",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"author": "Havea Crenata <havea.crenata@gmail.com>",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
"@bejibun/app": "^0.1.22",
|
|
39
39
|
"@bejibun/logger": "^0.1.22",
|
|
40
40
|
"@bejibun/redis": "^0.1.34",
|
|
41
|
-
"@bejibun/utils": "^0.1.
|
|
41
|
+
"@bejibun/utils": "^0.1.23"
|
|
42
42
|
}
|
|
43
43
|
}
|