Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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.
Install with npm:
$ npm install --save cache-base
const Cache = require('cache-base');
Instantiate
// instantiate
const cache = new Cache();
// set values
cache.set('a', 'b');
cache.set('c.d', 'e');
// get values
console.log(cache.get('a'));
//=> 'b'
console.log(cache.get('c'));
//=> { d: 'e' }
console.log(cache);
//=> Cache { a: 'b' }
Initialize with an object
// instantiate
const cache = new Cache({ a: 'b', c: { d: 'e' } });
// get values
console.log(cache.get('a'));
//=> 'b'
console.log(cache.get('c'));
//=> { d: 'e' }
console.log(cache.get('c.d'));
//=> 'e'
console.log(cache);
//=> Cache { a: 'b' }
Inherit
class MyApp extends Cache {}
var cache = new MyApp();
cache.set('a', 'b');
console.log(cache.get('a'));
//=> 'b'
Custom namespace
Define a custom property name for storing values. By default, values are stored directly on the instance (for example, when cache.set('foo', 'bar')
is used, cache.foo
would be bar
).
const Cache = require('cache-base');
const cache = new Cache('data', { a: 'b' });
cache.set('c.d', 'e');
// get values
console.log(cache.get('a'));
//=> 'b'
console.log(cache.get('c'));
//=> { d: 'e' }
console.log(cache.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(cache);
//=> Cache { data: { a: 'b', c: { d: 'e' } } }
Params
cache
{Object}: Optionally pass an object to initialize with.Example
const cache = new Cache();
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 or an array of object path segments may be used.value
{any}returns
{Object}: Returns the instance for chaining.Events
emits
: set
with key
and value
as arguments.Example
cache.on('set', function(key, val) {
// do something when `set` is emitted
});
cache.set(key, value);
// also takes an object or array
cache.set({name: 'Halle'});
cache.set([{foo: 'bar'}, {baz: 'quux'}]);
console.log(cache);
//=> {name: 'Halle', foo: 'bar', baz: 'quux'}
Union array
to key
. Also emits set
with the key and value.
Params
key
{String|Array}: The name of the property to union. Dot-notation or an array of object path segments may be used.value
{any}returns
{Object}: Returns the instance for chaining.Example
cache.union('a.b', ['foo']);
cache.union('a.b', ['bar']);
console.log(cache.get('a'));
//=> {b: ['foo', 'bar']}
Return the value of key
. Dot notation may be used to get nested property values.
Params
key
{String|Array}: The name of the property to get. Dot-notation or an array of object path segments may be used.returns
{any}: Returns the value of key
Events
emits
: get
with key
and value
as arguments.Example
cache.set('a.b.c', 'd');
cache.get('a.b');
//=> { c: 'd' }
cache.get(['a', 'b']);
//=> { c: 'd' }
Return true if cache has a stored value for key
, false only if value is undefined
.
Params
key
{String|Array}: The name of the property to check. Dot-notation or an array of object path segments may be used.returns
{Boolean}Events
emits
: has
with key
and true or false as arguments.Example
cache.set('foo', 'bar');
cache.has('foo');
//=> true
Delete one or more properties from the instance.
Params
key
{String|Array}: The name of the property to delete. Dot-notation or an array of object path segments may be used.returns
{Object}: Returns the instance for chaining.Events
emits
: del
with the key
as the only argument.Example
cache.del(); // delete all
// or
cache.del('foo');
// or
cache.del(['foo', 'bar']);
Visit method
over the properties in the given object, or map
visit over the object-elements in an array.
Params
key
{String|Array}: The name of the method to visit. Dot-notation or an array of object path segments may be used.val
{Object|Array}: The object or array to iterate over.returns
{Object}: Returns the instance for chaining.Example
cache.clear();
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
) to get a nested value from an object. | homepage'a.b.c'
) paths. | homepageCommits | Contributor |
---|---|
57 | jonschlinkert |
2 | wtgtybhertgeghgtwtg |
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on December 17, 2017.
[2.0.0] - 2017-12-17
Changed
.namespace
was removed from the main export. Please see the readme for details.FAQs
Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.