@bejibun/cache 0.1.1 → 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 +32 -0
- package/builders/CacheBuilder.d.ts +7 -2
- package/builders/CacheBuilder.js +88 -7
- package/config/cache.js +5 -1
- package/facades/Cache.d.ts +4 -4
- package/facades/Cache.js +4 -4
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,38 @@ 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
|
+
|
|
23
|
+
## [v0.1.11](https://github.com/crenata/bejibun-cache/compare/v0.1.0...v0.1.11) - 2025-11-23
|
|
24
|
+
|
|
25
|
+
### 🩹 Fixes
|
|
26
|
+
|
|
27
|
+
### 📖 Changes
|
|
28
|
+
What's New :
|
|
29
|
+
- Adding `ttl` supports for increment & decrement
|
|
30
|
+
|
|
31
|
+
### ❤️Contributors
|
|
32
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
33
|
+
|
|
34
|
+
**Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
6
38
|
## [v0.1.1](https://github.com/crenata/bejibun-cache/compare/v0.1.0...v0.1.1) - 2025-11-23
|
|
7
39
|
|
|
8
40
|
### 🩹 Fixes
|
|
@@ -4,12 +4,17 @@ 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>;
|
|
10
15
|
add(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
11
16
|
put(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
12
17
|
forget(key: string): Promise<void>;
|
|
13
|
-
increment(key: string): Promise<number>;
|
|
14
|
-
decrement(key: string): Promise<number>;
|
|
18
|
+
increment(key: string, ttl?: number): Promise<number>;
|
|
19
|
+
decrement(key: string, ttl?: number): Promise<number>;
|
|
15
20
|
}
|
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;
|
|
@@ -124,18 +183,29 @@ export default class CacheBuilder {
|
|
|
124
183
|
break;
|
|
125
184
|
}
|
|
126
185
|
}
|
|
127
|
-
async increment(key) {
|
|
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)) {
|
|
133
203
|
data = 1;
|
|
134
|
-
await Redis.set(this.key(key), data);
|
|
204
|
+
await Redis.set(this.key(key), data, ttl);
|
|
135
205
|
}
|
|
136
206
|
else {
|
|
137
207
|
data++;
|
|
138
|
-
await Redis.set(this.key(key), data);
|
|
208
|
+
await Redis.set(this.key(key), data, ttl);
|
|
139
209
|
}
|
|
140
210
|
break;
|
|
141
211
|
default:
|
|
@@ -144,18 +214,29 @@ export default class CacheBuilder {
|
|
|
144
214
|
}
|
|
145
215
|
return data;
|
|
146
216
|
}
|
|
147
|
-
async decrement(key) {
|
|
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)) {
|
|
153
234
|
data = -1;
|
|
154
|
-
await Redis.set(this.key(key), data);
|
|
235
|
+
await Redis.set(this.key(key), data, ttl);
|
|
155
236
|
}
|
|
156
237
|
else {
|
|
157
238
|
data--;
|
|
158
|
-
await Redis.set(this.key(key), data);
|
|
239
|
+
await Redis.set(this.key(key), data, ttl);
|
|
159
240
|
}
|
|
160
241
|
break;
|
|
161
242
|
default:
|
package/config/cache.js
CHANGED
package/facades/Cache.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ export default class Cache {
|
|
|
2
2
|
static remember(key: string, callback: Function, ttl?: number): Promise<any>;
|
|
3
3
|
static has(key: string): Promise<boolean>;
|
|
4
4
|
static get(key: string): Promise<any>;
|
|
5
|
-
static add(key: string, value:
|
|
6
|
-
static put(key: string, value:
|
|
5
|
+
static add(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
6
|
+
static put(key: string, value: any, ttl?: number): Promise<boolean>;
|
|
7
7
|
static forget(key: string): Promise<void>;
|
|
8
|
-
static increment(key: string): Promise<number>;
|
|
9
|
-
static decrement(key: string): Promise<number>;
|
|
8
|
+
static increment(key: string, ttl?: number): Promise<number>;
|
|
9
|
+
static decrement(key: string, ttl?: number): Promise<number>;
|
|
10
10
|
}
|
package/facades/Cache.js
CHANGED
|
@@ -18,10 +18,10 @@ export default class Cache {
|
|
|
18
18
|
static async forget(key) {
|
|
19
19
|
return new CacheBuilder().forget(key);
|
|
20
20
|
}
|
|
21
|
-
static async increment(key) {
|
|
22
|
-
return new CacheBuilder().increment(key);
|
|
21
|
+
static async increment(key, ttl) {
|
|
22
|
+
return new CacheBuilder().increment(key, ttl);
|
|
23
23
|
}
|
|
24
|
-
static async decrement(key) {
|
|
25
|
-
return new CacheBuilder().decrement(key);
|
|
24
|
+
static async decrement(key, ttl) {
|
|
25
|
+
return new CacheBuilder().decrement(key, ttl);
|
|
26
26
|
}
|
|
27
27
|
}
|
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",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@bejibun/app": "^0.1.22",
|
|
39
39
|
"@bejibun/logger": "^0.1.22",
|
|
40
|
-
"@bejibun/redis": "^0.1.
|
|
41
|
-
"@bejibun/utils": "^0.1.
|
|
40
|
+
"@bejibun/redis": "^0.1.34",
|
|
41
|
+
"@bejibun/utils": "^0.1.23"
|
|
42
42
|
}
|
|
43
43
|
}
|