@bejibun/cache 0.1.14 → 0.1.16
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 +57 -1
- package/README.md +5 -1
- package/builders/CacheBuilder.d.ts +4 -1
- package/builders/CacheBuilder.js +61 -27
- package/config/cache.js +3 -0
- package/enums/CacheDriverEnum.d.ts +5 -0
- package/enums/CacheDriverEnum.js +6 -0
- package/enums/index.d.ts +1 -0
- package/enums/index.js +1 -0
- package/facades/Cache.d.ts +2 -0
- package/facades/Cache.js +3 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,10 +3,66 @@ All notable changes to this project will be documented in this file.
|
|
|
3
3
|
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
+
## [v0.1.16](https://github.com/crenata/bejibun-cache/compare/v0.1.15...v0.1.16) - 2025-12-15
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
- Something went wrong when processing cache file with TTL - [#2](https://github.com/crenata/bejibun-cache/issues/2)
|
|
10
|
+
|
|
11
|
+
### 📖 Changes
|
|
12
|
+
|
|
13
|
+
### ❤️Contributors
|
|
14
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
15
|
+
|
|
16
|
+
**Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## [v0.1.15](https://github.com/crenata/bejibun-cache/compare/v0.1.14...v0.1.15) - 2025-12-14
|
|
21
|
+
|
|
22
|
+
### 🩹 Fixes
|
|
23
|
+
|
|
24
|
+
### 📖 Changes
|
|
25
|
+
What's New :
|
|
26
|
+
- Added `connection()` to override cache connection.
|
|
27
|
+
|
|
28
|
+
Makes it more flexible by overriding connections at runtime.
|
|
29
|
+
|
|
30
|
+
- Added `driver` configuration.
|
|
31
|
+
|
|
32
|
+
#### What's its use?
|
|
33
|
+
The cache connection name is no longer static as before.
|
|
34
|
+
|
|
35
|
+
e.g. :
|
|
36
|
+
```text
|
|
37
|
+
connections: {
|
|
38
|
+
local: {
|
|
39
|
+
path: App.Path.storagePath("cache") // absolute path
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
You can now create a connection with any name and specify which driver to use.
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
connections: {
|
|
48
|
+
custom_name: {
|
|
49
|
+
driver: CacheDriverEnum.Local, // "local", "redis"
|
|
50
|
+
path: App.Path.storagePath("custom-cache")
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### ❤️Contributors
|
|
56
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
57
|
+
|
|
58
|
+
**Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
6
62
|
## [v0.1.14](https://github.com/crenata/bejibun-cache/compare/v0.1.12...v0.1.14) - 2025-12-12
|
|
7
63
|
|
|
8
64
|
### 🩹 Fixes
|
|
9
|
-
- Redis connection with Cache own
|
|
65
|
+
- Redis connection with Cache own configuration - [#1](https://github.com/crenata/bejibun-core/issues/1)
|
|
10
66
|
|
|
11
67
|
### 📖 Changes
|
|
12
68
|
What's New :
|
package/README.md
CHANGED
|
@@ -39,17 +39,20 @@ config/cache.ts
|
|
|
39
39
|
|
|
40
40
|
```ts
|
|
41
41
|
import App from "@bejibun/app";
|
|
42
|
+
import CacheDriverEnum from "@bejibun/utils/enums/CacheDriverEnum";
|
|
42
43
|
|
|
43
44
|
const config: Record<string, any> = {
|
|
44
45
|
connection: "local",
|
|
45
46
|
|
|
46
47
|
connections: {
|
|
47
48
|
local: {
|
|
49
|
+
driver: CacheDriverEnum.Local,
|
|
48
50
|
path: App.Path.storagePath("cache") // absolute path
|
|
49
51
|
},
|
|
50
52
|
|
|
51
53
|
redis: {
|
|
52
|
-
|
|
54
|
+
driver: CacheDriverEnum.Redis,
|
|
55
|
+
host: "127.0.0.1",
|
|
53
56
|
port: 6379,
|
|
54
57
|
password: "",
|
|
55
58
|
database: 0
|
|
@@ -68,6 +71,7 @@ How to use tha package.
|
|
|
68
71
|
```ts
|
|
69
72
|
import Cache from "@bejibun/cache";
|
|
70
73
|
|
|
74
|
+
Cache.connection();
|
|
71
75
|
await Cache.remember("key", () => {}, 60 /* seconds */); // any
|
|
72
76
|
await Cache.has("key"); // boolean
|
|
73
77
|
await Cache.get("key"); // any
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
export default class CacheBuilder {
|
|
2
2
|
protected conf: Record<string, any>;
|
|
3
|
+
protected conn?: string;
|
|
3
4
|
protected prefix: string;
|
|
4
5
|
protected redis: Record<string, Function>;
|
|
5
6
|
constructor();
|
|
6
7
|
private get config();
|
|
8
|
+
private get currentConnection();
|
|
9
|
+
private get driver();
|
|
7
10
|
private key;
|
|
8
|
-
private connection;
|
|
9
11
|
private filePath;
|
|
10
12
|
private file;
|
|
11
13
|
private setFile;
|
|
12
14
|
private getFile;
|
|
15
|
+
connection(conn: string): CacheBuilder;
|
|
13
16
|
remember(key: string, callback: Function, ttl?: number): Promise<any>;
|
|
14
17
|
has(key: string): Promise<boolean>;
|
|
15
18
|
get(key: string): Promise<any>;
|
package/builders/CacheBuilder.js
CHANGED
|
@@ -3,12 +3,15 @@ import Logger from "@bejibun/logger";
|
|
|
3
3
|
import Redis from "@bejibun/redis";
|
|
4
4
|
import Luxon from "@bejibun/utils/facades/Luxon";
|
|
5
5
|
import { defineValue, isEmpty, isNotEmpty } from "@bejibun/utils";
|
|
6
|
+
import Enum from "@bejibun/utils/facades/Enum";
|
|
6
7
|
import fs from "fs";
|
|
7
8
|
import path from "path";
|
|
8
9
|
import CacheConfig from "../config/cache";
|
|
9
10
|
import CacheException from "../exceptions/CacheException";
|
|
11
|
+
import CacheDriverEnum from "../enums/CacheDriverEnum";
|
|
10
12
|
export default class CacheBuilder {
|
|
11
13
|
conf;
|
|
14
|
+
conn;
|
|
12
15
|
prefix;
|
|
13
16
|
redis;
|
|
14
17
|
constructor() {
|
|
@@ -38,14 +41,36 @@ export default class CacheBuilder {
|
|
|
38
41
|
throw new CacheException("There is no config provided.");
|
|
39
42
|
return this.conf;
|
|
40
43
|
}
|
|
44
|
+
get currentConnection() {
|
|
45
|
+
return this.config.connections[defineValue(this.conn, this.config.connection)];
|
|
46
|
+
}
|
|
47
|
+
get driver() {
|
|
48
|
+
const driver = defineValue(this.currentConnection?.driver);
|
|
49
|
+
if (isEmpty(driver))
|
|
50
|
+
throw new CacheException(`Missing "driver" on cache config.`);
|
|
51
|
+
if (!Enum.setEnums(CacheDriverEnum).hasValue(driver))
|
|
52
|
+
throw new CacheException(`Not supported "driver" cache.`);
|
|
53
|
+
switch (driver) {
|
|
54
|
+
case "local":
|
|
55
|
+
if (isEmpty(this.currentConnection?.path))
|
|
56
|
+
throw new CacheException(`Missing "path" for "local" cache configuration.`);
|
|
57
|
+
break;
|
|
58
|
+
case "redis":
|
|
59
|
+
if (isEmpty(this.currentConnection?.host))
|
|
60
|
+
throw new CacheException(`Missing "host" for "redis" cache configuration.`);
|
|
61
|
+
if (isEmpty(this.currentConnection?.port))
|
|
62
|
+
throw new CacheException(`Missing "port" for "redis" cache configuration.`);
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
return driver;
|
|
68
|
+
}
|
|
41
69
|
key(key) {
|
|
42
70
|
return `${this.prefix}-${key.replaceAll("/", "-").replaceAll(" ", "-")}`;
|
|
43
71
|
}
|
|
44
|
-
connection() {
|
|
45
|
-
return this.config.connections[this.config.connection];
|
|
46
|
-
}
|
|
47
72
|
filePath(key) {
|
|
48
|
-
return path.resolve(this.
|
|
73
|
+
return path.resolve(this.currentConnection.path, `${this.key(key)}.cache`);
|
|
49
74
|
}
|
|
50
75
|
file(key) {
|
|
51
76
|
return Bun.file(this.filePath(key));
|
|
@@ -54,7 +79,7 @@ export default class CacheBuilder {
|
|
|
54
79
|
ttl = defineValue(ttl, "");
|
|
55
80
|
if (isNotEmpty(ttl))
|
|
56
81
|
ttl = Luxon.DateTime.now().toUnixInteger() + ttl;
|
|
57
|
-
await fs.promises.mkdir(this.
|
|
82
|
+
await fs.promises.mkdir(this.currentConnection.path, { recursive: true });
|
|
58
83
|
return await Bun.write(this.filePath(key), `${ttl}|${data}`);
|
|
59
84
|
}
|
|
60
85
|
async getFile(key) {
|
|
@@ -62,25 +87,34 @@ export default class CacheBuilder {
|
|
|
62
87
|
ttl: null,
|
|
63
88
|
data: null
|
|
64
89
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
try {
|
|
91
|
+
const file = this.file(key);
|
|
92
|
+
if (await file.exists()) {
|
|
93
|
+
const raw = await file.text();
|
|
94
|
+
const [unix, ...rest] = raw.split("|");
|
|
95
|
+
const ttl = Number(unix);
|
|
96
|
+
const data = rest.join("|");
|
|
97
|
+
if (isEmpty(ttl) || Luxon.DateTime.now().toUnixInteger() <= ttl)
|
|
98
|
+
metadata = {
|
|
99
|
+
ttl: defineValue(Number(ttl)),
|
|
100
|
+
data
|
|
101
|
+
};
|
|
102
|
+
else
|
|
103
|
+
await file.delete();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
Logger.setContext("Cache").error("Something went wrong when processing cache file.").trace(error);
|
|
78
108
|
}
|
|
79
109
|
return metadata;
|
|
80
110
|
}
|
|
111
|
+
connection(conn) {
|
|
112
|
+
this.conn = conn;
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
81
115
|
async remember(key, callback, ttl) {
|
|
82
116
|
let data;
|
|
83
|
-
switch (this.
|
|
117
|
+
switch (this.driver) {
|
|
84
118
|
case "local":
|
|
85
119
|
const raw = await this.getFile(key);
|
|
86
120
|
data = raw.data;
|
|
@@ -104,7 +138,7 @@ export default class CacheBuilder {
|
|
|
104
138
|
}
|
|
105
139
|
async has(key) {
|
|
106
140
|
let data;
|
|
107
|
-
switch (this.
|
|
141
|
+
switch (this.driver) {
|
|
108
142
|
case "local":
|
|
109
143
|
const raw = await this.getFile(key);
|
|
110
144
|
data = raw.data;
|
|
@@ -120,7 +154,7 @@ export default class CacheBuilder {
|
|
|
120
154
|
}
|
|
121
155
|
async get(key) {
|
|
122
156
|
let data;
|
|
123
|
-
switch (this.
|
|
157
|
+
switch (this.driver) {
|
|
124
158
|
case "local":
|
|
125
159
|
const raw = await this.getFile(key);
|
|
126
160
|
data = raw.data;
|
|
@@ -138,7 +172,7 @@ export default class CacheBuilder {
|
|
|
138
172
|
let status = true;
|
|
139
173
|
let data;
|
|
140
174
|
try {
|
|
141
|
-
switch (this.
|
|
175
|
+
switch (this.driver) {
|
|
142
176
|
case "local":
|
|
143
177
|
const raw = await this.getFile(key);
|
|
144
178
|
data = raw.data;
|
|
@@ -151,7 +185,7 @@ export default class CacheBuilder {
|
|
|
151
185
|
break;
|
|
152
186
|
}
|
|
153
187
|
if (isEmpty(data)) {
|
|
154
|
-
switch (this.
|
|
188
|
+
switch (this.driver) {
|
|
155
189
|
case "local":
|
|
156
190
|
await this.setFile(key, value, ttl);
|
|
157
191
|
break;
|
|
@@ -176,7 +210,7 @@ export default class CacheBuilder {
|
|
|
176
210
|
async put(key, value, ttl) {
|
|
177
211
|
let status = true;
|
|
178
212
|
try {
|
|
179
|
-
switch (this.
|
|
213
|
+
switch (this.driver) {
|
|
180
214
|
case "local":
|
|
181
215
|
await this.setFile(key, value, ttl);
|
|
182
216
|
break;
|
|
@@ -194,7 +228,7 @@ export default class CacheBuilder {
|
|
|
194
228
|
return status;
|
|
195
229
|
}
|
|
196
230
|
async forget(key) {
|
|
197
|
-
switch (this.
|
|
231
|
+
switch (this.driver) {
|
|
198
232
|
case "local":
|
|
199
233
|
try {
|
|
200
234
|
await this.file(key).delete();
|
|
@@ -212,7 +246,7 @@ export default class CacheBuilder {
|
|
|
212
246
|
}
|
|
213
247
|
async increment(key, ttl) {
|
|
214
248
|
let data;
|
|
215
|
-
switch (this.
|
|
249
|
+
switch (this.driver) {
|
|
216
250
|
case "local":
|
|
217
251
|
const raw = await this.getFile(key);
|
|
218
252
|
data = Number(raw.data);
|
|
@@ -244,7 +278,7 @@ export default class CacheBuilder {
|
|
|
244
278
|
}
|
|
245
279
|
async decrement(key, ttl) {
|
|
246
280
|
let data;
|
|
247
|
-
switch (this.
|
|
281
|
+
switch (this.driver) {
|
|
248
282
|
case "local":
|
|
249
283
|
const raw = await this.getFile(key);
|
|
250
284
|
data = Number(raw.data);
|
package/config/cache.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import App from "@bejibun/app";
|
|
2
|
+
import CacheDriverEnum from "../enums/CacheDriverEnum";
|
|
2
3
|
const config = {
|
|
3
4
|
connection: "local",
|
|
4
5
|
connections: {
|
|
5
6
|
local: {
|
|
7
|
+
driver: CacheDriverEnum.Local,
|
|
6
8
|
path: App.Path.storagePath("cache") // absolute path
|
|
7
9
|
},
|
|
8
10
|
redis: {
|
|
11
|
+
driver: CacheDriverEnum.Redis,
|
|
9
12
|
host: "127.0.0.1",
|
|
10
13
|
port: 6379,
|
|
11
14
|
password: "",
|
package/enums/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../enums/CacheDriverEnum";
|
package/enums/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../enums/CacheDriverEnum";
|
package/facades/Cache.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import CacheBuilder from "../builders/CacheBuilder";
|
|
1
2
|
export default class Cache {
|
|
3
|
+
static connection(connection: string): CacheBuilder;
|
|
2
4
|
static remember(key: string, callback: Function, ttl?: number): Promise<any>;
|
|
3
5
|
static has(key: string): Promise<boolean>;
|
|
4
6
|
static get(key: string): Promise<any>;
|
package/facades/Cache.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import CacheBuilder from "../builders/CacheBuilder";
|
|
2
2
|
export default class Cache {
|
|
3
|
+
static connection(connection) {
|
|
4
|
+
return new CacheBuilder().connection(connection);
|
|
5
|
+
}
|
|
3
6
|
static async remember(key, callback, ttl) {
|
|
4
7
|
return new CacheBuilder().remember(key, callback, ttl);
|
|
5
8
|
}
|
package/index.d.ts
CHANGED
package/index.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.16",
|
|
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.36",
|
|
41
|
-
"@bejibun/utils": "^0.1.
|
|
41
|
+
"@bejibun/utils": "^0.1.28"
|
|
42
42
|
}
|
|
43
43
|
}
|