mini-cache v0.1.0
mini-cache
A tiny abstract request caching class.
Manual
Installation
This module uses class
es, spreading, let
, generators etc. so depending on your Node version you may need --harmony
or --es_staging
.
npm i --save mini-cache
Example
var Cache = require('mini-cache').RequestCache;
var cacheOptions = {
defaultTimeout: 3e4, // After 30 seconds, all requests will resolve with an error object
defaultCacheTimeout: 1e3, // Keep requests in cache for 1s after the first one is resolved
renewable: true, // Renew the cache timeout with every new request
defaultRequestHandler: () => { return new Promise(r => r()) } // Will be overwritten if one is passed to the handle method
}
var cache = new Cache(cacheOptions);
var requestOptions = {
requestId: '/image.jpg', // Identify request by requestId
cacheTimeout: 2e3, // Cache for 2s after response is received
renewable: true, // Renew cache timeout after every new request with this id
requestHandler: () => { return new Promise(r => r()) }, // A promise that will resolve with a response object
responseHandler: response => { return 'ok' }, // A function that will handle the response object
requestHandlerArguments: [] // Arguments that will be passed to the requestHandler function
};
var request = cache.handle(requestOptions);
request.then(console.log); // Will log the response
Class: cache.RequestCache(options)
This creates an index to store request handlers and resolve them when the response of the first request is available.
Options:
defaultTimeout
: (Optional) Time inms
after which the requests will be resolved with an error object. By default, no timeout is set.
{ error: 'Request timed out' }
defaultCacheTimeout
: (Optional) Time inms
that it takes for a request to be deleted from the index after it has been resolved. By default, no timeout is set.renewable
: (Optional) Can betrue
orfalse
. Whether or not to renew the cache timeout on the arrival of new requests. Default:false
.defaultRequestHandler
: (Optional) Anasync function
or afunction
that returns aPromise
that will be used to handle requests and will resolve with a response object. Will be overwritten if arequestHandler
is specified inrc.handle
'soptions
.
rc.handle(options)
Will pass a request the cache object which will be handled immediately if it is the first of its kind, or will share the response of its predecessors.
Options:
requestId
: (Required) This is the request's key in the index. If norequestId
is specified, an attempt to create one based on the JSON ofrequestHandlerArguments
will be made. IfrequestHandlerArguments
is an emptyArray
orundefined
, an error is thrown.timeout
: (Optional) Time inms
after which all requests of that id will resolve with an error object as a response. By default, no timeout is set.
{ error: 'Request timed out' }
cacheTimeout
: (Optional) Time inms
that it takes for a request to be deleted from the index after it has been resolved. By default, no timeout is set.renewable
: (Optional) Can betrue
orfalse
. Whether or not to renew the cache timeout on the arrival of new requests. Default:false
.requestHandler
: (Required unlessoptions.defaultRequestHanler
is set) Anasync function
or afunction
that returns aPromise
that will be used to handle requests and will resolve with a response orbject. OverwritesdefaultRequestHandler
for that request.responseHandler
: (Required) Afunction
that will be called with aresponse
object once it is produced by the request handler.requestHandlerArguments
: (Optional) Arguments that will be passed to the requestHandler function. Default:[]
.
rc.resolve(requestId, response)
This method is used internally when a requestHandler
resolves with a response
object or a timeout
occurs. If neither functions
get to resolve the request, you can manually call .resolve(requestId, yourResponseObject)
externally.
License
(The MIT License)
Copyright (c) 2015 Kosmas Papadatos kosmas.papadatos@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.