A simple caching module that provides an ability to cache and invalidate your data. Unlike other modules, Mahsan supports the following features:
- Data is stored only in memory (without serialization/deserialization) - this why it's so fast.
- It has a strict invalidation policy which allows clients to invalidate cached entities easily.
- We optimize data transfer to the distributed storage in order to remain performant even on slow connections.
npm install mahsan --save
// Init
const Cache = require('mahsan');
const cache = new Cache();
// ... do something ...
await cache.set('myKey', {foo: 'bar'});
// ... do something ...
const data = await cache.get('myKey');
console.log(data); // {foo: 'bar'}
const Cache = require('mahsan');
const cache = new Cache();
async getCourses() {
// Try get data from cache.
const data = await cache.get(['courses', 'lecturers']);
if (data) return data;
const dbResult = await sql(`SELECT c.name AS name, l.name AS lecturer
FROM courses AS c
LEFT JOIN lecturers AS l ON l.id = c.lecturer_id;`);
// Save all courses names and related lecturers names in cache (in current NodeJs instance).
await cache.set(['courses', 'lecturers'], dbResult);
return dbResult;
}
async getLecturers() {
// Try get data from cache.
const data = await cache.get(['lecturers']);
if (data) return data;
const dbResult = await sql(`SELECT * FROM lecturers;`);
// Save all lecturers in cache (in current NodeJs instance).
await cache.set(['lecturers'], dbResult);
return dbResult;
}
async deleteCourse(id) {
await sql(`DELETE FROM courses WHERE id = ?;`, [id]);
// Invalidate cache for "getCourses()" function.
await cache.invalidate('courses');
}
async deleteLecturer(id) {
await sql(`DELETE FROM lecturers WHERE id = ?;`, [id]);
// Invalidate cache for both "getCourses()" and "getLecturers()" function.
await cache.invalidate('lecturers');
}
options
(Object)ttl
(Number) is a default time to live for all.set()
calls in milliseconds.Infinity
by default.checkPeriod
(Number) is a period in milliseconds, used for deleting expired data. 5 min (5 * 60 * 1000
) by default.manager
(String|ValidityManager) is a ValidityManager instance or one predefined "InMemory" or "Redis".InMemory
by default.managerOptions
(Object) is currently necessary only for "Redis" manager.redisClient
(redis client).prefix
(String) is a prefix for all keys in Redis.ttl
(Number) is a default time to live for all Redis keys in milliseconds. 7 days (7 * 24 * 60 * 60 * 1000
) by default. This option is necessary in order not to clog up Redis.
Put the value to the cache.
keyParts
(String|Array) is a list of keys used to save the value.value
(Any) is a user data.ttl
(Number) is a time to live in milliseconds. This argument affects only local instance cache.
Get the value from the cache.
keyParts
(String|Array) is a list of keys used to retrieve a value previously saved withcache.set()
on this NodeJS instance.
Check a value availability.
keyParts
(String|Array) is a list of keys used to retrieve a value previously saved withcache.set()
on this NodeJS instance.
Delete a value from the cache (on this NodeJS instance).
keyParts
(String|Array) is a list of keys used to retrieve a value previously saved withcache.set()
on this NodeJS instance.
Clear cache on this NodeJS instance.
Invalidate keys locally (in case of Node.JS instance) or globally (in case of Redis).
keyParts
(String|Array) is a list of keys used to retrieve a value.
ValidityManager is an interface which has two methods async sign(keyParts)
and async update(keyParts)
. ValidityManager may be override (for example, with an own implementation of a custom shared storage).
value
argument always stored by reference. Don't modify it after setting/getting from cache.- mahsan (מחסן) is a storage in Hebrew.