@algolia/cache-browser-local-storage
Advanced tools
Comparing version 4.17.2 to 4.18.0
@@ -18,2 +18,21 @@ 'use strict'; | ||
}; | ||
const setNamespace = (namespace) => { | ||
getStorage().setItem(namespaceKey, JSON.stringify(namespace)); | ||
}; | ||
const removeOutdatedCacheItems = () => { | ||
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null; | ||
const namespace = getNamespace(); | ||
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
return cacheItem.timestamp !== undefined; | ||
})); | ||
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems); | ||
if (!timeToLive) | ||
return; | ||
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => { | ||
const currentTimestamp = new Date().getTime(); | ||
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp; | ||
return !isExpired; | ||
})); | ||
setNamespace(filteredNamespaceWithoutExpiredItems); | ||
}; | ||
return { | ||
@@ -25,6 +44,9 @@ get(key, defaultValue, events = { | ||
.then(() => { | ||
removeOutdatedCacheItems(); | ||
const keyAsString = JSON.stringify(key); | ||
const value = getNamespace()[keyAsString]; | ||
return Promise.all([value || defaultValue(), value !== undefined]); | ||
return getNamespace()[keyAsString]; | ||
}) | ||
.then(value => { | ||
return Promise.all([value ? value.value : defaultValue(), value !== undefined]); | ||
}) | ||
.then(([value, exists]) => { | ||
@@ -39,3 +61,6 @@ return Promise.all([value, exists || events.miss(value)]); | ||
// eslint-disable-next-line functional/immutable-data | ||
namespace[JSON.stringify(key)] = value; | ||
namespace[JSON.stringify(key)] = { | ||
timestamp: new Date().getTime(), | ||
value, | ||
}; | ||
getStorage().setItem(namespaceKey, JSON.stringify(namespace)); | ||
@@ -42,0 +67,0 @@ return value; |
import { Cache } from '@algolia/cache-common'; | ||
export declare type BrowserLocalStorageCacheItem = { | ||
/** | ||
* The cache item creation timestamp. | ||
*/ | ||
readonly timestamp: number; | ||
/** | ||
* The cache item value | ||
*/ | ||
readonly value: any; | ||
}; | ||
export declare type BrowserLocalStorageOptions = { | ||
@@ -9,2 +20,6 @@ /** | ||
/** | ||
* The time to live for each cached item in seconds. | ||
*/ | ||
readonly timeToLive?: number; | ||
/** | ||
* The native local storage implementation. | ||
@@ -11,0 +26,0 @@ */ |
@@ -14,2 +14,21 @@ function createBrowserLocalStorageCache(options) { | ||
}; | ||
const setNamespace = (namespace) => { | ||
getStorage().setItem(namespaceKey, JSON.stringify(namespace)); | ||
}; | ||
const removeOutdatedCacheItems = () => { | ||
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null; | ||
const namespace = getNamespace(); | ||
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
return cacheItem.timestamp !== undefined; | ||
})); | ||
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems); | ||
if (!timeToLive) | ||
return; | ||
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => { | ||
const currentTimestamp = new Date().getTime(); | ||
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp; | ||
return !isExpired; | ||
})); | ||
setNamespace(filteredNamespaceWithoutExpiredItems); | ||
}; | ||
return { | ||
@@ -21,6 +40,9 @@ get(key, defaultValue, events = { | ||
.then(() => { | ||
removeOutdatedCacheItems(); | ||
const keyAsString = JSON.stringify(key); | ||
const value = getNamespace()[keyAsString]; | ||
return Promise.all([value || defaultValue(), value !== undefined]); | ||
return getNamespace()[keyAsString]; | ||
}) | ||
.then(value => { | ||
return Promise.all([value ? value.value : defaultValue(), value !== undefined]); | ||
}) | ||
.then(([value, exists]) => { | ||
@@ -35,3 +57,6 @@ return Promise.all([value, exists || events.miss(value)]); | ||
// eslint-disable-next-line functional/immutable-data | ||
namespace[JSON.stringify(key)] = value; | ||
namespace[JSON.stringify(key)] = { | ||
timestamp: new Date().getTime(), | ||
value, | ||
}; | ||
getStorage().setItem(namespaceKey, JSON.stringify(namespace)); | ||
@@ -38,0 +63,0 @@ return value; |
{ | ||
"name": "@algolia/cache-browser-local-storage", | ||
"version": "4.17.2", | ||
"version": "4.18.0", | ||
"private": false, | ||
@@ -20,4 +20,4 @@ "description": "Promise-based cache library for browser using local storage.", | ||
"dependencies": { | ||
"@algolia/cache-common": "4.17.2" | ||
"@algolia/cache-common": "4.18.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8140
187
+ Added@algolia/cache-common@4.18.0(transitive)
- Removed@algolia/cache-common@4.17.2(transitive)
Updated@algolia/cache-common@4.18.0