Skip to content

Commit c01f9fd

Browse files
meili-bors[bot]sc-kknowlesfadeaway
authored
Merge #808
808: Add ability to clear cache to allow real-time refresh of search results. r=bidoubiwa a=fadeaway # Pull Request ## What does this PR do? Fixes [#790](#790) Implements clearCache method on InstantMeiliSearchInstance to allow realtime updates to search results. This allows algolias refresh example to work out of the box. ([https://www.algolia.com/doc/guides/building-search-ui/going-further/improve-performance/vue/#caching](https://www.algolia.com/doc/guides/building-search-ui/going-further/improve-performance/vue/#caching)) ## PR checklist Please check if your PR fulfills the following requirements: - [X ] Does this PR fix an existing issue? - [X ] Have you read the contributing guidelines? - [X ] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Kyle Knowles <[email protected]> Co-authored-by: Kyle Knowles <[email protected]>
2 parents bf35ee4 + 7ee595e commit c01f9fd

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/cache/search-cache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { stringifyArray } from '../utils'
77
export function SearchCache(
88
cache: Record<string, string> = {}
99
): SearchCacheInterface {
10-
const searchCache = cache
10+
let searchCache = cache
1111
return {
1212
getEntry: function (key: string) {
1313
if (searchCache[key]) {
@@ -25,5 +25,8 @@ export function SearchCache(
2525
setEntry: function <T>(key: string, searchResponse: T) {
2626
searchCache[key] = JSON.stringify(searchResponse)
2727
},
28+
clearCache: function () {
29+
searchCache = {}
30+
},
2831
}
2932
}

src/client/instant-meilisearch-client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ export function instantMeiliSearch(
2828
apiKey = '',
2929
instantMeiliSearchOptions: InstantMeiliSearchOptions = {}
3030
): InstantMeiliSearchInstance {
31+
const searchCache = SearchCache()
3132
// create search resolver with included cache
32-
const searchResolver = SearchResolver(SearchCache())
33+
const searchResolver = SearchResolver(searchCache)
3334
// paginationTotalHits can be 0 as it is a valid number
3435
let defaultFacetDistribution: any = {}
3536
const clientAgents = constructClientAgents(
@@ -43,6 +44,7 @@ export function instantMeiliSearch(
4344
})
4445

4546
return {
47+
clearCache: () => searchCache.clearCache(),
4648
/**
4749
* @param {readonlyAlgoliaMultipleQueriesQuery[]} instantSearchRequests
4850
* @returns {Array}

src/types/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type SearchCacheInterface = {
4040
getEntry: (key: string) => MeiliSearchResponse | undefined
4141
formatKey: (components: any[]) => string
4242
setEntry: <T>(key: string, searchResponse: T) => void
43+
clearCache: () => void
4344
}
4445

4546
export type InsideBoundingBox = string | ReadonlyArray<readonly number[]>
@@ -86,4 +87,6 @@ export type SearchContext = Omit<
8687
pagination: PaginationContext
8788
}
8889

89-
export type InstantMeiliSearchInstance = SearchClient
90+
export type InstantMeiliSearchInstance = SearchClient & {
91+
clearCache: () => void
92+
}

0 commit comments

Comments
 (0)