
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
Note: This module stores all data in memory - remember that.
A simple queue cache which follows FIFO strategy when size is specified.
FIFO is an acronym for first in, first out, a method for organizing and manipulating a data buffer, where the oldest (first) entry, or 'head' of the queue, is processed first. Wiki
When size isn't specified it's just a cache storage.
Queue cache is perfectly fit when you need to store last n
elements, and access to them from time to time.
$ npm install icache --save
const Cache = require('icache');
const cache = new Cache(5);
cache.put('user', 'strikeentco');
cache.has('user'); // -> true
cache.put('email', 'strikeentco@gmail.com');
cache.get('email'); // -> 'strikeentco@gmail.com'
cache.clear();
cache.keys(); // -> []
cache.all(); // -> {}
cache.put('github', { user: 'strikeentco', email: 'strikeentco@gmail.com' });
cache.keys(); // -> ['github']
cache.all(); // -> { github: { user: 'strikeentco', email: 'strikeentco@gmail.com' } }
cache.del('github');
cache.keys(); // -> []
cache.all(); // -> {}
Constructor.
const cache = new Cache();
Returns the number of elements in a Cache.
const cache = new Cache();
cache.length; // -> 0
cache.put('new', 'element');
cache.length; // -> 1
Returns Cache size.
const cache = new Cache();
cache.getSize();
Sets Cache size.
const cache = new Cache();
cache.setSize(10);
Adds a new element with a specified key and value to a Cache.
cache.put('new', 'element');
cache.put('another', { element: null });
Returns a specified element from a Cache.
cache.put('new', 'element');
cache.get('new'); // -> 'element'
Returns a boolean indicating whether an element with the specified key exists in a Cache or not.
cache.put('new', 'element');
cache.has('new'); // -> true
cache.has('old'); // -> false
Returns an array of elements keys from a Cache.
cache.put('new', 'element');
cache.put('newer', 'element');
cache.keys(); // ['new', 'newer']
Removes the specified element from a Cache.
cache.put('new', 'element').has('new'); // -> true
cache.del('new').has('new'); // -> false
Returns an object with all Cache elements.
Order is not guaranteed. For correct order, use in combination with .keys(). Example.
cache.put('1', null).put('2', null);
cache.all(); // -> { 1: null, 2: null }
Removes all elements from a Cache.
cache.put('1', null).put('2', null).all(); // -> { 1: null, 2: null }
cache.clear().all(); // -> { }
Sets timeout after which element will be removed.
To remove timeout, set time to 0.
cache.put('old', 'element');
cache.expire('old', 1); // will be removed after 1 second
cache.put('new', 'element', 5); // will be removed after 5 seconds
cache.expire('new', 0); // will cancel timeout
const Cache = require('icache');
class ExtCache extends Cache {
allInOrder() { // will return array with all Cache elements in accordance with this.keys() order
return this.keys().map(key => ({ [key]: this.get(key) }));
}
}
const cache = new ExtCache();
cache.put(2, 'element');
cache.put(3, 'element');
cache.put('1', 'element');
cache.all(); // -> { 1: 'element', 2: 'element', 3: 'element' }
cache.allInOrder(); // -> [{ 2: 'element' }, { 3: 'element' }, { 1: 'element' }]
cache.setSize(2);
cache.all(); // -> { 1: 'element', 3: 'element' }
cache.allInOrder(); // -> [{ 3: 'element' }, { 1: 'element' }]
const Cache = require('icache');
const app = require('express')();
const co = require('co');
const get = require('yarl').get;
const cache = new Cache(5);
app.get('/user/:name', (req, res) => {
co(function* () {
const name = req.params.name;
if (cache.has(name)) {
return res.json(cache.get(name));
}
const data = (yield get(`https://api.github.com/users/${name}`, { json: true })).body;
cache.put(name, data);
res.json(data);
}).catch(e => res.status(500).json(e));
});
app.listen(3000);
The MIT License (MIT)
Copyright (c) 2016 Alexey Bystrov
FAQs
A simple queue cache of limited or unlimited length
We found that icache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.