Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/brick-container/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -193,7 +194,9 @@ if (window.parent) {
origin
);
});

http.setClearCacheIgnoreList(
previewOptions.clearPreviewRequestCacheIgnoreList || []
);
startPreview();
}
};
Expand Down Expand Up @@ -252,4 +255,5 @@ export interface PreviewStartOptions {
settings?: {
properties?: Record<string, unknown>;
};
clearPreviewRequestCacheIgnoreList?: ClearRequestCacheListConfig[];
}
68 changes: 68 additions & 0 deletions packages/brick-http/src/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 23 additions & 2 deletions packages/brick-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export interface HttpRequestConfig {
meta?: Record<string, any>;
options?: HttpOptions;
}
export interface ClearRequestCacheListConfig {
uri?: string;
method?: string;
}

export interface HttpResponse<T = any> {
data: T;
Expand Down Expand Up @@ -215,10 +219,16 @@ class Http {
private requestCache = new Map<string, Promise<any>>();

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]) {
Expand Down Expand Up @@ -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;
}

Expand Down