@wild-framework/cache-auto 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wild Framework contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @wild-framework/cache-auto
2
+
3
+ Env-driven cache provider for [Wild Framework](https://github.com/KhandaSoftware/wild-framework).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @wild-framework/cache-auto
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Register `CacheProvider` and set `REDIS_URL` to swap backends without touching app code:
14
+
15
+ ```ts
16
+ import { CacheProvider } from "@wild-framework/cache-auto";
17
+
18
+ CacheProvider.keyPrefix = "app:"; // optional (Redis only)
19
+
20
+ app.register(CacheProvider);
21
+ ```
22
+
23
+ | `REDIS_URL` | Backend |
24
+ | -------------------- | ------------------- |
25
+ | `redis://…` / `rediss://…` | Redis (node-redis) |
26
+ | unset / anything else | In-memory (dev) |
27
+
28
+ ## License
29
+
30
+ MIT
@@ -0,0 +1,10 @@
1
+ /** Which concrete cache backend an env var maps to. */
2
+ export type CacheBackend = "redis" | "memory";
3
+ /**
4
+ * Classify a cache connection URL.
5
+ *
6
+ * redis://… / rediss://… → "redis"
7
+ * anything else/undefined → "memory"
8
+ */
9
+ export declare function classifyCacheUrl(url: string | undefined): CacheBackend;
10
+ //# sourceMappingURL=classify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE9C;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,YAAY,CAItE"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Classify a cache connection URL.
3
+ *
4
+ * redis://… / rediss://… → "redis"
5
+ * anything else/undefined → "memory"
6
+ */
7
+ export function classifyCacheUrl(url) {
8
+ if (!url)
9
+ return "memory";
10
+ if (url.startsWith("redis://") || url.startsWith("rediss://"))
11
+ return "redis";
12
+ return "memory";
13
+ }
14
+ //# sourceMappingURL=classify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify.js","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAuB;IACpD,IAAI,CAAC,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC1B,IAAI,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,WAAW,CAAC;QAAE,OAAO,OAAO,CAAC;IAC9E,OAAO,QAAQ,CAAC;AACpB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { CacheProvider } from "./provider.js";
2
+ export { classifyCacheUrl } from "./classify.js";
3
+ export type { CacheBackend } from "./classify.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { CacheProvider } from "./provider.js";
2
+ export { classifyCacheUrl } from "./classify.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { Provider, Application } from "@wild-framework/core";
2
+ /**
3
+ * Unified, env-driven cache provider.
4
+ *
5
+ * Selects a concrete driver from `REDIS_URL` and binds a `Cache` into the
6
+ * container. This is the "one provider to rule them all" — register it and
7
+ * set `REDIS_URL` to swap backends without touching app code:
8
+ *
9
+ * REDIS_URL=redis://… → RedisCacheDriver
10
+ * (unset) → InMemoryCacheDriver
11
+ *
12
+ * Static configuration (no-arg constructor requirement):
13
+ *
14
+ * ```ts
15
+ * CacheProvider.keyPrefix = "app:"; // optional (Redis only)
16
+ * ```
17
+ *
18
+ * The explicit providers (`CacheProvider` from `@wild-framework/cache` and
19
+ * `RedisCacheProvider` from `@wild-framework/cache-redis`) remain available
20
+ * when you want to be explicit.
21
+ */
22
+ export declare class CacheProvider implements Provider {
23
+ /** Redis connection string. Overrides `process.env.REDIS_URL` when set. */
24
+ static url: string | undefined;
25
+ /** Optional prefix prepended to every key (Redis only). */
26
+ static keyPrefix: string | undefined;
27
+ private driver;
28
+ register(app: Application): void;
29
+ boot(): Promise<void>;
30
+ shutdown(): Promise<void>;
31
+ }
32
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAMlE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,aAAc,YAAW,QAAQ;IAC1C,2EAA2E;IAC3E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,2DAA2D;IAC3D,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAErC,OAAO,CAAC,MAAM,CAA+B;IAE7C,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;IAa1B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAGlC"}
@@ -0,0 +1,50 @@
1
+ import { Container } from "@wild-framework/container";
2
+ import { Cache, InMemoryCacheDriver } from "@wild-framework/cache";
3
+ import { RedisCacheDriver } from "@wild-framework/cache-redis";
4
+ import { classifyCacheUrl } from "./classify.js";
5
+ /**
6
+ * Unified, env-driven cache provider.
7
+ *
8
+ * Selects a concrete driver from `REDIS_URL` and binds a `Cache` into the
9
+ * container. This is the "one provider to rule them all" — register it and
10
+ * set `REDIS_URL` to swap backends without touching app code:
11
+ *
12
+ * REDIS_URL=redis://… → RedisCacheDriver
13
+ * (unset) → InMemoryCacheDriver
14
+ *
15
+ * Static configuration (no-arg constructor requirement):
16
+ *
17
+ * ```ts
18
+ * CacheProvider.keyPrefix = "app:"; // optional (Redis only)
19
+ * ```
20
+ *
21
+ * The explicit providers (`CacheProvider` from `@wild-framework/cache` and
22
+ * `RedisCacheProvider` from `@wild-framework/cache-redis`) remain available
23
+ * when you want to be explicit.
24
+ */
25
+ export class CacheProvider {
26
+ /** Redis connection string. Overrides `process.env.REDIS_URL` when set. */
27
+ static url;
28
+ /** Optional prefix prepended to every key (Redis only). */
29
+ static keyPrefix;
30
+ driver;
31
+ register(app) {
32
+ const container = app.resolve(Container);
33
+ const url = CacheProvider.url ?? process.env.REDIS_URL;
34
+ if (classifyCacheUrl(url) === "redis") {
35
+ const driver = new RedisCacheDriver({ url, keyPrefix: CacheProvider.keyPrefix });
36
+ this.driver = driver;
37
+ container.singleton(Cache, () => new Cache(driver));
38
+ }
39
+ else {
40
+ container.singleton(Cache, () => new Cache(new InMemoryCacheDriver()));
41
+ }
42
+ }
43
+ async boot() {
44
+ await this.driver?.connect();
45
+ }
46
+ async shutdown() {
47
+ await this.driver?.close();
48
+ }
49
+ }
50
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,aAAa;IACtB,2EAA2E;IAC3E,MAAM,CAAC,GAAG,CAAqB;IAC/B,2DAA2D;IAC3D,MAAM,CAAC,SAAS,CAAqB;IAE7B,MAAM,CAA+B;IAE7C,QAAQ,CAAC,GAAgB;QACrB,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACzC,MAAM,GAAG,GAAG,aAAa,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC;QAEvD,IAAI,gBAAgB,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAC;YACpC,MAAM,MAAM,GAAG,IAAI,gBAAgB,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,aAAa,CAAC,SAAS,EAAE,CAAC,CAAC;YACjF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;YACrB,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;QACxD,CAAC;aAAM,CAAC;YACJ,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,IAAI,mBAAmB,EAAE,CAAC,CAAC,CAAC;QAC3E,CAAC;IACL,CAAC;IAED,KAAK,CAAC,IAAI;QACN,MAAM,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,QAAQ;QACV,MAAM,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IAC/B,CAAC;CACJ"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@wild-framework/cache-auto",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Sphah",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/KhandaSoftware/wild-framework.git"
24
+ },
25
+ "description": "Env-driven cache provider — auto-selects Redis or in-memory from REDIS_URL.",
26
+ "keywords": [
27
+ "wild",
28
+ "framework",
29
+ "cache",
30
+ "redis",
31
+ "ttl",
32
+ "auto"
33
+ ],
34
+ "dependencies": {
35
+ "@wild-framework/container": "0.2.0",
36
+ "@wild-framework/core": "0.2.0",
37
+ "@wild-framework/cache-redis": "0.2.0",
38
+ "@wild-framework/cache": "0.2.0"
39
+ },
40
+ "scripts": {
41
+ "build": "tsc",
42
+ "clean": "rm -rf dist",
43
+ "test": "vitest run"
44
+ }
45
+ }