Skip to content

Commit

Permalink
Merge #808
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Jul 25, 2022
2 parents bf35ee4 + 7ee595e commit c01f9fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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
}

0 comments on commit c01f9fd

Please sign in to comment.