-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat(javascript): add cache layer #274
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be0aef9
feat(javascript): add cache layer
shortcuts 4400bd6
Merge branch 'main' into feat/APIC-176/javascript-cache
shortcuts adb9896
should fix utils cache
shortcuts 7aeba74
use subfolder to make package clearer, fix jest testEnvironment
shortcuts 097c3fd
Merge branch 'main' into feat/APIC-176/javascript-cache
shortcuts df85f88
fix roots path
shortcuts e8a1a69
chore: make CI run JavaScript client-common test (#279)
shortcuts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
clients/algoliasearch-client-javascript/packages/client-common/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
clients/algoliasearch-client-javascript/packages/client-common/jest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { Config } from '@jest/types'; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...lient-javascript/packages/client-common/src/__tests__/browser-local-storage-cache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { createBrowserLocalStorageCache } from '../createBrowserLocalStorageCache'; | ||
|
||
const version = 'foobar'; | ||
const notAvailableStorage = new Proxy(window.localStorage, { | ||
get() { | ||
return (): void => { | ||
throw new Error('Component is not available'); | ||
}; | ||
}, | ||
}); | ||
|
||
type DefaultValue = Promise<{ bar: number }>; | ||
|
||
describe('browser local storage cache', () => { | ||
const missMock = jest.fn(); | ||
const events = { | ||
miss: (): Promise<any> => Promise.resolve(missMock()), | ||
}; | ||
|
||
beforeEach(() => { | ||
window.localStorage.clear(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('sets/gets values', async () => { | ||
const cache = createBrowserLocalStorageCache({ key: version }); | ||
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 1 }); | ||
|
||
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject( | ||
{ bar: 1 } | ||
); | ||
expect(missMock.mock.calls.length).toBe(1); | ||
|
||
await cache.set({ key: 'foo' }, { foo: 2 }); | ||
|
||
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject( | ||
{ foo: 2 } | ||
); | ||
expect(missMock.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('deletes keys', async () => { | ||
const cache = createBrowserLocalStorageCache({ key: version }); | ||
|
||
await cache.set({ key: 'foo' }, { bar: 1 }); | ||
await cache.delete({ key: 'foo' }); | ||
|
||
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 }); | ||
|
||
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject( | ||
{ bar: 2 } | ||
); | ||
expect(missMock.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('can be cleared', async () => { | ||
const cache = createBrowserLocalStorageCache({ key: version }); | ||
|
||
await cache.set({ key: 'foo' }, { bar: 1 }); | ||
await cache.clear(); | ||
|
||
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 }); | ||
|
||
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject( | ||
{ bar: 2 } | ||
); | ||
expect(missMock.mock.calls.length).toBe(1); | ||
expect(localStorage.length).toBe(0); | ||
}); | ||
|
||
it('do throws localstorage exceptions on access', async () => { | ||
const message = | ||
"Failed to read the 'localStorage' property from 'Window': Access is denied for this document."; | ||
const cache = createBrowserLocalStorageCache( | ||
new Proxy( | ||
{ key: 'foo' }, | ||
{ | ||
get(_, key): DOMException | string { | ||
if (key === 'key') { | ||
return 'foo'; | ||
} | ||
|
||
// Simulates a window.localStorage access. | ||
throw new DOMException(message); | ||
}, | ||
} | ||
) | ||
); | ||
const key = { foo: 'bar' }; | ||
const value = 'foo'; | ||
const fallback = 'bar'; | ||
|
||
await expect(cache.delete(key)).rejects.toEqual(new DOMException(message)); | ||
await expect(cache.set(key, value)).rejects.toEqual( | ||
new DOMException(message) | ||
); | ||
await expect( | ||
cache.get(key, () => Promise.resolve(fallback)) | ||
).rejects.toEqual(new DOMException(message)); | ||
}); | ||
|
||
it('do throws localstorage exceptions after access', async () => { | ||
const cache = createBrowserLocalStorageCache({ | ||
key: version, | ||
localStorage: notAvailableStorage, | ||
}); | ||
const key = { foo: 'bar' }; | ||
const value = 'foo'; | ||
const fallback = 'bar'; | ||
const message = 'Component is not available'; | ||
|
||
await expect(cache.delete(key)).rejects.toEqual(new Error(message)); | ||
await expect(cache.set(key, value)).rejects.toEqual(new Error(message)); | ||
await expect( | ||
cache.get(key, () => Promise.resolve(fallback)) | ||
).rejects.toEqual(new Error(message)); | ||
}); | ||
|
||
it('creates a namespace within local storage', async () => { | ||
const cache = createBrowserLocalStorageCache({ | ||
key: version, | ||
}); | ||
const key = { foo: 'bar' }; | ||
const value = 'foo'; | ||
|
||
expect( | ||
localStorage.getItem(`algoliasearch-client-js-${version}`) | ||
).toBeNull(); | ||
|
||
await cache.set(key, value); | ||
|
||
expect(localStorage.getItem(`algoliasearch-client-js-${version}`)).toBe( | ||
'{"{\\"foo\\":\\"bar\\"}":"foo"}' | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if memory cache should be the default value here. Probably null cache for all (response, request, host for both node and browser). What do you think?
What do we already have for the v4 js client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same here as the v4 js client, which seems to work well but I don't know much about the limitations here