Tiny Cacher is a simple package that provides a simple API for data caching supporting multiple storage options, such as Redis, Memcached, SQLite and files, supporting basic operations, increments and TTL, allowing to store strings as well as other kind of variables that will be serialised into JSON or other formats, according to the storage strategy in use.
According with your needs, you may have to install some additional modules: if you are going to use Redis, you need to install the required module using the command
npm install redis, in a similar way to use Memcached, you need to install the required module using the command
npm install memcached, same for SQLite using the command
npm install sqlite3. Once the requirements are mets, you can install the module running this command:
npm install tiny-cacher
First you need to set up a class instance according with the storage option that you are going to use, here you are some examples:
First you need to create an instance of the class, then you can set the general setting like the namespace for cache entries and the default TTL.
let cache = ;//Set an optional namespace for entries.cache;//Set the default TTL (in seconds), by default entries have no expire.cache;
Using internal storage
You can save your data internally within the class instance or in a shared way:
//Store data within the class instance.cache;//Store data within the script but shared across class instances.cache;
//options, DB indexcache;
You can check the official repository on GitHub for more information about connection options for the Redis driver.
For more information about the driver used with Memcached check the official repository on GitHub.
For more information about the SQLite3 driver refer on the official documentation that can be found to the repository on GitHub.
Once you created the class instance and completed the connection with the storage service you can start adding items to the cache, here you are an example:
//Save one element.//key, value, overwrite, ttlcache;//Save multiple elements.//elements, overwrite, ttlcache;
In a similar way you can retrieve the value of one or multiple keys, here an example:
//key, quiet (return null instead of throwing an exception).cache;//You can pull multiple elements by using this method and passing an array of keys.//array of keys, quiet.cache;
You can check if a key exists as following:
cache;//You can check for multiple keys as well.cache;//And you can check if all the given keys exist.cache;
Then you can remove a key in this way:
cache;//You can remove multiple elements with a single call as following:cache;
If you are working with numeric values you can use increments, note that currently this feature is not available when using files as storage option:
//Pass the element key and the increment value, it can be a negative value as well, by default 1 is used.cache;//Of course you can apply increment on multiple elements.cache;//And decrement values, note that these methods internally call the methods "increment" or "incrementMulti" using a negative increment value.cache;
You can remove all stored elements using this method, alternatively you can remove all the elements stored under a given namespace:
//If you pass "true" as parameter it will remove all elements created by this class, no matter the namespace.cache;
If you switch to another storage strategy, you may want to close no more used connections, in this case you may want to run this method:
//If you pass "true" as parameters, it will close all connections, no matter the storage option in use.cache;
Considerations on TTL
TTL is supported in almost all storage options, anyway currently is not supported when using file as option. TTL is natively supported by Redis and Memcached, while is you are using another storage option you will need to use one of these technique in order to remove dead records, note that expired records will not be considered in data readings so this operation is only required whenever you need to free up some memory, here you are some usage example:
//Remove expired elements saved in shared storage.TinyCacher;//Remove expired elements saved in local storage.cache;//Remove expired elements saved in a SQLite3 database.cache;
If you are going to store your data using the "local" or the "shared" option, you could run the garbage collector methods using a Timer, here you are an example:
//Start the garbage collector in order to clean expired elements saved in shared storage.TinyCacher;//Stop it using this method.TinyCacher;//In a similar way you can run the same action on local storage.cache;//And then stop it.cache;
If you like this project and think that is useful don't be scared and feel free to contribute reporting bugs, issues or suggestions or if you feel generous, you can send a donation here.
Are you looking for the PHP version? Give a look here.