@nowarajs/elysia-cache 1.2.0 โ†’ 1.2.2

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
@@ -1,24 +1,19 @@
1
1
 
2
- ## v1.2.0
2
+ ## v1.2.2
3
3
 
4
- [compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.1.0...v1.2.0)
4
+ [compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.2.1...v1.2.2)
5
5
 
6
- ### ๐Ÿš€ Enhancements
6
+ ### ๐Ÿงน Refactors
7
7
 
8
- - **๐Ÿš€:** [Add cache exports to index files] ([48995cd](https://github.com/NowaraJS/elysia-cache/commit/48995cd))
9
-
10
- ### ๐Ÿ”ง Fixes
11
-
12
- - **๐Ÿ”ง:** [Simplify cache response handling] ([86a502e](https://github.com/NowaraJS/elysia-cache/commit/86a502e))
8
+ - **๐Ÿงน:** [Refactor caching mechanism for improved readability] ## Refactoring - Simplified state management for cache routes. - Removed unnecessary variables and streamlined cache retrieval logic. - Enhanced clarity in cache handling by restructuring code flow. ([04deea2](https://github.com/NowaraJS/elysia-cache/commit/04deea2))
13
9
 
14
10
  ### ๐Ÿ“– Documentation
15
11
 
16
- - **๐Ÿ“–:** [Update CHANGELOG for version 1.1.0 release] ## Documentation Changes - Removed outdated entries from the CHANGELOG.md for version 1.1.0. - Cleared previous enhancements, documentation updates, types, chores, tests, and CI changes to reflect a clean slate for future updates. ([cdcbffe](https://github.com/NowaraJS/elysia-cache/commit/cdcbffe))
12
+ - **๐Ÿ“–:** [Remove outdated v1.2.1 section from CHANGELOG] ([e903bcf](https://github.com/NowaraJS/elysia-cache/commit/e903bcf))
17
13
 
18
- ### ๐Ÿ“ฆ Build
14
+ ### ๐ŸŒŠ Types
19
15
 
20
- - **๐Ÿ“ฆ:** [Remove enums entrypoint from build configuration] ## Refactoring - Removed the entrypoint for enums from the Bun build configuration. ([ebd84b6](https://github.com/NowaraJS/elysia-cache/commit/ebd84b6))
21
- - **๐Ÿ“ฆ:** [Remove enums entrypoint from package exports] ## Build Changes - Removed the "./enums" entrypoint from the package exports. ([efb87e5](https://github.com/NowaraJS/elysia-cache/commit/efb87e5))
16
+ - **๐ŸŒŠ:** [Add CacheItem interface for caching mechanism] ([a7b46be](https://github.com/NowaraJS/elysia-cache/commit/a7b46be))
22
17
 
23
18
  ### โค๏ธ Contributors
24
19
 
package/dist/cache.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { Elysia } from 'elysia';
2
2
  import { MemoryStore } from '@nowarajs/kv-store/memory';
3
3
  import type { CacheOptions } from './types/cacheOptions';
4
- export declare const cache: ({ defaultTtl, prefix, storage }?: CacheOptions) => Elysia<"", {
4
+ export declare const cache: ({ defaultTtl, prefix, store }?: CacheOptions) => Elysia<"", {
5
5
  decorator: {};
6
6
  store: {
7
7
  kvStore: import("@nowarajs/kv-store/types").KvStore | MemoryStore;
8
- _cacheKey: string;
8
+ _cachedRoutes: Set<string>;
9
9
  };
10
10
  derive: {};
11
11
  resolve: {};
@@ -20,7 +20,7 @@ export declare const cache: ({ defaultTtl, prefix, storage }?: CacheOptions) =>
20
20
  }>;
21
21
  macroFn: {
22
22
  readonly isCached: (enable: boolean | number) => {
23
- afterHandle({ set, response, store }: {
23
+ afterHandle({ set, response, store, request }: {
24
24
  body: unknown;
25
25
  query: Record<string, string>;
26
26
  params: Record<string, string>;
@@ -39,7 +39,7 @@ export declare const cache: ({ defaultTtl, prefix, storage }?: CacheOptions) =>
39
39
  request: Request;
40
40
  store: {
41
41
  kvStore: import("@nowarajs/kv-store/types").KvStore | MemoryStore;
42
- _cacheKey: string;
42
+ _cachedRoutes: Set<string>;
43
43
  };
44
44
  status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 301 | 302 | 303 | 307 | 308 | 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 300 | 304 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 ? {
45
45
  readonly 100: "Continue";
package/dist/index.js CHANGED
@@ -9,30 +9,50 @@ import { MemoryStore } from "@nowarajs/kv-store/memory";
9
9
  var cache = ({
10
10
  defaultTtl = 60,
11
11
  prefix = "",
12
- storage = ":memory:"
13
- } = {}) => new Elysia().state("kvStore", storage === ":memory:" ? new MemoryStore : storage).state("_cacheKey", "").onRequest(async ({ request, store, set }) => {
14
- const cacheKey = await generateCacheKey(request.clone());
15
- const cachedResponse = await store.kvStore.get(`${prefix}${cacheKey}`);
16
- store._cacheKey = cacheKey;
17
- if (cachedResponse) {
18
- set.headers["Cache-Control"] = `max-age=${defaultTtl}, public`;
19
- set.headers["X-Cache"] = "HIT";
20
- set.headers["ETag"] = `"${prefix}${cacheKey}"`;
21
- return cachedResponse;
12
+ store = ":memory:"
13
+ } = {}) => new Elysia().state({
14
+ kvStore: store === ":memory:" ? new MemoryStore : store
15
+ }).state({
16
+ _cachedRoutes: new Set
17
+ }).onRequest(async ({ request, store: store2, set }) => {
18
+ const sanitizeUrl = new URL(request.url).pathname;
19
+ if (store2._cachedRoutes.has(`${request.method}:${sanitizeUrl}`)) {
20
+ const cacheKey = await generateCacheKey(request.clone());
21
+ const cachedData = await store2.kvStore.get(`${prefix}${cacheKey}`);
22
+ if (cachedData && typeof cachedData === "object" && "response" in cachedData && "metadata" in cachedData) {
23
+ const { response, metadata } = cachedData;
24
+ set.headers["cache-control"] = `max-age=${defaultTtl}, public`;
25
+ set.headers["x-cache"] = "HIT";
26
+ set.headers["etag"] = `"${prefix}${cacheKey}"`;
27
+ set.headers["expires"] = new Date(Date.now() + defaultTtl * 1000).toUTCString();
28
+ set.headers["last-modified"] = metadata.createdAt;
29
+ return response;
30
+ }
31
+ set.headers["x-cache"] = "MISS";
22
32
  }
23
33
  return;
24
34
  }).macro({
25
35
  isCached: (enable) => {
26
36
  const ttl = typeof enable === "number" ? enable : enable ? defaultTtl : 0;
27
37
  return {
28
- async afterHandle({ set, response, store }) {
29
- const cacheKey = store._cacheKey;
38
+ async afterHandle({ set, response, store: store2, request }) {
39
+ const sanitizeUrl = new URL(request.url).pathname;
40
+ if (!store2._cachedRoutes.has(`${request.method}:${sanitizeUrl}`))
41
+ store2._cachedRoutes.add(`${request.method}:${sanitizeUrl}`);
42
+ const cacheKey = await generateCacheKey(request.clone());
30
43
  const now = new Date;
31
- set.headers["Cache-Control"] = `max-age=${ttl}, public`;
32
- set.headers["ETag"] = `"${prefix}${cacheKey}"`;
33
- set.headers["Last-Modified"] = now.toUTCString();
34
- set.headers["Expires"] = new Date(now.getTime() + ttl * 1000).toUTCString();
35
- await store.kvStore.set(`${prefix}${cacheKey}`, response, ttl);
44
+ set.headers["cache-control"] = `max-age=${ttl}, public`;
45
+ set.headers["etag"] = `"${prefix}${cacheKey}"`;
46
+ set.headers["last-modified"] = now.toUTCString();
47
+ set.headers["expires"] = new Date(now.getTime() + ttl * 1000).toUTCString();
48
+ const cacheData = {
49
+ response,
50
+ metadata: {
51
+ createdAt: now.toUTCString(),
52
+ ttl
53
+ }
54
+ };
55
+ await store2.kvStore.set(`${prefix}${cacheKey}`, cacheData, ttl);
36
56
  }
37
57
  };
38
58
  }
@@ -0,0 +1,7 @@
1
+ export interface CacheItem {
2
+ response: unknown;
3
+ metadata: {
4
+ createdAt: string;
5
+ ttl: number;
6
+ };
7
+ }
@@ -17,5 +17,5 @@ export interface CacheOptions {
17
17
  *
18
18
  * @defaultValue ':memory:'
19
19
  */
20
- storage?: ':memory:' | KvStore;
20
+ store?: ':memory:' | KvStore;
21
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nowarajs/elysia-cache",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "author": "NowaraJS",
5
5
  "description": "A template to create a bun package.",
6
6
  "type": "module",