From f7a6d417625642af4d37ae0ca87cdeb4a316a7fa Mon Sep 17 00:00:00 2001 From: astrid Date: Mon, 22 Aug 2022 21:09:51 +0800 Subject: [PATCH] feat(): support clearPreviewRequestCacheIgnoreList refs NEXT_BUILDER-1908 --- packages/brick-container/src/index.tsx | 6 ++- packages/brick-http/src/http.spec.ts | 68 ++++++++++++++++++++++++++ packages/brick-http/src/http.ts | 25 +++++++++- 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/packages/brick-container/src/index.tsx b/packages/brick-container/src/index.tsx index c3589f5b54..a05626ae3a 100644 --- a/packages/brick-container/src/index.tsx +++ b/packages/brick-container/src/index.tsx @@ -16,6 +16,7 @@ import { HttpRequestConfig, HttpResponse, HttpError, + ClearRequestCacheListConfig, } from "@next-core/brick-http"; import { initializeLibrary } from "@next-core/fontawesome-library"; import { apiAnalyzer } from "@next-core/easyops-analytics"; @@ -193,7 +194,9 @@ if (window.parent) { origin ); }); - + http.setClearCacheIgnoreList( + previewOptions.clearPreviewRequestCacheIgnoreList || [] + ); startPreview(); } }; @@ -252,4 +255,5 @@ export interface PreviewStartOptions { settings?: { properties?: Record; }; + clearPreviewRequestCacheIgnoreList?: ClearRequestCacheListConfig[]; } diff --git a/packages/brick-http/src/http.spec.ts b/packages/brick-http/src/http.spec.ts index 2eae6071b8..3d5c8a203c 100644 --- a/packages/brick-http/src/http.spec.ts +++ b/packages/brick-http/src/http.spec.ts @@ -153,6 +153,15 @@ describe("http", () => { const mockListen = jest.fn(); http.on("match-api-cache", mockListen); http.enableCache(true); + http.setClearCacheIgnoreList([ + { + method: "GET", + }, + { + method: "POST", + uri: "http://example.com", + }, + ]); // get await http.get("http://example.com"); expect(spyOnFetch).toBeCalledTimes(1); @@ -209,6 +218,65 @@ describe("http", () => { expect(mockListen).toHaveBeenLastCalledWith(6); }); + it("preview was true and http request should use clearCacheIgnoreList", async () => { + const mockListen = jest.fn(); + http.on("match-api-cache", mockListen); + http.enableCache(true); + http.setClearCacheIgnoreList([ + { + method: "GET", + }, + { + method: "POST", + uri: ".*cmdb.instance.PostSearch.*", + }, + { + method: "POST", + uri: "api/gateway/data_exchange.olap.Query/api/v1/data_exchange/olap", + }, + ]); + // get + await http.get( + "api/gateway/cmdb.instance.GetDetail/object/HOST/instance/58fe908324229" + ); + expect(spyOnFetch).toBeCalledTimes(1); + await http.post( + "api/gateway/cmdb.instance.PostSearch/object/HOST/instance/_search", + { + fields: { ip: 1 }, + } + ); + expect(spyOnFetch).toBeCalledTimes(2); + await http.post( + "api/gateway/data_exchange.olap.Query/api/v1/data_exchange/olap", + { + test: 1, + } + ); + expect(spyOnFetch).toBeCalledTimes(3); + + await http.get( + "api/gateway/cmdb.instance.GetDetail/object/HOST/instance/58fe908324229" + ); + expect(spyOnFetch).toBeCalledTimes(3); + await http.post( + "api/gateway/cmdb.instance.PostSearch/object/HOST/instance/_search", + { + fields: { ip: 1 }, + } + ); + expect(spyOnFetch).toBeCalledTimes(3); + // 在 clearCacheIgnoreList 名单外,清除缓存 + await http.post( + "api/gateway/data_exchange.olap.Query/api/v2/data_exchange/olap", + { + test: 1, + } + ); + expect(spyOnFetch).toBeCalledTimes(4); + expect(mockListen).toHaveBeenLastCalledWith(10); + }); + it("should work with getUrlWithParams", () => { const result = http.getUrlWithParams("http://example.com/for-good", { name: "monkey", diff --git a/packages/brick-http/src/http.ts b/packages/brick-http/src/http.ts index da920caa1f..a77b76d720 100644 --- a/packages/brick-http/src/http.ts +++ b/packages/brick-http/src/http.ts @@ -9,6 +9,10 @@ export interface HttpRequestConfig { meta?: Record; options?: HttpOptions; } +export interface ClearRequestCacheListConfig { + uri?: string; + method?: string; +} export interface HttpResponse { data: T; @@ -215,10 +219,16 @@ class Http { private requestCache = new Map>(); private isEnableCache: boolean; + private clearCacheIgnoreList: ClearRequestCacheListConfig[] = []; public enableCache = (enable: boolean): void => { this.isEnableCache = enable; }; + public setClearCacheIgnoreList = ( + list: ClearRequestCacheListConfig[] + ): void => { + this.clearCacheIgnoreList = list; + }; public on(action: string, fn: any): void { if (this.listener[action]) { @@ -329,8 +339,19 @@ class Http { while (chain.length) { promise = promise.then(chain.shift(), chain.shift()); } - - this.isEnableCache && this.requestCache.set(key, promise); + if (this.isEnableCache) { + const shouldCache = this.clearCacheIgnoreList.some( + (req) => + req.method === config.method && + (!req.uri || new RegExp(req.uri).test(config.url)) + ); + // 遇到 clearCacheIgnoreList 缓存列表外的,需要清除缓存 + if (shouldCache) { + this.requestCache.set(key, promise); + } else { + this.requestCache.clear(); + } + } return promise; }