Skip to content

Cookbook: cache

Eugene Lazutkin edited this page Nov 26, 2019 · 7 revisions

In order to use io.cache make sure that it is enabled:

// optional:
// set custom properties (can be done at any time)
io.cache.storage = io.cache.makeStorage('localStorage');
// defaults are sane, so you are unlikely to modify anything

// activate the package
io.cache.attach();

See How to include for more details. See cache for the full documentation.

Opt-out from caching

By default, all GET requests will be cached. How to opt-out?

var req = io.get({
  url: 'abc',
  cache: false
});
// not cached

Clear cache

io.cache.clear();

Remove a specific cache entry

var req1 = io.get('/abc'); // from server
// now it is cached

req = io.get('/abc'); // from cache

io.cache.remove('/abc');
req = io.get('/abc'); // from server

Note: io.cache.remove('/abc') removes only '/abc'. Other cached entries like '/abc?q=a' or '/abc/xyz' are left in the cache. If you want to remove them use a wildcard.

Remove by a wildcard

var req = io.get('/abc', {q: 1}); // from server
// now it is cached

io.cache.remove('/abc?*');
var req = io.get('/abc', {q: 1}); // from server

Note: io.cache.remove('/abc*') will remove all following cached entries:

  • '/abc'
  • '/abc?q=1'
  • '/abc/xyz'
  • '/abcd'

Remove by a regular expression

var req = io.get('/abc', {q: 1}); // from server
// now it is cached

// remove all cached resources, which URL has 'abc" in it as a word
// so it will remove '/abc' and '/xyz/abc?q=1' but not '/abcd'
io.cache.remove(/\babc\b/);
var req = io.get('/abc', {q: 1}); // from server
Clone this wiki locally