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 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
19 changes: 12 additions & 7 deletions packages/core/__tests__/Cache/StorageCacheCommon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultConfig } from '../../src/Cache/constants';
import { StorageCacheCommon } from '../../src/Cache/StorageCacheCommon';
import { KeyValueStorageInterface } from '../../src/types';
import { ConsoleLogger } from '../../src/Logger';
import { StorageCache } from '../../src/Cache/StorageCache';
import {
getByteLength,
getCurrentSizeKey,
Expand Down Expand Up @@ -584,16 +585,20 @@ describe('StorageCacheCommon', () => {
});

describe('clear()', () => {
const cache = getStorageCache(config);
const cache = new StorageCache(config);

it('clears the cache, including the currentSizeKey', async () => {
mockGetAllCacheKeys.mockReturnValue([
currentSizeKey,
`${keyPrefix}some-key`,
]);
await cache.setItem('key1', 'value1');
await cache.setItem('key2', 'value2');

expect(await cache.getItem('key1')).toBe('value1');
expect(await cache.getItem('key2')).toBe('value2');

await cache.clear();
expect(loggerSpy.debug).toHaveBeenCalledWith('Clear Cache');
expect(mockKeyValueStorageRemoveItem).toHaveBeenCalledTimes(2);

expect(await cache.getItem('key1')).toBeNull();
expect(await cache.getItem('key2')).toBeNull();
expect(await cache.getCurrentCacheSize()).toBe(0);
});
});

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