icache
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.
Usage
$ npm install icache --save
const Cache = ; const cache = 5; cache;cache; // -> truecache;cache; // -> 'strikeentco@gmail.com'cacheclear;cache; // -> []cacheall; // -> {} cache;cache; // -> ['github']cacheall; // -> { github: { user: 'strikeentco', email: 'strikeentco@gmail.com' } }cache;cache; // -> []cacheall; // -> {}
Methods
new Cache([size])
Constructor.
Params:
- [size] (Integer) - Maximum elements number (by default = 0, i.e. no limits)
const cache = ;
.length
Returns the number of elements in a Cache.
const cache = ;cachelength; // -> 0cache;cachelength; // -> 1
.getSize()
Returns Cache size.
const cache = ;cache;
.setSize([size])
Sets Cache size.
Params:
- [size] (Integer) - Maximum elements number (by default = 0, i.e. no limits)
const cache = ;cache;
.put(key, value, [time])
Adds a new element with a specified key and value to a Cache.
Params:
- key (String) - The key of the element
- value (Mixed) - The value of the element
- [time] (Integer|Float) - Time in seconds after which the element will be removed
cache;cache;
.get(key)
Returns a specified element from a Cache.
Params:
- key (String) - The key of the element
cache;cache; // -> 'element'
.has(key)
Returns a boolean indicating whether an element with the specified key exists in a Cache or not.
Params:
- key (String) - The key of the element.
cache;cache; // -> truecache; // -> false
.keys()
Returns an array of elements keys from a Cache.
cache;cache;cache; // ['new', 'newer']
.del(key)
Removes the specified element from a Cache.
Params:
- key (String) - The key of the element.
cache; // -> truecache; // -> false
.all()
Returns an object with all Cache elements.
Order is not guaranteed. For correct order, use in combination with .keys(). Example.
cache;cacheall; // -> { 1: null, 2: null }
.clear()
Removes all elements from a Cache.
cacheall; // -> { 1: null, 2: null }cacheclearall; // -> { }
.expire(key, time)
Sets timeout after which element will be removed.
To remove timeout, set time to 0.
Params:
- key (String) - The key of the element
- time (Integer|Float) - Time in seconds after which the element will be removed
cache;cache; // will be removed after 1 secondcache; // will be removed after 5 secondscache; // will cancel timeout
Examples
Adding new method
const Cache = ; { // will return array with all Cache elements in accordance with this.keys() order return this; } const cache = ;cache;cache;cache; cacheall; // -> { 1: 'element', 2: 'element', 3: 'element' }cache; // -> [{ 2: 'element' }, { 3: 'element' }, { 1: 'element' }] cache; cacheall; // -> { 1: 'element', 3: 'element' }cache; // -> [{ 3: 'element' }, { 1: 'element' }]
Caching last 5 requests
const Cache = ;const app = ;const co = ;const get = get; const cache = 5; app; app;
License
The MIT License (MIT)
Copyright (c) 2016 Alexey Bystrov