Application to synchronize cache between multiple processes.
Run
tsc
const { CacheClient, CacheServer } = require('./lib');
const Server = new CacheServer({ port: 3000, password: 'secret!!' });
const Client1 = new CacheClient({ port: 3000, password: 'secret!!' });
const Client2 = new CacheClient({ port: 3000, password: 'secret!!' });
(async() => {
await Client1.set('key', 'value');
(await Client2.get('key')).log(); // value
await Client2.delete('key');
(await Client1.get('key')).log(); // undefined
// You can also access the local cache directly, be careful with this
await Client1.cache.set('key', 'value');
await Client1.cache.get('key').log(); // value
(await Client2.get('key')).log(); // undefined
// You can also use JSON path notation to access nested objects
await Client1.set('key', { nested: { value: 'value' } });
(await Client2.get('key.nested.value')).log(); // value
})();
Use the <CacheClient>#set
method to set a value.
key
Is a string of the property or json path to set.
value
Is the value to set the provided path to.
ttl
Is an integer in ms for how long the data should persist.
await <CacheClient>.set(key, value, ttl);
Use the <CacheClient>#get
method to get a value.
key
Is a string of the property or json path get.
await <CacheClient>.get(key);
Use the <CacheClient>#delete
method to delete a value.
key
Is a string of the property or json path delete.
await <CacheClient>.delete(key);
All cache clients have a local cache that can be accessed directly.
This is useful for when you want to access the cache without having to make a request to the server.
Be careful with this, especially with writes as it does not refresh against all clients.
<CacheClient>.cache.set(key, value);
<CacheClient>.cache.get(key);
<CacheClient>.cache.delete(key);