Simple caching solution in Typescript.
const cache = new Cache<string>();
// Load from cache.
// If not found, value will be generated and stored in cache.
cache.load('key_1', () => {
// Some expensive operation
return 'Lorem ipsum';
}); // Return type is string
cache.has('key_1'); // true
cache.load('key_1'); // 'Lorem ipsum'
// Load from cache without generating.
cache.load('key_2'); // Return type is string | undefined
// Save to cache.
cache.save('key_2', 'Lorem ipsum');
// Check if cache exists.
cache.has('key_2'); // true
cache.load('key_2'); // 'Lorem ipsum'
const cache = new Cache<number>();
cache.save('key', 42, {
expire: 1000 // Expire after 1 second.
});
// Works with load generator too.
cache.load('key', () => 42, { expire: 1000 });
const cache = new Cache<number>();
cache.save('key', 42, {
expire: 1000,
sliding: true // Update expiration after each load
});
const cache = new Cache<string>();
cache.save('key', "My text data", {
files: [
'file_1.txt',
'file_2.txt'
] // Expired when some file is modified or deleted.
});
const cache = new Cache<string>();
function isValid(): boolean {
//...
return true;
}
cache.save('key', "My text data", {
callbacks: [
isValid,
() => false,
] // Expired when some callback returns false.
});
Description of all classes and methods with examples will found in the documentation.
Check out other ours packages ๐ฆ!