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
13 changes: 12 additions & 1 deletion src/Typesense/RequestWithCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ export default class RequestWithCache {
responsePromise,
});

const response = await responsePromise;
let response: TResult;
try {
response = await responsePromise;
} catch (error) {
if (
this.responsePromiseCache.get(requestParamsJSON)?.responsePromise ===
responsePromise
) {
this.responsePromiseCache.delete(requestParamsJSON);
}
throw error;
}
this.responseCache.set(requestParamsJSON, {
requestTimestamp: now,
response: response,
Expand Down
43 changes: 43 additions & 0 deletions test/Typesense/SearchClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,47 @@ describe("SearchClient", function () {

expect(mockAxios.history["post"].length).toBe(1);
});

it("should not cache failed multi_search requests", async function () {
typesense = new TypesenseSearchClient({
nodes: [
{
host: "node0",
port: 8108,
protocol: "http",
},
],
apiKey: "abcd",
randomizeNodes: false,
numRetries: 0,
cacheSearchResultsForSeconds: 2 * 60,
});
const searchRequest = {
searches: [{ q: "term1" }],
};
const commonParams = {
collection: "docs",
query_by: "field",
};

mockAxios
.onPost("http://node0:8108/multi_search")
.networkErrorOnce()
.onPost("http://node0:8108/multi_search")
.reply(200, JSON.stringify({ results: [] }), {
"content-type": "application/json",
});

await expect(
typesense.multiSearch.perform(searchRequest, commonParams),
).rejects.toThrow();

const response = await typesense.multiSearch.perform(
searchRequest,
commonParams,
);

expect(response).toEqual({ results: [] });
expect(mockAxios.history["post"].length).toBe(2);
});
});
Loading