Skip to content

Commit

Permalink
limit scope
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 6, 2023
1 parent 9382261 commit d1e03d3
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,26 @@ export function defineCachedFunction<T = any>(

try {
entry.value = await pending[key];

} catch (error) {
// Make sure entries that reject get removed.
if (!isPending) {
// Update mtime, integrity + validate and set the value in cache only the first time the request is made.
entry.mtime = Date.now();
entry.integrity = integrity;
delete pending[key];
if (validate(entry)) {
useStorage()
.setItem(cacheKey, entry)
.catch((error) => console.error("[nitro] [cache]", error));
}
}
} catch (error) {
// Make sure entries that reject get removed.
delete pending[key];

// Re-throw error to make sure the caller knows the task failed.
throw error;
}

if (!isPending) {
// Update mtime, integrity + validate and set the value in cache only the first time the request is made.
entry.mtime = Date.now();
entry.integrity = integrity;
delete pending[key];
if (validate(entry)) {
useStorage()
.setItem(cacheKey, entry)
.catch((error) => console.error("[nitro] [cache]", error));
}
}
};

const _resolvePromise = expired ? _resolve() : Promise.resolve();
Expand Down

0 comments on commit d1e03d3

Please sign in to comment.