encache
v1.1.1
Published
overly lightweight inmemory cache for your nodejs server
Downloads
3
Readme
A lightweight , Inmemory Async Cache for your Nodejs Server.
- Quick replacement for inmemory caches like memcache or redis
- No need to setup a cache server
- 2 lines of code required to setup the cache
Documentation
1. Downloading and importing the Encache Library
npm install encache
To check the version of encache installed , use
npx encache --version
Now to import the cache object , use
//commonjs
const Cache = require('encache')
or
//esmodule import
import Cache from 'encache'
2. Creating the cache instance
by default the cache object reserves 5000 bytes as max memory limit .
User can provide custom memory limit to their cache according to performance needs .
The cache can be initiated by providing a set of options which are not compulsory.
const options = {
size : 5000,
policy : "LRU",
compression "none"
}
const store = new Cache(options)
or
//initiating cache with default values
const store = new Cache({})
3. Set the cache policy
currently five policies are available :
- FIFO -> Standard Queue based first in , first out policy . Used as the default policy.
- Lazy-TTL -> Lazily checks for the volatile keys based on their time to live.
- LRU -> Least recently used keys are evicted first.
- Random Eviction -> Randomly Evicts the keys.
- No Eviction -> Does not evict any keys if the cache memory limit is being exceeded.
- LFU -> Evicts the least frequently used keys.
store.setPolicy('FIFO')
When you set the policy to ttl , you can set the time to live for the
cache elements in milliseconds
//set the volatility to 1000 ms
store.setTTL(1000)
4. Set the cache Compression mode
The cache provides an option to compress the data to improve memory efficiency . It currently has two options
- LZ4 -> works well for partially random or non random data .
- Default ( No compression)
store.setCompression('LZ4')
5. Usage of cache methods to manage your data store
- The get method is an async method used to fetch the data stored on a key. It returns data.
const fetch = async(key) =>{
return await store.get(key)
}
- The put method is also an async method used to store the {key , value} pair in the cache. It returns nothing.
const insert = async(key ,data) =>{
await store.put(key , data)
return;
}
- The reset method allows the user to reset the cache .It flushes all the keys and resets the metrics and policy specific data.
store.reset()
- The keys method returns a list of all the available keys in the cache store
let keyList = store.keys()
console.log(keyList)
6. Metrics
The following methods are
- Hit Ratio: calculated as (number of hits / number of references). returns a floating point value
return store.hitRatio()
- Miss Ratio: calculated as 1 - hitRatio returns a floating point value
return store.missRatio()
- Memory Consumption: Returns the approximate current size of the cache in bytes returns an Integer value
return store.memoryConsumption()
- Fill Rate: The ratio of cache filled . calculated as (current memory/ max memory limit) returns a floating point value
return store.fillRate()
- Eviction Rate: The rate at which keys are evicted returns a floating point value
return store.evictionRate()
- show: It logs all the metrics in console by default .
store.show()
7. Logger
Encache provides an in-house logger to trace the cache , its performance and key movements in a file logs/app.log
.
For now the logger has been kept closely to cache's scope only and ,however in later iterations we will
make it user centric and more suitable for user's custom logging needs.
Currently , you can:
- Set the file logging level (FLL) for your cache. Options
- warn
- info
- error
- debug
- off
- all
store.logger.configureFLL(option)
8. Tests
Encache uses JEST testing framework to develop its core unit tests. The code coverage as of now is very low , but is expected to be gradually improved To run the tests , use the following command
- without coverage report
npm run test
- with coverage report
npm run test-coverage
##THANK YOU FOR READING PATIENTLY