Creating a Cache Plugin
Creating a Cache Plugin
Cache plugins are used to cache image data so that slower or more expensive storage solutions (e.g. Amazon S3) are not touched more than necessary if a file is accessed repeatedly in a short time window.
A cache plugin must implement a get
and put
method. The speed of cache invalidation is up to the specific library to decide.
Methods
get(id)
get will be called with an image id, it will look something like this.
It should return a promise that resolves with a buffer of image data:
If there is no record that matches the key, the promise should resolve with null
.
NOTE: The only reason the promise should ever reject is if there was some sort of fetching error (e.g. Redis crashes randomly). A missing key is a normal occurrence, do not error for this.
put(id, record)
put will be called with a record that looks like the following:
It should return a promise that resolves when the record has been successfully saved, and rejects if there was some problem saving the file.
init()
Any plugin may also (optionally) provide an init
method.
If one is found, librarian will wait until the init()
promise resolves before processing requests.
This is a good way for plugins to check for the availability of external resources.
The init()
promise should resolve when the plugin is good to go,
and should reject if the plugin will never be able to function correctly.
An example of this would be invalid api keys for the S3 Storage plugin or bad connection details for the Mysql Data plugin.
Tests
Your plugin should pass the cache plugin tests.