Introduction
AdonisJS Cache is an open source cache service provider for the AdonisJS framework with full test coverage and TypeScript support. You can view the source code on GitHub.
Currently, it only has support for a file driver but there are more drivers coming.
Installation
To install, you can use NPM.
npm i --save adonisjs-cache
Once installed, you should run the following command to generate the config file and add the provider.
node ace invoke adonisjs-cache
Usage
Checking For Existence
You can use the has
method to determine if the given key exists in the cache store. A boolean will be returned indicating if the key exists or not.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
const existsInCache: boolean = await Cache.has('key')
Retrieving Items
The get
method is used to retrieve an item from the cache store. If the item exists, we get the data stored, otherwise we get null.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
const value = await Cache.get('key')
Storing Items
To store an item in cache, we can use the set
method.
The third parameter in this function, indicates the amount of seconds you'd like to store the cache entry for.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.set('key', 'value', 10) // Store for 10 seconds.
If you'd like to store the item until you explicitly delete it, you can omit the third parameter.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.set('key', 'value') // Store until you delete the entry.
The set method will overwrite any existing data under the given key, if you'd like to store the item only if it doesn't exist, use the add
method.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.add('key', 'value', 10)
Retrieving and Storing
If you'd like to get a cache entry and also store a default value if it doesn't exist, you can use the remember
method.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.remember('key', () => {
return 'value'
}, 10) // Store for 10 seconds.
Similar to the set
method, you can omit the third parameter to store until you delete the item.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.remember('key', () => {
return 'value'
}) // Store until you explicitly delete.
Deleting Items
To delete an entry, you can use the delete
method. The method will return a boolean indicating if the deletion was completed successfully.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.delete('key')
If you'd like to remove all entries in cache, you can use the flush
method. This removes all existing cache entries.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
await Cache.flush()
Listing Keys
If you need a list of all keys currently stored in cache, you can use the keys
method to get an array of key names.
import Cache from '@ioc:EstalaPaul/AdonisJSCache'
const allKeys: string[] = await Cache.keys()