Cacheable
Cache manager that doesn't suck.
Make the result of you async functions cacheable, automatically pickle and unpickle the data.
Manage all cache keys in one place, use a simple ._clearCache()
to purge cache.
Usage
var Redis = ;var Cacheable = ; var client = Redis;var cached = ; cachedcachedcachedcached
Wraping an async function:
// Get remote content that expires in 3600 secondsvar getUrlContent = cached
Manage cache for your models:
{ thisattributes = data} Userprototype { return thisattributes} // get user by idUser { // get the user from data base // ... var user = data ;} Userprototype { } // register the constructor firstcached // enable cache for `User.get` method// So when you call `User.get(some_id)`, it will fetch data// from cache first, when cache not found, then the original function will be called.User // '{0}' means the `arguments[0]` // You can also enable cache for an instance methodUser
API
cached.register(cls, name)
You have to register
all model constructors, so when cache is hit, the cached manager would know
how to restore the data as a proper JavaScript Object.
If your model constructor doesn't have a name, you can give a name as the second parameter, then cached will use this name.
var {}cached
Your class.prototype must have a .toJSON
method, so the cache wrapper could know how to save it to cache.
The .toJSON
will be extended by cache.register
, the output object will always have a property __cachedname
,
as is the constructor's modelName. You can add a .toObject = .toJSON
, and use .toObject
whenever you need a clean object.
If an ._unpickle
method is also defined, it will be called each time the object is restored from cache.
That is:
var item = jsonitemreturn item
Note that it would be impossible to unpickle a cache if the constructor's name was changed.
When registered, the class will have a property ._cacheKeys
and an instance would have
a method ._clearCache()
.
Userprototype { var self = this // destroy the item from database db}
cached.wrap(fn, [key], [ttl])
Wrap an standard nodejs async function(which should have a callback(err, result)
as the last parameter).
The ttl
is in seconds. If no ttl
set, the cache will never automatically expire, even it an options.ttl
is passed when you do new Cached()
.
The parameter key
is a pattern for formatting real cache keys.
The default key
is:
{_model_}:{_fn_}
{_fn_}
is the name of the function fn
. If not found, an error will throw.
So you'd better alway name your functions, like this:
User { // ...}
{_model_}
equals to {this.name}
, which is this.modelName || this.name
in the scope when the function is called.
For a class method, this usually means the name of a constructor.
Numbers like {0}
is indexes of arguments when the function is called.
%j{0}
mean the first argument value will be converted to json.
cls.enableCache(methodName, [key], [ttl])
When a cls
is registered, you can use cls.enableCache
to enable cache for class/instance methods.
If methodName
starts with a dot (.)
, it will be considered as an instance method, otherwise,
it's a class method.
/** * * List all ids * * Options: * * `limit`: limit per page * `offset`: offset * */User {} Userprototype { // get user's posts } User User // You can omit the key, cacheable will automatically use the method nameUser// KEY: '{_model_}:{id}:getTagsIds', expires in: 3600 seconds
It is strongly recommended to use this approach to add cache, instead of directly call cached.wrap
.
License
the MIT licence.