InMemCache is a memory based local cache.
Its primary usage is for simple storage of key/value-data whem there is no Redis, or alike, available.
Every namespace can have a separate default timeout and a limit on how many items that can be stored.
Get a key from a namespace. If the key isn't found in this namespace this function will return null.
Set a value for a key in a namespace. This value will also have a timeout set. The default value for the timeout is 300 seconds (timeout is given i milliseconds).
Set the max number of cached items in a specific namespace
Set the timeout for a namespace
Get the max number of cached items in a specific namespace
Get the timeout for a namespace
Get all namespaces currently in the cache
Jack in on the internal eventhandler.
The following events will be emitted:
- expired
- overflow
- status
- delete
- set
- clear
- clear-all
Add a one-time listener that will be triggered once.
Remove an event listener.
Remove an event listener.
Remove all event listener. If an event is given then only listener for that event will be removed.
Force an event to be emitted.
The number of elements stored.
If a namespace is given then the function will return the number of elements stored in that namespace otherwise it will return the total amount of elements.
Returns true if the key in the namespace is set. Otherwise it will return false.
Remove the data for the key in the namspace.
Remove all data for all keys in a namespace.
Remove all data for all keys in all namespaces.
Initialize the cache. This is automatically done when the cache object is created.
maxCount defined the maximum number of items within a namespace.
cleanUpInterval defines how many milliseconds there should be between clean-up intervals. The main reason for this property is to make testing smoother.
But if you for some reason wishes to make a prooper reinitailization then you'll have to call the kill function first. Otherwise it will just fall thru and do nothing.
Stops the interval handler that manages overflows and autmatic deletion of timed out values.
Returns "alive" if the interval handler is active. Otherwise it returnes "dead".
Will calculate the number of milliseconds there is in the number of days, hours, minutes and seconds given as arguments.
The default values for all arguments is 0.
There are a number of static constants declared for convinience. If the value you need isn't there then there is a function called calcTimeout where you can create your own. An alternative is to calculate the number of microseconds manually.
- timeout1Y - One year
- timeout6M - Six months
- timeout3M - Three months
- timeout2M - Two months
- timeout1M - One month
- timeout1W - One week
- timeout3D - Three days
- timeout2D - Two days
- timeout1D - One day
- timeout24h - Twentyfour hours
- timeout12h - Twelve hours
- timeout6h - Six hours
- timeout3h - Three hours
- timeout2h - Two hours
- timeout1h - One hour
- timeout30m - Thirty minutes
- timeout15m - Fifteen minutes
- timeout10m - Ten minutes
- timeout5m - Five minutes
- timeout2m - Two minutes
- timeout1m - One minute
- timeout30s - Thirty seconds
- timeout15s - Fifteen seconds
- timeout10s - Ten seconds
- timeout5s - Five seconds
- timeout2s - Two seconds
- timeout1s - One second
examples/simple.ts
import { InMemCache } from '..'
const cache = new InMemCache(5)
cache.set("test", "key1", "value1", InMemCache.timeout5s)
cache.set("test", "key2", "value2", InMemCache.timeout5s)
cache.set("test", "key3", "value3", InMemCache.timeout5s)
cache.set("test", "key4", "value4", InMemCache.timeout5s)
cache.set("test", "key5", "value5", InMemCache.timeout5s)
cache.set("test", "key6", "value6", InMemCache.timeout5s)
cache.set("test", "key7", "value7", InMemCache.timeout5s)
cache.set("test", "key8", "value8", InMemCache.timeout10s)
console.log("Size is", cache.size())
setTimeout(() => {
console.log("Size is", cache.size())
}, 2000)
setTimeout(() => {
console.log("Size is", cache.size())
}, 7500)
setTimeout(() => {
cache.kill()
}, 12500)
Output
# npm run example-simple
> inmemcache@x.x.x build
> tsc
Size is 8
Size is 5
Size is 1
#
Directly after the eight items has been added the size will be 8.
When the clean-up job is run (which is once every second) it will delete those who is set to expire first.
So the second output will say that the size is 5 which is correct since the clean-up job has been run.
The last output says that the size is 1 since the rest of the items added had a shorter timeout and has been deleted by the clean-up job.
Since there is an active interval handler in this module node will just keep running. So by calling cache.kill() you make node release its hooks and the program terminates.
nrun is a flexible and extended wrapper for npm run built in Go that can be found here.
Build the code in the dist directory.
Build the code and run the basic example.
Build the code and run the simple example.
Tests the code
Tests the code with coverage
The goal for coverage is 100% coverage without using /* istanbul ignore next */