Skip to content

Commit 8935023

Browse files
committedJan 18, 2025·
feat(v0.6.3): add persistent cache functionality to filesystem resolve #33
1 parent f83b433 commit 8935023

File tree

8 files changed

+186
-52
lines changed

8 files changed

+186
-52
lines changed
 

‎CHANGELOG.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# CHANGELOG 📝
22

3+
## v0.6.3
4+
5+
- Refactor: Add `cacheKey` to cache plugin for persistent cache
6+
- Tests: Add persistent cache test
7+
38
## v0.6.2
49

5-
- Refactor: minor improvment
10+
- Refactor: minor improvement
611
- Refactor: use `'./utils'` instead of `'xior/utils'`, fix [Unable to resolve module xior/utils #34](https://github.com/suhaotian/xior/issues/34)
712

813
## v0.6.1
@@ -11,7 +16,7 @@
1116

1217
## v0.6.0
1318

14-
- Fix: response interceptors should run after plugins(Fix https://github.com/suhaotian/xior/issues/29)
19+
- Fix: response interceptors should run after plugins (Fix https://github.com/suhaotian/xior/issues/29)
1520

1621
## v0.5.5
1722

@@ -27,7 +32,7 @@
2732

2833
## v0.5.2 2024/7/9
2934

30-
- Fix(core): if params include `Date` value, call `.toISOString()` and utils `encodeParams` support options `allowDot: true` and `arrayFormat: 'indices' | 'repeat' | 'brackets'`(default is `'indices'`). Fix [issues/22](https://github.com/suhaotian/xior/issues/22) and [issues/23](https://github.com/suhaotian/xior/issues/23)
35+
- Fix(core): if params include `Date` value, call `.toISOString()` and utils `encodeParams` support options `allowDot: true` and `arrayFormat: 'indices' | 'repeat' | 'brackets'` (default is `'indices'`). Fix [issues/22](https://github.com/suhaotian/xior/issues/22) and [issues/23](https://github.com/suhaotian/xior/issues/23)
3136

3237
---
3338

@@ -100,12 +105,12 @@ import { Xior } from 'xior';
100105

101106
This version is about Axios compatible issue in some cases. Fixing https://github.com/suhaotian/xior/issues/12 and https://github.com/suhaotian/xior/issues/15.
102107

103-
- Feat(core): when `responseType: 'blob' | 'arrarybuffer'` then the `resposne.data` is `Blob` and `ArrayBuffer`, no need `response.blob()` or `response.arraybuffer()` anymore.
104-
- Fix(interceptors): make sure the multiple response interceptors chain behaviour same as axios's interceptors.
108+
- Feat(core): when `responseType: 'blob' | 'arraybuffer'` then the `response.data` is `Blob` and `ArrayBuffer`, no need `response.blob()` or `response.arraybuffer()` anymore.
109+
- Fix(interceptors): make sure the multiple response interceptors chain behavior same as axios's interceptors.
105110

106111
## v0.3.13 2024/04/21
107112

108-
- Feat(plugin): add custom paramaters of LRU in plugins: cache, error-cache, throttle
113+
- Feat(plugin): add custom parameters of LRU in plugins: cache, error-cache, throttle
109114
- Feat(plugin): add `cacheTime` to cache plugin
110115

111116
## v0.3.12 2024/04/13
@@ -198,7 +203,7 @@ http.plugins.use(
198203

199204
## v0.3.0 2024/03/24
200205

201-
- fix(core): POST/DELETE/PUT/PATCH methods when `content-type=application/x-www-form-urlencoded`, use formData to in body(previous put in url)
206+
- fix(core): POST/DELETE/PUT/PATCH methods when `content-type=application/x-www-form-urlencoded`, use formData to in body (previous put in url)
202207
- refactor(core): default request interceptors should work before send fetch
203208
- refactor(core): remove `_data` in request config
204209
- refactor(core): remove `encode` in options, use `paramsSerializer` option instead
@@ -212,7 +217,7 @@ http.plugins.use(
212217
## v0.2.5 2024/03/20
213218

214219
- fix(plugin): fix `error-retry` plugin default options override bugs
215-
- fix(plugin): `requestConfig` with plugins should always get latest config from `requestinterceptors`
220+
- fix(plugin): `requestConfig` with plugins should always get latest config from `request interceptors`
216221

217222
## v0.2.4
218223

@@ -227,13 +232,13 @@ http.plugins.use(
227232

228233
- fix(plugin): fix `error-retry` plugin, `TypeError` should retry too
229234
- feat(plugin): `error-retry` plugin, `retryInterval` can be `function` too, and add `onRetry` to options
230-
- chore(core): minor improvment
235+
- chore(core): minor improvement
231236

232237
## v0.2.1 2024/03/17
233238

234239
- feat(core): support direct call `xior.get/post..` similar to `axios.get/post..` API, no need create instance at first
235240
- fix(core): `DELETE` and `OPTIONS` method's data option should be url encoded format like `GET` / `HEAD`
236-
- feat: add `UMD`(Universal Module Definition) format bundle(now you can directly load xior in browser)
241+
- feat: add `UMD` (Universal Module Definition) format bundle (now you can directly load xior in browser)
237242
- feat: add `VERSION` to `xior`, now you can get current version of xior by: `import xior from 'xior'; console.log(xior.VERSION)`
238243
- feat(new plugin): add `error-cache` plugin
239244
- feat(new plugin): add `dedupe` plugin
@@ -295,7 +300,7 @@ instance.options('/options_api_path', { params: { a: 1, text: 'this is data' } }
295300

296301
## v0.1.1 2024-03-04
297302

298-
- Fix: compatiable `delete` method with axios, and `delete` method shouldn't have body
303+
- Fix: compatible `delete` method with axios, and `delete` method shouldn't have body
299304
- Chore: remove unused code in core module
300305

301306
**Breaking change:**
@@ -319,15 +324,15 @@ http.delete('/', { params: { a: 1, b: 2 } });
319324

320325
## v0.0.9 2024-02-29
321326

322-
- fix(plugins): resolve import plugins not found file error in expo(react-native) project
327+
- fix(plugins): resolve import plugins not found file error in expo (react-native) project
323328

324329
## v0.0.8 2024-02-29
325330

326-
- feat(core): compatiable axios's options: `paramsSerializer` and `withCredentials`
331+
- feat(core): compatible axios's options: `paramsSerializer` and `withCredentials`
327332

328333
## v0.0.7 2024-02-27
329334

330-
- feat(core): suport nested object paramaters in default
335+
- feat(core): support nested object parameters in default
331336
- feat(plugin): implemented **error retry**, **cache**, **throttle**, and upload/download **progress** plugins
332337
- fix(build): resolved Bunchee build output error with Vite projects.
333338
- chore(doc): updated README.md
@@ -344,7 +349,7 @@ http.delete('/', { params: { a: 1, b: 2 } });
344349

345350
## v0.0.4
346351

347-
- feat: support url as first paramter in xiorInstance.request('/url')
352+
- feat: support url as first parameter in xiorInstance.request('/url')
348353
- feat: Removed first parameter `url` from `xiorInstance.request`
349354

350355
## v0.0.3

‎README.md

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![Build](https://github.com/suhaotian/xior/actions/workflows/check.yml/badge.svg)](https://github.com/suhaotian/xior/actions/workflows/check.yml)
2-
[![Size](https://deno.bundlejs.com/badge?q=xior@0.6.2&badge=detailed)](https://bundlejs.com/?q=xior%400.6.2)
2+
[![Size](https://deno.bundlejs.com/badge?q=xior@0.6.3&badge=detailed)](https://bundlejs.com/?q=xior%400.6.3)
33
[![npm version](https://badgen.net/npm/v/xior?color=green)](https://www.npmjs.com/package/xior)
44
![Downloads](https://img.shields.io/npm/dm/xior.svg?style=flat)
55
![typescript](https://badgen.net/badge/icon/typescript?icon=typescript&label&color=blue)
@@ -41,6 +41,7 @@ A lite request lib based on **fetch** with plugin support and similar API to axi
4141
- [Request dedupe plugin](#request-dedupe-plugin)
4242
- [Error cache plugin](#error-cache-plugin)
4343
- [Cache plugin](#cache-plugin)
44+
- [Persist cache data](#persist-cache-data)
4445
- [Upload and download progress plugin](#upload-and-download-progress-plugin)
4546
- [Mock plugin](#mock-plugin)
4647
- [Auth refresh token plugin(from community)](#auth-refresh-token-pluginfrom-community)
@@ -95,12 +96,12 @@ yarn add xior
9596

9697
#### Use CDN
9798

98-
> Since v0.2.1, xior support UMD format
99+
> Since v0.2.1, xior supports UMD format
99100
100101
Use jsDelivr CDN:
101102

102103
```html
103-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
104+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
104105

105106
<!-- Usage -->
106107
<script>
@@ -115,7 +116,7 @@ Use jsDelivr CDN:
115116
Use unpkg CDN:
116117

117118
```html
118-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
119+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
119120

120121
<!-- Usage -->
121122
<script>
@@ -131,7 +132,7 @@ Use unpkg CDN:
131132
import xior from 'xior';
132133

133134
export const xiorInstance = xior.create({
134-
baseURL: 'https://apiexampledomian.com/api',
135+
baseURL: 'https://apiexampledomain.com/api',
135136
headers: {
136137
// put your common custom headers here
137138
},
@@ -187,7 +188,7 @@ async function run() {
187188
import xior from 'xior';
188189

189190
export const xiorInstance = xior.create({
190-
baseURL: 'https://apiexampledomian.com/api',
191+
baseURL: 'https://apiexampledomain.com/api',
191192
});
192193

193194
function setAccessToken(token: string) {
@@ -207,7 +208,7 @@ function removeUserToken() {
207208
import xior from 'xior';
208209

209210
const xiorInstance = xior.create({
210-
baseURL: 'https://apiexampledomian.com/api',
211+
baseURL: 'https://apiexampledomain.com/api',
211212
});
212213

213214
const { data, headers } = await xiorInstance.get('/');
@@ -598,9 +599,9 @@ Use CDN:
598599
Using jsDelivr CDN:
599600

600601
```html
601-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
602+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
602603
<!-- Load plugin -->
603-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/error-retry.umd.js"></script>
604+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/error-retry.umd.js"></script>
604605

605606
<!-- Usage -->
606607
<script>
@@ -613,10 +614,10 @@ Using jsDelivr CDN:
613614
Using unpkg CDN:
614615

615616
```html
616-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
617+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
617618

618619
<!-- Load plugin -->
619-
<script src="https://unpkg.com/xior@0.6.2/plugins/error-retry.umd.js"></script>
620+
<script src="https://unpkg.com/xior@0.6.3/plugins/error-retry.umd.js"></script>
620621

621622
<!-- Usage -->
622623
<script>
@@ -709,9 +710,9 @@ Use CDN:
709710
Using jsDelivr CDN:
710711

711712
```html
712-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
713+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
713714
<!-- Load plugin -->
714-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/throttle.umd.js"></script>
715+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/throttle.umd.js"></script>
715716

716717
<!-- Usage -->
717718
<script>
@@ -724,10 +725,10 @@ Using jsDelivr CDN:
724725
Using unpkg CDN:
725726

726727
```html
727-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
728+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
728729

729730
<!-- Load plugin -->
730-
<script src="https://unpkg.com/xior@0.6.2/plugins/throttle.umd.js"></script>
731+
<script src="https://unpkg.com/xior@0.6.3/plugins/throttle.umd.js"></script>
731732

732733
<!-- Usage -->
733734
<script>
@@ -782,9 +783,9 @@ Use CDN:
782783
Using jsDelivr CDN:
783784

784785
```html
785-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
786+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
786787
<!-- Load plugin -->
787-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/dedupe.umd.js"></script>
788+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/dedupe.umd.js"></script>
788789

789790
<!-- Usage -->
790791
<script>
@@ -797,10 +798,10 @@ Using jsDelivr CDN:
797798
Using unpkg CDN:
798799

799800
```html
800-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
801+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
801802

802803
<!-- Load plugin -->
803-
<script src="https://unpkg.com/xior@0.6.2/plugins/dedupe.umd.js"></script>
804+
<script src="https://unpkg.com/xior@0.6.3/plugins/dedupe.umd.js"></script>
804805

805806
<!-- Usage -->
806807
<script>
@@ -862,9 +863,9 @@ Use CDN:
862863
Using jsDelivr CDN:
863864

864865
```html
865-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
866+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
866867
<!-- Load plugin -->
867-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/error-cache.umd.js"></script>
868+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/error-cache.umd.js"></script>
868869

869870
<!-- Usage -->
870871
<script>
@@ -877,10 +878,10 @@ Using jsDelivr CDN:
877878
Using unpkg CDN:
878879

879880
```html
880-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
881+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
881882

882883
<!-- Load plugin -->
883-
<script src="https://unpkg.com/xior@0.6.2/plugins/error-cache.umd.js"></script>
884+
<script src="https://unpkg.com/xior@0.6.3/plugins/error-cache.umd.js"></script>
884885

885886
<!-- Usage -->
886887
<script>
@@ -942,7 +943,7 @@ http.post('/users', { enableCache: true }); // make real http request
942943
const res = await http.post('/users', { enableCache: true }); // get cache from previous request
943944
if (res.fromCache) {
944945
// if `fromCache` is true, means data from cache!
945-
console.log('data from cache!', res.cacheTime);
946+
console.log('data from cache!', res.cacheKey, res.cacheTime);
946947
}
947948
```
948949

@@ -974,6 +975,12 @@ const cacheA = lru(100);
974975
http.get('/users', { enableCache: true, defaultCache: cacheA, forceUpdate: true });
975976
```
976977

978+
### Persist cache data
979+
980+
How to persist cache data to the filesystem to prevent loss after a server restart?
981+
982+
For more details, refer to this GitHub issue: [GitHub issue 33](https://github.com/suhaotian/xior/issues/33)
983+
977984
### Upload and download progress plugin
978985

979986
> Enable upload and download progress like axios, but the progress is simulated,
@@ -1025,9 +1032,9 @@ Use CDN:
10251032
Using jsDelivr CDN:
10261033

10271034
```html
1028-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
1035+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
10291036
<!-- Load plugin -->
1030-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/progress.umd.js"></script>
1037+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/progress.umd.js"></script>
10311038

10321039
<!-- Usage -->
10331040
<script>
@@ -1040,10 +1047,10 @@ Using jsDelivr CDN:
10401047
Using unpkg CDN:
10411048

10421049
```html
1043-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
1050+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
10441051

10451052
<!-- Load plugin -->
1046-
<script src="https://unpkg.com/xior@0.6.2/plugins/progress.umd.js"></script>
1053+
<script src="https://unpkg.com/xior@0.6.3/plugins/progress.umd.js"></script>
10471054

10481055
<!-- Usage -->
10491056
<script>
@@ -1150,9 +1157,9 @@ Use CDN:
11501157
Using jsDelivr CDN:
11511158

11521159
```html
1153-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/dist/xior.umd.js"></script>
1160+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/dist/xior.umd.js"></script>
11541161
<!-- Load plugin -->
1155-
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.2/plugins/mock.umd.js"></script>
1162+
<script src="https://cdn.jsdelivr.net/npm/xior@0.6.3/plugins/mock.umd.js"></script>
11561163

11571164
<!-- Usage -->
11581165
<script>
@@ -1165,10 +1172,10 @@ Using jsDelivr CDN:
11651172
Using unpkg CDN:
11661173

11671174
```html
1168-
<script src="https://unpkg.com/xior@0.6.2/dist/xior.umd.js"></script>
1175+
<script src="https://unpkg.com/xior@0.6.3/dist/xior.umd.js"></script>
11691176

11701177
<!-- Load plugin -->
1171-
<script src="https://unpkg.com/xior@0.6.2/plugins/mock.umd.js"></script>
1178+
<script src="https://unpkg.com/xior@0.6.3/plugins/mock.umd.js"></script>
11721179

11731180
<!-- Usage -->
11741181
<script>

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xior",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"description": "A lite request lib based on fetch with plugin support and similar API to axios.",
55
"repository": "suhaotian/xior",
66
"bugs": "https://github.com/suhaotian/xior/issues",

‎src/plugins/cache/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ declare module 'xior' {
3030
cacheTime?: number;
3131
cacheKey?: string;
3232
}
33+
34+
interface XiorResponseInterceptorConfig {
35+
fromCache?: boolean;
36+
cacheTime?: number;
37+
cacheKey?: string;
38+
}
3339
}
3440

3541
export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlugin {
@@ -79,7 +85,9 @@ export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlu
7985
if (!responsePromise || forceUpdate) {
8086
responsePromise = (async () => {
8187
try {
82-
return await adapter(config);
88+
const res = await adapter(config);
89+
if (res) (res as any).cacheKey = key;
90+
return res;
8391
} catch (reason) {
8492
if ('delete' in cache) {
8593
cache.delete(key);
@@ -98,7 +106,7 @@ export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlu
98106
return responsePromise.then((res) => {
99107
(res as any).fromCache = true;
100108
(res as any).cacheTime = Date.now();
101-
(res as any).cacheKey = key;
109+
if (key) (res as any).cacheKey = key;
102110

103111
return res;
104112
});

‎src/xior.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Xior {
4747
static create(options?: XiorRequestConfig): XiorInstance {
4848
return new Xior(options);
4949
}
50-
static VERSION = '0.6.2';
50+
static VERSION = '0.6.3';
5151

5252
config?: XiorRequestConfig;
5353
defaults: XiorInterceptorRequestConfig;

‎tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cache.json

‎tests/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"scripts": {
55
"build:lib": "rm -rf lib && tsc --project tsconfig.json",
6-
"test": "pnpm build:lib && node --test ./lib"
6+
"test": "pnpm build:lib && node --test ./lib",
7+
"test:cache": "pnpm build:lib && node --test ./lib/plugins/persist-cache.test.js"
78
},
89
"devDependencies": {
910
"xior": "workspace:*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import fs from 'fs/promises';
2+
import assert from 'node:assert';
3+
import { after, before, describe, it } from 'node:test';
4+
import { lru, LRU } from 'tiny-lru';
5+
import { Xior as xior, delay, Xior } from 'xior';
6+
import xiorCachePlugin from 'xior/plugins/cache';
7+
8+
import { startServer } from '../server';
9+
10+
let close: Function;
11+
const port = 7778;
12+
const baseURL = `http://localhost:${port}`;
13+
before(async () => {
14+
close = await startServer(port);
15+
});
16+
17+
after(async () => {
18+
return close(1);
19+
});
20+
21+
export function persistCachePlugin(
22+
xiorInstance: Xior,
23+
cache: LRU<any>,
24+
filePath: string,
25+
debounceTime = 500
26+
) {
27+
xiorInstance.interceptors.response.use((response) => {
28+
if (!response.fromCache && response.cacheKey) {
29+
debouncedSyncToFs();
30+
}
31+
return response;
32+
});
33+
34+
async function initFromFs() {
35+
try {
36+
const data = JSON.parse(await fs.readFile(filePath, 'utf-8'));
37+
data.forEach(([key, value]: [string, any]) => {
38+
if (value) {
39+
cache.set(key, Promise.resolve(value));
40+
}
41+
});
42+
} catch (e) {
43+
console.error('initFromFs error:', e);
44+
}
45+
}
46+
47+
async function syncToFs() {
48+
const keys = cache.keys();
49+
const data = await Promise.all(keys.map(async (key) => [key, await cache.get(key)]));
50+
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
51+
}
52+
const debouncedSyncToFs = debounce(syncToFs, debounceTime);
53+
54+
function debounce(func: () => Promise<void>, wait: number) {
55+
let timeout: NodeJS.Timeout | null = null;
56+
return () => {
57+
if (timeout) {
58+
clearTimeout(timeout);
59+
}
60+
timeout = setTimeout(() => {
61+
func();
62+
}, wait);
63+
};
64+
}
65+
66+
return { initFromFs, debouncedSyncToFs };
67+
}
68+
69+
const instance = xior.create({ baseURL });
70+
const cache = lru(100, 1000 * 60 * 5);
71+
instance.plugins.use(
72+
xiorCachePlugin({
73+
enableCache: (config) => config?.method === 'GET' || config?.isGet === true,
74+
defaultCache: cache,
75+
})
76+
);
77+
const { initFromFs } = persistCachePlugin(instance, cache, './cache.json', 500);
78+
79+
describe('xior cache plugin tests', async () => {
80+
it('Generate cache data should work', async () => {
81+
for (const key of [1, 2, 3, 4, 5, 6, 7, 8]) {
82+
const { data } = await instance.get<{ value: string }>('/get', {
83+
params: { idx: key },
84+
headers: {
85+
'x-custom-value': key,
86+
},
87+
});
88+
assert.strictEqual(data.value, key + '');
89+
}
90+
assert.strictEqual(cache.size, 8);
91+
await delay(600);
92+
});
93+
94+
it('Async cache should work', async () => {
95+
cache.clear();
96+
assert.strictEqual(cache.size, 0);
97+
98+
await initFromFs();
99+
100+
assert.strictEqual(cache.size, 8);
101+
for (const key of [1, 2, 3, 4, 5, 6, 7, 8]) {
102+
const { data, fromCache } = await instance.get<{ value: string }>('/get', {
103+
params: { idx: key },
104+
headers: {
105+
'x-custom-value': key,
106+
},
107+
});
108+
assert.strictEqual(data.value, key + '');
109+
assert.strictEqual(fromCache, true);
110+
}
111+
});
112+
});

0 commit comments

Comments
 (0)
Please sign in to comment.