Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
cache-memory
Advanced tools
Node module for in memory caching.
With npm
If using a node version that supports async/await do
$ npm install cache-memory --save
Otherwise do
$ npm install cache-memory@2.1.1 --save
'use strict';
const cache = require('cache-memory')
.ttl(60)
.storeUndefinedObjects(false)
.cleanup(60)
.create({ id: 'snacks' });
function * getData() {
return { snack: 'chocolate' };
}
function standardGetAndSetExample() {
const key = 'the_key1';
let result = cache.get(key);
if (!result) {
cache.set(key, { snack: 'crisps'});
result = cache.get(key);
}
return { value: result, expiry: cache.getExpiry(key) };
}
function * generatorGetAndSetExample() {
const key = 'the_key2';
let result = yield* cache.getAndSet(key, {
generator: function * () {
return yield* getData();
}
});
return { value: result, expiry: cache.getExpiry(key) };
}
console.log(`Result 1: ${JSON.stringify(standardGetAndSetExample())}`);
console.log(`Result 2: ${JSON.stringify(generatorGetAndSetExample().next().value)}`);
Example console output of executed code:
Result 1: {"value":{"snack":"crisps"},"expiry":"2017-04-18T08:15:19.087Z"}
Result 2: {"value":{"snack":"chocolate"},"expiry":"2017-04-18T08:15:19.091Z"}
Object is used to create cacher instances, default configuration variables and scheduled object cleanup.
let cacher = require('cache-memory')
.create([options]);
Creates a new cacher instance.
clone
: (default: true
) Defines whether objects should be cloned when set in and retrieved from cache.id
: The id of the cacher (string value).storeUndefinedObjects
: (default: false) Defines whether undefined objects should be stored in memory.ttl
: (default: 0
) Defines in seconds how long an object should be stored in memory.
0
= Foreverhit
: Function called §every time an object is retrieved from cache.miss
: Function called every time an object is not from cache.added
: Function called every time an object is added to cache.removed
: Function called every time an object is removed from cache.count
: Function called every time an object is added or removed from cache.** hit, miss, added, removed functions are all called with the following object structure.
{
id: 'some-id',
key: 'some-key'
}
** count function is called with the following object structure.
{
id: 'some-id',
count: 123
}
require('cache-memory')
.clone(false);
Turns off object cloning (default true
).
require('cache-memory')
.storeUndefinedItems(true);
Allows undefined objects to be stored in cache (default false
).
require('cache-memory')
.cleanup(60);
Forces expired objects to be removed from cache every 60
seconds. By default no cleanup
is performed.
require('cache-memory')
.clear();
Clears the in memory cache of all active cache instances.
let cachers = require('cache-memory').cachers();
Gets all active cache instances.
let cacher = require('cache-memory').cacher('snacks');
Gets an active cacher by it's id.
let stats = require('cache-memory').stats();
Gets an array of stats across all active cachers.
[
{
id: '1',
stats: {
count: 5,
hits: 17123,
misses: 57,
hitRate: 0.9966821885913854
}
}
]
require('cache-memory').dispose();
Clears all in memory cacher instances and also clears the cleanup task if defined using the cleanup
function.
It's functions are defined below.
let obj = cache.get(key, [options]);
Gets an object from cache, undefined will be returned if object does not exist.
clone
: (default: true
) Defines whether objects should be cloned when set in and retrieved from cache.id
: The id of the cacher (string value).storeUndefinedObjects
: (default: false
) Defines whether undefined objects should be stored in memory.ttl
: (default: 0
) Defines in seconds how long an object should be stored in memory.
0
= Foreverhit
: Function called every time an object is retrieved from cache.miss
: Function called every time an object is not from cache.added
: Function called every time an object is added to cache.removed
: Function called every time an object is removed from cache.count
: Function called every time an object is added or removed from cache.** hit, miss, added, removed functions are all called with the following object structure.
{
id: 'some-id',
key: 'some-key'
}
** count function is called with the following object structure.
{
id: 'some-id',
count: 123
}
let expiry = cache.getExpiry(key);
Gets the expiry DateTime of an object in cache, undefined is returned if object is not found.
cache.set(key, value, [options]);
Stores an object in cache.
storeUndefinedObjects
: (default: global or instance level definition) Defines whether undefined objects should be stored in memory.ttl
: (default: global or instance level definition) Defines in seconds how long an object should be stored in memory.
0
= ForeverIf 'storeUndefinedObjects' is false. undefined, null and objects with an IsNull function that returns true will not be stored.
Version 2.* of the modules getAndSet function is an async
function
async function getter() {
return 'hello-world-' + Math.random(0, 100);
}
await cache.getAndSet(key, getter, [options]);
Gets and sets an object in cache. The getAndSet function is a generator
function so should be yielded or Promisified etc.
storeUndefinedObjects
: (default: global or instance level definition) Defines whether undefined objects should be stored in memory.ttl
: (default: global or instance level definition) Defines in seconds how long an object should be stored in memory.
0
= ForeverVersion 1.* of the modules getAndSet function is a generator
function
yield* cache.getAndSet(key, [options]);
Gets and sets an object in cache. The getAndSet function is a generator
function so should be yielded or Promisified etc.
generator
: A generator function that will be yielded to return the object value to store in cache.storeUndefinedObjects
: (default: global or instance level definition) Defines whether undefined objects should be stored in memory.ttl
: (default: global or instance level definition) Defines in seconds how long an object should be stored in memory.
0
= Forevercache.clear();
Removes all objects from the cache instance.
cache.remove(key);
Remove the object from cache.
let stats = cache.stats();
Gets the stats for the cache instance.
Example return value:
{
count: 5,
hits: 17123,
misses: 57,
hitRate: 0.9966821885913854
}
let keys = cache.keys();
Gets all keys for objects stored in the cache instance.
let options = cache.options();
Gets all configured options for a cache instance.
Example return value:
{
ttl: 60,
clone: true,
storeUndefinedObjects: false
}
(MIT)
Copyright (c) 2017 Lee Crowe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Node module for in memory caching
We found that cache-memory 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.