📜 ⬆️ ⬇️

cache-js. caching function

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) { //      var data = '...'; fn(null, data); }); } 

and after:
 var cache = {}; function loadCacheData(url, fn) { var cacheData = cache[url]; //  if (cacheData) { fn(null, cacheData); } else { ajax.get(url, function (err, result) { //      var data = '...'; //   cache[url] = data; fn(null, data); }); } 


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) { //      var data = '...'; fn(null, data); }); } var loadCacheData = cache(loadData); 

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:


Examples using are available in the repository.

Source: https://habr.com/ru/post/140338/


All Articles