@bejibun/cache 0.1.14 → 0.1.15

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 CHANGED
@@ -3,10 +3,52 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  ---
5
5
 
6
+ ## [v0.1.15](https://github.com/crenata/bejibun-cache/compare/v0.1.14...v0.1.15) - 2025-12-14
7
+
8
+ ### 🩹 Fixes
9
+
10
+ ### 📖 Changes
11
+ What's New :
12
+ - Added `connection()` to override cache connection.
13
+
14
+ Makes it more flexible by overriding connections at runtime.
15
+
16
+ - Added `driver` configuration.
17
+
18
+ #### What's its use?
19
+ The cache connection name is no longer static as before.
20
+
21
+ e.g. :
22
+ ```text
23
+ connections: {
24
+ local: {
25
+ path: App.Path.storagePath("cache") // absolute path
26
+ }
27
+ }
28
+ ```
29
+
30
+ You can now create a connection with any name and specify which driver to use.
31
+
32
+ ```text
33
+ connections: {
34
+ custom_name: {
35
+ driver: CacheDriverEnum.Local, // "local", "redis"
36
+ path: App.Path.storagePath("custom-cache")
37
+ }
38
+ }
39
+ ```
40
+
41
+ ### ❤️Contributors
42
+ - Havea Crenata ([@crenata](https://github.com/crenata))
43
+
44
+ **Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
45
+
46
+ ---
47
+
6
48
  ## [v0.1.14](https://github.com/crenata/bejibun-cache/compare/v0.1.12...v0.1.14) - 2025-12-12
7
49
 
8
50
  ### 🩹 Fixes
9
- - Redis connection with Cache own settings - [#1](https://github.com/crenata/bejibun-core/issues/1)
51
+ - Redis connection with Cache own configuration - [#1](https://github.com/crenata/bejibun-core/issues/1)
10
52
 
11
53
  ### 📖 Changes
12
54
  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
- host: "127.0.0.100",
54
+ driver: CacheDriverEnum.Redis,
55
+ host: "127.0.0.1",
53
56
  port: 6379,
54
57
  password: "",
55
58
  database: 0
@@ -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>;
@@ -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.connection().path, `${this.key(key)}.cache`);
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.connection().path, { recursive: true });
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) {
@@ -78,9 +103,13 @@ export default class CacheBuilder {
78
103
  }
79
104
  return metadata;
80
105
  }
106
+ connection(conn) {
107
+ this.conn = conn;
108
+ return this;
109
+ }
81
110
  async remember(key, callback, ttl) {
82
111
  let data;
83
- switch (this.config.connection) {
112
+ switch (this.driver) {
84
113
  case "local":
85
114
  const raw = await this.getFile(key);
86
115
  data = raw.data;
@@ -104,7 +133,7 @@ export default class CacheBuilder {
104
133
  }
105
134
  async has(key) {
106
135
  let data;
107
- switch (this.config.connection) {
136
+ switch (this.driver) {
108
137
  case "local":
109
138
  const raw = await this.getFile(key);
110
139
  data = raw.data;
@@ -120,7 +149,7 @@ export default class CacheBuilder {
120
149
  }
121
150
  async get(key) {
122
151
  let data;
123
- switch (this.config.connection) {
152
+ switch (this.driver) {
124
153
  case "local":
125
154
  const raw = await this.getFile(key);
126
155
  data = raw.data;
@@ -138,7 +167,7 @@ export default class CacheBuilder {
138
167
  let status = true;
139
168
  let data;
140
169
  try {
141
- switch (this.config.connection) {
170
+ switch (this.driver) {
142
171
  case "local":
143
172
  const raw = await this.getFile(key);
144
173
  data = raw.data;
@@ -151,7 +180,7 @@ export default class CacheBuilder {
151
180
  break;
152
181
  }
153
182
  if (isEmpty(data)) {
154
- switch (this.config.connection) {
183
+ switch (this.driver) {
155
184
  case "local":
156
185
  await this.setFile(key, value, ttl);
157
186
  break;
@@ -176,7 +205,7 @@ export default class CacheBuilder {
176
205
  async put(key, value, ttl) {
177
206
  let status = true;
178
207
  try {
179
- switch (this.config.connection) {
208
+ switch (this.driver) {
180
209
  case "local":
181
210
  await this.setFile(key, value, ttl);
182
211
  break;
@@ -194,7 +223,7 @@ export default class CacheBuilder {
194
223
  return status;
195
224
  }
196
225
  async forget(key) {
197
- switch (this.config.connection) {
226
+ switch (this.driver) {
198
227
  case "local":
199
228
  try {
200
229
  await this.file(key).delete();
@@ -212,7 +241,7 @@ export default class CacheBuilder {
212
241
  }
213
242
  async increment(key, ttl) {
214
243
  let data;
215
- switch (this.config.connection) {
244
+ switch (this.driver) {
216
245
  case "local":
217
246
  const raw = await this.getFile(key);
218
247
  data = Number(raw.data);
@@ -244,7 +273,7 @@ export default class CacheBuilder {
244
273
  }
245
274
  async decrement(key, ttl) {
246
275
  let data;
247
- switch (this.config.connection) {
276
+ switch (this.driver) {
248
277
  case "local":
249
278
  const raw = await this.getFile(key);
250
279
  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: "",
@@ -0,0 +1,5 @@
1
+ declare enum CacheDriverEnum {
2
+ Local = "local",
3
+ Redis = "redis"
4
+ }
5
+ export default CacheDriverEnum;
@@ -0,0 +1,6 @@
1
+ var CacheDriverEnum;
2
+ (function (CacheDriverEnum) {
3
+ CacheDriverEnum["Local"] = "local";
4
+ CacheDriverEnum["Redis"] = "redis";
5
+ })(CacheDriverEnum || (CacheDriverEnum = {}));
6
+ export default CacheDriverEnum;
@@ -0,0 +1 @@
1
+ export * from "../enums/CacheDriverEnum";
package/enums/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "../enums/CacheDriverEnum";
@@ -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
@@ -1,2 +1,3 @@
1
1
  export { default } from "./facades/Cache";
2
+ export * from "./enums/index";
2
3
  export * from "./facades/index";
package/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { default } from "./facades/Cache";
2
+ export * from "./enums/index";
2
3
  export * from "./facades/index";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bejibun/cache",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
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.27"
41
+ "@bejibun/utils": "^0.1.28"
42
42
  }
43
43
  }