Skip to content

Commit

Permalink
Merge pull request #1589 from input-output-hk/feat/LW-12228-improve-c…
Browse files Browse the repository at this point in the history
…aching-mechanism

feat: equip persistent cache with better data serialisation
  • Loading branch information
szymonmaslowski authored Feb 7, 2025
2 parents 69ca0b5 + 9d170e9 commit 6c72881
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Cache, fromSerializableObject, toSerializableObject } from '@cardano-sdk/util';
import { Storage } from 'webextension-polyfill';

type StorageLocal = Pick<Storage.StorageArea, 'get' | 'set' | 'remove'>;
Expand Down Expand Up @@ -31,7 +32,7 @@ export const makePersistentCacheStorageFactory =
fallbackMaxCollectionItemsGuard: number;
resourceName: string;
quotaInBytes: number;
}) => {
}): Cache<T> => {
const loaded = createVolatileCache<T>();
const metadataKey = makeMetadataKey(resourceName);
const getItemKey = (key: string) => makeItemKey(resourceName, key);
Expand Down Expand Up @@ -91,7 +92,7 @@ export const makePersistentCacheStorageFactory =
let value = loaded.get(itemKey);
if (!value) {
const result = await extensionLocalStorage.get(itemKey);
value = result[itemKey];
value = fromSerializableObject(result[itemKey]);
}

if (value) {
Expand All @@ -103,7 +104,7 @@ export const makePersistentCacheStorageFactory =
async set(key: string, value: T) {
const itemKey = getItemKey(key);
loaded.set(itemKey, value);
await extensionLocalStorage.set({ [itemKey]: value });
await extensionLocalStorage.set({ [itemKey]: toSerializableObject(value) });

void (async () => {
await updateAccessTime(itemKey);
Expand Down

0 comments on commit 6c72881

Please sign in to comment.