Skip to content

Commit

Permalink
feat: retry阶段请求不再计入缓存,而是外面那层计入缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed May 2, 2022
1 parent 26127af commit 4ea005e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion __test__/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('AxiosRequestTemplate retry', () => {
params: { test: 1 },
});
expect(mockGet.mock.calls.length).toBe(1);
expect(mockSet.mock.calls.length).toBe(4);
expect(mockSet.mock.calls.length).toBe(1);
});
describe('immediate', () => {
test('use', async () => {
Expand Down
12 changes: 5 additions & 7 deletions src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,15 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
}

// 请求
protected useCache(ctx: RetryContext<CC>, request: () => Promise<any>) {
protected useCache(ctx: Context<CC>, request: () => Promise<any>) {
const { customConfig, requestKey } = ctx;

const cacheConfig = customConfig.cache as CustomCacheConfig;

// 缓存
if (cacheConfig.enable) {
if (!ctx.isRetry) {
const cache = this.cache.get(requestKey);
if (cache) return cache;
}
const cache = this.cache.get(requestKey);
if (cache) return cache;
// 请求
const promise = request();
// 存储缓存
Expand All @@ -200,7 +198,7 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {

// 请求
protected fetch(ctx: RetryContext<CC>) {
return this.useCache(ctx, () => this.axiosIns(ctx.requestConfig));
return this.axiosIns(ctx.requestConfig);
}

protected handleRetry(ctx: Context<CC>) {
Expand Down Expand Up @@ -319,7 +317,7 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
const ctx = this.generateContext(customConfig, requestConfig);
this.beforeRequest(ctx);
try {
return await this.execRequest(ctx);
return await this.useCache(ctx, () => this.execRequest(ctx));
} catch (e: any) {
return await this.handleError(ctx, e);
} finally {
Expand Down

0 comments on commit 4ea005e

Please sign in to comment.