Skip to content
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

fix(core): fixed cache.clear() not working as expected #13926

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions packages/core/__tests__/Cache/StorageCacheCommon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,24 @@ describe('StorageCacheCommon', () => {
const cache = getStorageCache(config);

it('clears the cache, including the currentSizeKey', async () => {
mockGetAllCacheKeys.mockReturnValue([
const initialKeys = [
currentSizeKey,
`${keyPrefix}some-key`,
]);
`${keyPrefix}another-key`,
];
mockGetAllCacheKeys.mockResolvedValue(initialKeys);

await cache.clear();

expect(loggerSpy.debug).toHaveBeenCalledWith('Clear Cache');
expect(mockKeyValueStorageRemoveItem).toHaveBeenCalledTimes(2);
expect(mockKeyValueStorageRemoveItem).toHaveBeenCalledTimes(
initialKeys.length,
);
mockKeyValueStorageGetItem.mockResolvedValue(null);
yuhengshs marked this conversation as resolved.
Show resolved Hide resolved
mockGetAllCacheKeys.mockResolvedValue([]);
expect(await cache.getCurrentCacheSize()).toBe(0);
expect(await cache.getItem('some-key')).toBeNull();
yuhengshs marked this conversation as resolved.
Show resolved Hide resolved
expect(mockGetAllCacheKeys).toHaveBeenCalledTimes(1);
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Cache/StorageCacheCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ export abstract class StorageCacheCommon {
try {
const keys = await this.getAllKeys();
for (const key of keys) {
await this.getStorage().removeItem(key);
const prefixedKey = `${this.config.keyPrefix}${key}`;
await this.getStorage().removeItem(prefixedKey);
}
} catch (e) {
logger.warn(`clear failed! ${e}`);
Expand Down
Loading