Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
cache-base
Advanced tools
Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
The cache-base npm package is a simple and fast key-value store for caching data. It provides an easy-to-use API for setting, getting, and managing cached data with support for namespaces and custom storage engines.
Setting and getting values
This feature allows users to store and retrieve data by key. The example shows how to set an object with user details and then retrieve it.
{
const Cache = require('cache-base');
const cache = new Cache();
cache.set('user', { name: 'John Doe', age: 30 });
console.log(cache.get('user'));
}
Using namespaces
Namespaces allow for organizing data under a specific context, avoiding key collisions and making data management clearer. The example demonstrates setting and getting data under a 'users' namespace.
{
const Cache = require('cache-base');
const usersCache = new Cache('users');
usersCache.set('user1', { name: 'John Doe', age: 30 });
console.log(usersCache.get('user1'));
}
Custom storage engines
Cache-base supports custom storage engines, allowing users to define how data is stored and retrieved. This example uses JavaScript's Map object as a custom storage engine.
{
const Cache = require('cache-base');
const customStore = new Map();
const cache = new Cache({ store: customStore });
cache.set('key', 'value');
console.log(cache.get('key'));
}
node-cache is another simple caching solution for Node.js. It offers similar functionalities to cache-base but also includes TTL (time to live) support for automatic cache invalidation, which cache-base does not provide by default.
lru-cache is a cache object that deletes the least-recently-used items. It is similar to cache-base in providing a key-value cache but is specifically optimized for scenarios where you need to limit memory usage and manage entries based on usage frequency.
Basic object cache with
get
,set
,del
, andhas
methods for node.js/javascript projects.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
(TOC generated by verb using markdown-toc)
Install with npm:
$ npm install --save cache-base
const CacheBase = require('cache-base');
const app = new CacheBase();
app.set('a.b', 'c');
console.log(app.cache.a); //=> { b: 'c' }
console.log(app.cache.a.b); //=> 'c'
console.log(app.get('a')); //=> { b: 'c' }
console.log(app.get('a.b')); //=> 'c'
More usage examples below.
Params
prop
{String|Object}: (optional) Property name to use for the cache, or the object to initialize with.cache
{Object}: (optional) An object to initialize with.Example
const app = new CacheBase();
Assign value
to key
. Also emits set
with the key and value.
Params
key
{String|Array}: The name of the property to set. Dot-notation may be used to set nested properties.value
{any}returns
{Object}: Returns the instance for chaining.Events
emits
: set
with key
and value
as arguments.Example
app.on('set', function(key, val) {
// do something when `set` is emitted
});
app.set('admin', true);
// also takes an object or an array of objects
app.set({ name: 'Brian' });
app.set([{ foo: 'bar' }, { baz: 'quux' }]);
console.log(app);
//=> { name: 'Brian', foo: 'bar', baz: 'quux' }
Return the value of key
.
Params
key
{String|Array}: The name of the property to get. Dot-notation may be used to set nested properties.returns
{any}: Returns the value of key
Events
emits
: get
with key
and value
as arguments.Example
app.set('a.b.c', 'd');
app.get('a.b');
//=> { c: 'd' }
Create a property on the cache with the given value
only if it doesn't already exist.
Params
key
{String}: Property name or object path notation.val
{any}returns
{Object}: Returns the instance for chaining.Example
console.log(app.cache); //=> {}
app.set('one', { foo: 'bar' });
app.prime('one', { a: 'b' });
app.prime('two', { c: 'd' });
console.log(app.cache.one); //=> { foo: 'bar' }
console.log(app.cache.two); //=> { c: 'd' }
Set a default value to be used when .get()
is called and the value is not defined on the cache. Returns a value from the defaults when only a key is passed.
Params
key
{String|Array}: The name of the property to set. Dot-notation may be used to set nested properties.value
{any}: (optional) The value to set on the defaults object.returns
{Object}: Returns the instance for chaining.Example
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app);
// CacheBase {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
Set an array of unique values on cache key
.
Params
key
{String|Array}: The name of the property to union. Dot-notation may be used to set nested properties.value
{any}returns
{Object}: Returns the instance for chaining.Example
app.union('a.b.c', 'foo');
app.union('a.b.c', 'bar');
app.union('a.b.c', ['bar', 'baz']);
console.log(app.get('a'));
//=> { b: { c: ['foo', 'bar', 'baz'] } }
Return true if the value of property key
is not undefined
.
Params
key
{String|Array}: The name of the property to check. Dot-notation may be used to set nested properties.returns
{Boolean}Example
app.set('foo', true);
app.set('baz', null);
app.set('bar', undefined);
app.has('foo'); //=> true
app.has('bar'); //=> true
app.has('baz'); //=> false
Returns true if the specified property is an own (not inherited) property. Similar to .has(), but returns true if the key exists, even if the value is undefined
.
Params
key
{String}returns
{Boolean}: Returns true if object key
exists. Dot-notation may be used to set nested properties.Example
app.set('a.b.c', 'd');
app.set('x', false);
app.set('y', null);
app.set('z', undefined);
app.hasOwn('a'); //=> true
app.hasOwn('b'); //=> true
app.hasOwn('c'); //=> true
app.hasOwn('a.b.c'); //=> true
app.hasOwn('x'); //=> true
app.hasOwn('y'); //=> true
app.hasOwn('z'); //=> true
app.hasOwn('lslsls'); //=> false
Delete one or more properties from the instance.
Params
key
{String|Array}: The name of the property to delete. Dot-notation may be used to set nested properties.returns
{Object}: Returns the instance for chaining.Events
emits
: del
with the key
as the only argument.Example
// setup a listener to update a property with a default
// value when it's deleted by the user
app.on('del', key => app.set(key, app.default(key)));
app.del(); // delete all properties on the cache
// or
app.del('foo');
// or an array of keys
app.del(['foo', 'bar']);
Reset the entire cache to an empty object. Note that this does not also clear the defaults
object, since you can manually do cache.defaults = {}
if you want to reset that object as well.
Example
// clear "defaults" whenever the cache is cleared
app.on('clear', key => (app.defaults = {}));
app.clear();
Visit (or map visit) the specified method (key
) over the properties in the
given object or array.
Params
key
{String|Array}: The name of the method to visit.val
{Object|Array}: The object or array to iterate over.returns
{Object}: Returns the instance for chaining.Gets an array of names of all enumerable properties on the cache.
Example
const app = new CacheBase();
app.set('user', true);
app.set('admin', false);
console.log(app.keys);
//=> ['user', 'admin']
Gets the length of keys.
Example
const app = new CacheBase();
app.set('user', true);
app.set('admin', false);
console.log(app.size);
//=> 2
Create an instance of cache-base
const app = new CacheBase();
app.set('a', 'b');
app.set('c.d', 'e');
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app);
//=> CacheBase { a: 'b' }
Initialize with an object
const app = new CacheBase({ a: 'b', c: { d: 'e' } });
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.get('c.d'));
//=> 'e'
console.log(app);
//=> CacheBase { cache: { a: 'b' } }
Inherit
class MyApp extends CacheBase {}
const app = new MyApp();
app.set('a', 'b');
app.set('c', 'd');
console.log(app.get('a'));
//=> 'b'
console.log(app);
//=> MyApp { cache: { a: 'b', c: 'd' } }
Custom namespace
Pass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the cache
property.
const CacheBase = require('cache-base');
const app = new CacheBase('data', { a: 'b' });
app.set('c.d', 'e');
// get values
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(app);
//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
'a.b.c'
) paths. | homepageCommits | Contributor |
---|---|
67 | jonschlinkert |
2 | wtgtybhertgeghgtwtg |
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on March 23, 2018.
FAQs
Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
The npm package cache-base receives a total of 11,267,266 weekly downloads. As such, cache-base popularity was classified as popular.
We found that cache-base demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.