Some time ago it became necessary to cache server responses on the client. Immediately make a reservation that I know about the browser cache, but this was not my case. Without hesitation, I began to supplement the code for loading data, so that before sending a request to the server, it was checked whether there was already a result with such a request.
For clarity, I will give an example to:
function loadData(url, fn) { ajax.get(url, function (err, result) {
and after:
var cache = {}; function loadCacheData(url, fn) { var cacheData = cache[url];
Having rewritten some more functions in this style, I became bored, and there is not much functionality for this method, for example, you cannot reset the cache. A bit “googling” (maybe googled badly), I didn’t find a library that would allow me to screw the cache to an existing code with a simple movement of my hand.
This marked the beginning of writing a simple wrapper function that would turn loadData into loadCacheData.
It should look something like this:
function loadData(url, fn) { ajax.get(url, function (err, result) {
The loadCacheData function obtained in this way should cache the result by itself, as well as provide cache management functionality.
')
After some time, it was possible to implement a more or less working version of the “cache” function. All sources are available on
github .
What this function allows you to do:
1. Reset cache.
var loadCacheData = cache(loadData); loadCacheData.clearCache();
2. Set the lifetime for the cache. The default is 1 year.
var loadCacheData = cache({ expire: 10 * 1000 }, loadData);
3. Set the maximum cache size (how many responses to keep in the cache). The default is 10.
var loadCacheData = cache({ max: 10 }, loadData);
4. Select the cache storage method. The default is "app".
app - keep the cache in memory.
local - store the cache in localStorage and is available between all pages of the site.
var loadCacheData = cache({ storage: 'local' }, loadData);
When setting “local”, you can optionally set “cacheKey” - the name of the key in localStorage where the cache is stored.
5. Set your own cache storage.
var loadCacheData = cache({ storage: new MyCacheStorage() }, loadData);
In this case, MyCacheStorage () should implement three methods:
“Get (key, fn);” - the method selects the results from the cache by key.
"Set (key, val, fn);" - the method sets the value to the cache.
“Clear (fn);” - the method resets the cache.
“Fn” is a callback function that takes the first argument “error”, the second “result” (if any) of the function.
The use of “cache-js” imposes one restriction, the functions whose result must be “cached” must obey the rule: “The last parameter must be a callback function!”.
TODO:
- Now the “cache” function is being added to the global area, it is planned to be moved to a separate
- Add cache using sessionStorage
- Global options, for example, to completely disable the cache during development
- Support for older browsers. Currently the parse / stringify methods of the JSON object are used, getItem / setItem / removeItem of the localStorage object
- !? Port to nodejs (questionable)
Examples using are available in the
repository.