1 # cache-base [![NPM version](https://img.shields.io/npm/v/cache-base.svg?style=flat)](https://www.npmjs.com/package/cache-base) [![NPM monthly downloads](https://img.shields.io/npm/dm/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![NPM total downloads](https://img.shields.io/npm/dt/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/cache-base.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/cache-base)
3 > Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
7 Install with [npm](https://www.npmjs.com/):
10 $ npm install --save cache-base
16 var Cache = require('cache-base');
19 var app = new Cache();
31 console.log(app.cache);
38 var util = require('util');
39 var Cache = require('cache-base');
44 util.inherits(MyApp, Cache);
46 var app = new MyApp();
54 Define a custom property for storing values.
57 var Cache = require('cache-base').namespace('data');
58 var app = new Cache();
60 console.log(app.data);
66 ### [namespace](index.js#L29)
68 Create a `Cache` constructor that when instantiated will store values on the given `prop`.
72 * `prop` **{String}**: The property name to use for storing values.
73 * `returns` **{Function}**: Returns a custom `Cache` constructor
78 var Cache = require('cache-base').namespace('data');
79 var cache = new Cache();
81 cache.set('foo', 'bar');
82 //=> {data: {foo: 'bar'}}
85 ### [Cache](index.js#L43)
87 Create a new `Cache`. Internally the `Cache` constructor is created using the `namespace` function, with `cache` defined as the storage object.
91 * `cache` **{Object}**: Optionally pass an object to initialize with.
96 var app = new Cache();
99 ### [.set](index.js#L84)
101 Assign `value` to `key`. Also emits `set` with the key and value.
107 * `returns` **{Object}**: Returns the instance for chaining.
111 * `emits`: `set` with `key` and `value` as arguments.
116 app.on('set', function(key, val) {
117 // do something when `set` is emitted
122 // also takes an object or array
123 app.set({name: 'Halle'});
124 app.set([{foo: 'bar'}, {baz: 'quux'}]);
126 //=> {name: 'Halle', foo: 'bar', baz: 'quux'}
129 ### [.union](index.js#L114)
131 Union `array` to `key`. Also emits `set` with the key and value.
137 * `returns` **{Object}**: Returns the instance for chaining.
142 app.union('a.b', ['foo']);
143 app.union('a.b', ['bar']);
144 console.log(app.get('a'));
145 //=> {b: ['foo', 'bar']}
148 ### [.get](index.js#L144)
150 Return the value of `key`. Dot notation may be used to get [nested property values](https://github.com/jonschlinkert/get-value).
154 * `key` **{String}**: The name of the property to get. Dot-notation may be used.
155 * `returns` **{any}**: Returns the value of `key`
159 * `emits`: `get` with `key` and `value` as arguments.
164 app.set('a.b.c', 'd');
172 ### [.has](index.js#L171)
174 Return true if app has a stored value for `key`, false only if value is `undefined`.
179 * `returns` **{Boolean}**
183 * `emits`: `has` with `key` and true or false as arguments.
188 app.set('foo', 'bar');
193 ### [.del](index.js#L199)
195 Delete one or more properties from the instance.
199 * `key` **{String|Array}**: Property name or array of property names.
200 * `returns` **{Object}**: Returns the instance for chaining.
204 * `emits`: `del` with the `key` as the only argument.
209 app.del(); // delete all
213 app.del(['foo', 'bar']);
216 ### [.clear](index.js#L218)
218 Reset the entire cache to an empty object.
226 ### [.visit](index.js#L235)
228 Visit `method` over the properties in the given object, or map
229 visit over the object-elements in an array.
233 * `method` **{String}**: The name of the `base` method to call.
234 * `val` **{Object|Array}**: The object or array to iterate over.
235 * `returns` **{Object}**: Returns the instance for chaining.
241 * [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/jonschlinkert/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods "base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.")
242 * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.")
243 * [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.")
244 * [option-cache](https://www.npmjs.com/package/option-cache): Simple API for managing options in JavaScript applications. | [homepage](https://github.com/jonschlinkert/option-cache "Simple API for managing options in JavaScript applications.")
245 * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
246 * [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value "Delete nested properties from an object using dot notation.")
250 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
254 | **Commits** | **Contributor** |
256 | 54 | [jonschlinkert](https://github.com/jonschlinkert) |
257 | 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
261 _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
263 To generate the readme, run the following command:
266 $ npm install -g verbose/verb#dev verb-generate-readme && verb
271 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:
274 $ npm install && npm test
281 * [github/jonschlinkert](https://github.com/jonschlinkert)
282 * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
286 Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
287 Released under the [MIT License](LICENSE).
291 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 22, 2017._