Skip to content

Add ability to clear cache to allow real-time refresh of search results. #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2022
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
5 changes: 4 additions & 1 deletion src/cache/search-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { stringifyArray } from '../utils'
export function SearchCache(
cache: Record<string, string> = {}
): SearchCacheInterface {
const searchCache = cache
let searchCache = cache
return {
getEntry: function (key: string) {
if (searchCache[key]) {
Expand All @@ -25,5 +25,8 @@ export function SearchCache(
setEntry: function <T>(key: string, searchResponse: T) {
searchCache[key] = JSON.stringify(searchResponse)
},
clearCache: function () {
searchCache = {}
},
}
}
4 changes: 3 additions & 1 deletion src/client/instant-meilisearch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export function instantMeiliSearch(
apiKey = '',
instantMeiliSearchOptions: InstantMeiliSearchOptions = {}
): InstantMeiliSearchInstance {
const searchCache = SearchCache()
// create search resolver with included cache
const searchResolver = SearchResolver(SearchCache())
const searchResolver = SearchResolver(searchCache)
// paginationTotalHits can be 0 as it is a valid number
let defaultFacetDistribution: any = {}
const clientAgents = constructClientAgents(
Expand All @@ -43,6 +44,7 @@ export function instantMeiliSearch(
})

return {
clearCache: () => searchCache.clearCache(),
/**
* @param {readonlyAlgoliaMultipleQueriesQuery[]} instantSearchRequests
* @returns {Array}
Expand Down
5 changes: 4 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type SearchCacheInterface = {
getEntry: (key: string) => MeiliSearchResponse | undefined
formatKey: (components: any[]) => string
setEntry: <T>(key: string, searchResponse: T) => void
clearCache: () => void
}

export type InsideBoundingBox = string | ReadonlyArray<readonly number[]>
Expand Down Expand Up @@ -86,4 +87,6 @@ export type SearchContext = Omit<
pagination: PaginationContext
}

export type InstantMeiliSearchInstance = SearchClient
export type InstantMeiliSearchInstance = SearchClient & {
clearCache: () => void
}