Gathering detailed insights and metrics for cache-base
Gathering detailed insights and metrics for cache-base
Gathering detailed insights and metrics for cache-base
Gathering detailed insights and metrics for cache-base
base
Framework for rapidly creating high quality, server-side node.js applications, using plugins like building blocks
xdg-basedir
Get XDG Base Directory paths
react-router-cache-route
cache-route for react-router base on react v15+ and router v4+
base-config
base-methods plugin that adds a `config` method for mapping declarative configuration values to other 'base' methods or custom functions.
Basic object store with methods like get/set/extend/omit
npm install cache-base
Typescript
Module System
Min. Node Version
Node Version
NPM Version
97.8
Supply Chain
97.8
Quality
75.7
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
4,470,393,826
Last Day
800,948
Last Week
11,160,353
Last Month
48,440,720
Last Year
547,450,183
MIT License
57 Stars
86 Commits
19 Forks
3 Watchers
1 Branches
3 Contributors
Updated on May 13, 2025
Minified
Minified + Gzipped
Latest Version
4.0.2
Package Id
cache-base@4.0.2
Size
6.14 kB
NPM Version
8.1.0
Node Version
17.0.0
Published on
Nov 06, 2021
Cumulative downloads
Total Downloads
Last Day
-4.4%
800,948
Compared to previous day
Last Week
-7.5%
11,160,353
Compared to previous week
Last Month
2.6%
48,440,720
Compared to previous month
Last Year
-20.2%
547,450,183
Compared to previous year
8
3
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:
1$ npm install --save cache-base
1const CacheBase = require('cache-base'); 2const app = new CacheBase(); 3 4app.set('a.b', 'c'); 5 6console.log(app.cache.a); //=> { b: 'c' } 7console.log(app.cache.a.b); //=> 'c' 8 9console.log(app.get('a')); //=> { b: 'c' } 10console.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
1const 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
1app.on('set', function(key, val) { 2 // do something when `set` is emitted 3}); 4 5app.set('admin', true); 6 7// also takes an object or an array of objects 8app.set({ name: 'Brian' }); 9app.set([{ foo: 'bar' }, { baz: 'quux' }]); 10console.log(app); 11//=> { 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
1app.set('a.b.c', 'd'); 2app.get('a.b'); 3//=> { 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
1console.log(app.cache); //=> {} 2app.set('one', { foo: 'bar' }); 3app.prime('one', { a: 'b' }); 4app.prime('two', { c: 'd' }); 5console.log(app.cache.one); //=> { foo: 'bar' } 6console.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
1app.set('foo', 'xxx'); 2app.default('foo', 'one'); 3app.default('bar', 'two'); 4app.default('baz', 'three'); 5app.set('baz', 'zzz'); 6 7console.log(app.get('foo')); 8//=> 'xxx' 9 10console.log(app.get('bar')); 11//=> 'two' 12 13console.log(app.get('baz')); 14//=> 'zzz' 15 16console.log(app); 17// CacheBase { 18// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' }, 19// 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
1app.union('a.b.c', 'foo'); 2app.union('a.b.c', 'bar'); 3app.union('a.b.c', ['bar', 'baz']); 4console.log(app.get('a')); 5//=> { 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
1app.set('foo', true); 2app.set('baz', null); 3app.set('bar', undefined); 4 5app.has('foo'); //=> true 6app.has('bar'); //=> true 7app.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
1app.set('a.b.c', 'd'); 2app.set('x', false); 3app.set('y', null); 4app.set('z', undefined); 5 6app.hasOwn('a'); //=> true 7app.hasOwn('b'); //=> true 8app.hasOwn('c'); //=> true 9app.hasOwn('a.b.c'); //=> true 10app.hasOwn('x'); //=> true 11app.hasOwn('y'); //=> true 12app.hasOwn('z'); //=> true 13app.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
1// setup a listener to update a property with a default 2// value when it's deleted by the user 3app.on('del', key => app.set(key, app.default(key))); 4 5app.del(); // delete all properties on the cache 6// or 7app.del('foo'); 8// or an array of keys 9app.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
1// clear "defaults" whenever the cache is cleared 2app.on('clear', key => (app.defaults = {})); 3app.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
1const app = new CacheBase(); 2app.set('user', true); 3app.set('admin', false); 4 5console.log(app.keys); 6//=> ['user', 'admin']
Gets the length of keys.
Example
1const app = new CacheBase(); 2app.set('user', true); 3app.set('admin', false); 4 5console.log(app.size); 6//=> 2
Create an instance of cache-base
1const app = new CacheBase(); 2 3app.set('a', 'b'); 4app.set('c.d', 'e'); 5 6console.log(app.get('a')); 7//=> 'b' 8console.log(app.get('c')); 9//=> { d: 'e' } 10console.log(app); 11//=> CacheBase { a: 'b' }
Initialize with an object
1const app = new CacheBase({ a: 'b', c: { d: 'e' } }); 2 3console.log(app.get('a')); 4//=> 'b' 5console.log(app.get('c')); 6//=> { d: 'e' } 7console.log(app.get('c.d')); 8//=> 'e' 9console.log(app); 10//=> CacheBase { cache: { a: 'b' } }
Inherit
1class MyApp extends CacheBase {} 2 3const app = new MyApp(); 4app.set('a', 'b'); 5app.set('c', 'd'); 6 7console.log(app.get('a')); 8//=> 'b' 9 10console.log(app); 11//=> 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.
1const CacheBase = require('cache-base'); 2const app = new CacheBase('data', { a: 'b' }); 3app.set('c.d', 'e'); 4 5// get values 6console.log(app.get('a')); 7//=> 'b' 8console.log(app.get('c')); 9//=> { d: 'e' } 10console.log(app.data); 11//=> { a: 'b', c: { d: 'e' } } 12console.log(app); 13//=> 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:
1$ 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:
1$ 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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/20 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More