@nowarajs/elysia-cache 1.2.0 โ 1.2.1
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 +5 -14
- package/dist/cache.d.ts +1 -1
- package/dist/index.js +7 -7
- package/dist/types/cacheOptions.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
## v1.2.
|
|
2
|
+
## v1.2.1
|
|
3
3
|
|
|
4
|
-
[compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.
|
|
4
|
+
[compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.2.0...v1.2.1)
|
|
5
5
|
|
|
6
|
-
###
|
|
6
|
+
### ๐งน Refactors
|
|
7
7
|
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
### ๐ง Fixes
|
|
11
|
-
|
|
12
|
-
- **๐ง:** [Simplify cache response handling] ([86a502e](https://github.com/NowaraJS/elysia-cache/commit/86a502e))
|
|
8
|
+
- **๐งน:** [Rename storage to store in cache options] ([66faf70](https://github.com/NowaraJS/elysia-cache/commit/66faf70))
|
|
13
9
|
|
|
14
10
|
### ๐ Documentation
|
|
15
11
|
|
|
16
|
-
- **๐:** [Update CHANGELOG for version 1.
|
|
17
|
-
|
|
18
|
-
### ๐ฆ Build
|
|
19
|
-
|
|
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))
|
|
12
|
+
- **๐:** [Update CHANGELOG for version 1.2.0 release] ([e2bdc78](https://github.com/NowaraJS/elysia-cache/commit/e2bdc78))
|
|
22
13
|
|
|
23
14
|
### โค๏ธ Contributors
|
|
24
15
|
|
package/dist/cache.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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,
|
|
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;
|
package/dist/index.js
CHANGED
|
@@ -9,11 +9,11 @@ import { MemoryStore } from "@nowarajs/kv-store/memory";
|
|
|
9
9
|
var cache = ({
|
|
10
10
|
defaultTtl = 60,
|
|
11
11
|
prefix = "",
|
|
12
|
-
|
|
13
|
-
} = {}) => new Elysia().state("kvStore",
|
|
12
|
+
store = ":memory:"
|
|
13
|
+
} = {}) => new Elysia().state("kvStore", store === ":memory:" ? new MemoryStore : store).state("_cacheKey", "").onRequest(async ({ request, store: store2, set }) => {
|
|
14
14
|
const cacheKey = await generateCacheKey(request.clone());
|
|
15
|
-
const cachedResponse = await
|
|
16
|
-
|
|
15
|
+
const cachedResponse = await store2.kvStore.get(`${prefix}${cacheKey}`);
|
|
16
|
+
store2._cacheKey = cacheKey;
|
|
17
17
|
if (cachedResponse) {
|
|
18
18
|
set.headers["Cache-Control"] = `max-age=${defaultTtl}, public`;
|
|
19
19
|
set.headers["X-Cache"] = "HIT";
|
|
@@ -25,14 +25,14 @@ var cache = ({
|
|
|
25
25
|
isCached: (enable) => {
|
|
26
26
|
const ttl = typeof enable === "number" ? enable : enable ? defaultTtl : 0;
|
|
27
27
|
return {
|
|
28
|
-
async afterHandle({ set, response, store }) {
|
|
29
|
-
const cacheKey =
|
|
28
|
+
async afterHandle({ set, response, store: store2 }) {
|
|
29
|
+
const cacheKey = store2._cacheKey;
|
|
30
30
|
const now = new Date;
|
|
31
31
|
set.headers["Cache-Control"] = `max-age=${ttl}, public`;
|
|
32
32
|
set.headers["ETag"] = `"${prefix}${cacheKey}"`;
|
|
33
33
|
set.headers["Last-Modified"] = now.toUTCString();
|
|
34
34
|
set.headers["Expires"] = new Date(now.getTime() + ttl * 1000).toUTCString();
|
|
35
|
-
await
|
|
35
|
+
await store2.kvStore.set(`${prefix}${cacheKey}`, response, ttl);
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
}
|