simple-in-memory-cache
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -0,6 +1,8 @@ | ||
import type { PickOne } from 'type-fns'; | ||
export interface SimpleInMemoryCache<T> { | ||
get: (key: string) => T | undefined; | ||
set: (key: string, value: T, options?: { | ||
set: (key: string, value: T, options?: Partial<PickOne<{ | ||
secondsUntilExpiration?: number; | ||
}) => void; | ||
seconds?: number; | ||
}>>) => void; | ||
keys: () => string[]; | ||
@@ -14,3 +16,3 @@ } | ||
} | ||
export declare const createCache: <T>({ seconds, defaultSecondsUntilExpiration: defaultSecondsUntilExpirationInput, }?: { | ||
export declare const createCache: <T>({ seconds, defaultSecondsUntilExpiration: defaultSecondsUntilExpirationInput, }?: Partial<PickOne<{ | ||
/** | ||
@@ -27,2 +29,2 @@ * the number of seconds items in the cache expire after | ||
seconds?: number | undefined; | ||
}) => SimpleInMemoryCache<T>; | ||
}>>) => SimpleInMemoryCache<T>; |
@@ -27,3 +27,3 @@ "use strict"; | ||
return undefined; // if not in cache, then undefined | ||
if (cacheContent.expiresAtMse < getMseNow()) | ||
if (cacheContent.expiresAtMse <= getMseNow()) | ||
return undefined; // if already expired, then undefined | ||
@@ -30,0 +30,0 @@ return cacheContent.value; // otherwise, its in the cache and not expired, so return the value |
@@ -71,2 +71,17 @@ "use strict"; | ||
})); | ||
it('should consider secondsUntilExpiration of null or infinity as never expiring', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { set, get } = (0, cache_1.createCache)({ | ||
defaultSecondsUntilExpiration: 0, // expire immediately | ||
}); | ||
// prove that setting something to the cache with default state will have it expired immediately | ||
yield set('dory-memory', 'something'); // lets see if dory can remember something | ||
const doryMemory = yield get('dory-memory'); | ||
expect(doryMemory).toEqual(undefined); // its already gone! dang default expiration | ||
// prove that if we record the memory with expires-at Infinity, it persists | ||
yield set('elephant-memory', 'something', { | ||
secondsUntilExpiration: Infinity, | ||
}); | ||
const elephantMemory = yield get('elephant-memory'); | ||
expect(elephantMemory).toEqual('something'); | ||
})); | ||
it('should accurately get keys', () => { | ||
@@ -73,0 +88,0 @@ // create the cache |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple in-memory cache, for nodejs and the browser, with time based expiration policies.", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"repository": "ehmpathy/simple-in-memory-cache", | ||
@@ -80,2 +80,3 @@ "homepage": "https://github.com/ehmpathy/simple-in-memory-cache", | ||
"ts-node": "10.9.1", | ||
"type-fns": "^1.19.0", | ||
"typescript": "4.9.4" | ||
@@ -82,0 +83,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22096
183
0
24