This module provides a class for a Cache data structure with support for limiting of values in the cache. The structure' constructor takes how many values can be stored in the cache (by default, it equals 20).
20
import RestrictedCache from 'core/cache/restricted';const cache = new RestrictedCache(3);cache.set('foo1', 'bar1');cache.set('foo2', 'bar2');cache.set('foo3', 'bar3');console.log(cache.size); // 3console.log(cache.has('foo1')); // truecache.set('foo4', 'bar4');console.log(cache.size); // 3console.log(cache.has('foo1')); // falseconsole.log(cache.has('foo4')); // true// Modify the cache' capacitycache.setCapacity(5);cache.set('foo5', 'bar5');cache.set('foo6', 'bar6');console.log(cache.size); // 5cache.setCapacity(3);console.log(cache.size); // 3console.log(cache.has('foo2')); // falseconsole.log(cache.has('foo3')); // false
See Cache.
Sets a new capacity of the cache. The method returns a map of truncated elements that the cache can't fit anymore.
import RestrictedCache from 'core/cache/restricted';// The constructor takes a number of maximum values in the cacheconst cache = new RestrictedCache(3);cache.set('foo1', 'bar1');cache.set('foo2', 'bar2');cache.set('foo3', 'bar3');// Map([['foo1', 'bar1], ['foo2', 'bar2']])console.log(cache.setCapacity(1));console.log(cache.size); // 1
core/cache/restricted
This module provides a class for a Cache data structure with support for limiting of values in the cache. The structure' constructor takes how many values can be stored in the cache (by default, it equals
20
).API
See Cache.
setCapacity
Sets a new capacity of the cache. The method returns a map of truncated elements that the cache can't fit anymore.