1 |
3a515b92
|
cagy
|
# fragment-cache [![NPM version](https://img.shields.io/npm/v/fragment-cache.svg?style=flat)](https://www.npmjs.com/package/fragment-cache) [![NPM downloads](https://img.shields.io/npm/dm/fragment-cache.svg?style=flat)](https://npmjs.org/package/fragment-cache) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/fragment-cache.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/fragment-cache)
|
2 |
|
|
|
3 |
|
|
> A cache for managing namespaced sub-caches
|
4 |
|
|
|
5 |
|
|
## Install
|
6 |
|
|
|
7 |
|
|
Install with [npm](https://www.npmjs.com/):
|
8 |
|
|
|
9 |
|
|
```sh
|
10 |
|
|
$ npm install --save fragment-cache
|
11 |
|
|
```
|
12 |
|
|
|
13 |
|
|
## Usage
|
14 |
|
|
|
15 |
|
|
```js
|
16 |
|
|
var Fragment = require('fragment-cache');
|
17 |
|
|
var fragment = new Fragment();
|
18 |
|
|
```
|
19 |
|
|
|
20 |
|
|
## API
|
21 |
|
|
|
22 |
|
|
### [FragmentCache](index.js#L24)
|
23 |
|
|
|
24 |
|
|
Create a new `FragmentCache` with an optional object to use for `caches`.
|
25 |
|
|
|
26 |
|
|
**Example**
|
27 |
|
|
|
28 |
|
|
```js
|
29 |
|
|
var fragment = new FragmentCache();
|
30 |
|
|
```
|
31 |
|
|
|
32 |
|
|
**Params**
|
33 |
|
|
|
34 |
|
|
* `cacheName` **{String}**
|
35 |
|
|
* `returns` **{Object}**: Returns the [map-cache](https://github.com/jonschlinkert/map-cache) instance.
|
36 |
|
|
|
37 |
|
|
### [.cache](index.js#L49)
|
38 |
|
|
|
39 |
|
|
Get cache `name` from the `fragment.caches` object. Creates a new `MapCache` if it doesn't already exist.
|
40 |
|
|
|
41 |
|
|
**Example**
|
42 |
|
|
|
43 |
|
|
```js
|
44 |
|
|
var cache = fragment.cache('files');
|
45 |
|
|
console.log(fragment.caches.hasOwnProperty('files'));
|
46 |
|
|
//=> true
|
47 |
|
|
```
|
48 |
|
|
|
49 |
|
|
**Params**
|
50 |
|
|
|
51 |
|
|
* `cacheName` **{String}**
|
52 |
|
|
* `returns` **{Object}**: Returns the [map-cache](https://github.com/jonschlinkert/map-cache) instance.
|
53 |
|
|
|
54 |
|
|
### [.set](index.js#L67)
|
55 |
|
|
|
56 |
|
|
Set a value for property `key` on cache `name`
|
57 |
|
|
|
58 |
|
|
**Example**
|
59 |
|
|
|
60 |
|
|
```js
|
61 |
|
|
fragment.set('files', 'somefile.js', new File({path: 'somefile.js'}));
|
62 |
|
|
```
|
63 |
|
|
|
64 |
|
|
**Params**
|
65 |
|
|
|
66 |
|
|
* `name` **{String}**
|
67 |
|
|
* `key` **{String}**: Property name to set
|
68 |
|
|
* `val` **{any}**: The value of `key`
|
69 |
|
|
* `returns` **{Object}**: The cache instance for chaining
|
70 |
|
|
|
71 |
|
|
### [.has](index.js#L93)
|
72 |
|
|
|
73 |
|
|
Returns true if a non-undefined value is set for `key` on fragment cache `name`.
|
74 |
|
|
|
75 |
|
|
**Example**
|
76 |
|
|
|
77 |
|
|
```js
|
78 |
|
|
var cache = fragment.cache('files');
|
79 |
|
|
cache.set('somefile.js');
|
80 |
|
|
|
81 |
|
|
console.log(cache.has('somefile.js'));
|
82 |
|
|
//=> true
|
83 |
|
|
|
84 |
|
|
console.log(cache.has('some-other-file.js'));
|
85 |
|
|
//=> false
|
86 |
|
|
```
|
87 |
|
|
|
88 |
|
|
**Params**
|
89 |
|
|
|
90 |
|
|
* `name` **{String}**: Cache name
|
91 |
|
|
* `key` **{String}**: Optionally specify a property to check for on cache `name`
|
92 |
|
|
* `returns` **{Boolean}**
|
93 |
|
|
|
94 |
|
|
### [.get](index.js#L115)
|
95 |
|
|
|
96 |
|
|
Get `name`, or if specified, the value of `key`. Invokes the [cache](#cache) method, so that cache `name` will be created it doesn't already exist. If `key` is not passed, the entire cache (`name`) is returned.
|
97 |
|
|
|
98 |
|
|
**Example**
|
99 |
|
|
|
100 |
|
|
```js
|
101 |
|
|
var Vinyl = require('vinyl');
|
102 |
|
|
var cache = fragment.cache('files');
|
103 |
|
|
cache.set('somefile.js', new Vinyl({path: 'somefile.js'}));
|
104 |
|
|
console.log(cache.get('somefile.js'));
|
105 |
|
|
//=> <File "somefile.js">
|
106 |
|
|
```
|
107 |
|
|
|
108 |
|
|
**Params**
|
109 |
|
|
|
110 |
|
|
* `name` **{String}**
|
111 |
|
|
* `returns` **{Object}**: Returns cache `name`, or the value of `key` if specified
|
112 |
|
|
|
113 |
|
|
## About
|
114 |
|
|
|
115 |
|
|
### Related projects
|
116 |
|
|
|
117 |
|
|
* [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/node-base/base) | [homepage](https://github.com/node-base/base "base 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`.")
|
118 |
|
|
* [map-cache](https://www.npmjs.com/package/map-cache): Basic cache object for storing key-value pairs. | [homepage](https://github.com/jonschlinkert/map-cache "Basic cache object for storing key-value pairs.")
|
119 |
|
|
|
120 |
|
|
### Contributing
|
121 |
|
|
|
122 |
|
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
123 |
|
|
|
124 |
|
|
### Building docs
|
125 |
|
|
|
126 |
|
|
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
|
127 |
|
|
|
128 |
|
|
To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
|
129 |
|
|
|
130 |
|
|
```sh
|
131 |
|
|
$ npm install -g verb verb-generate-readme && verb
|
132 |
|
|
```
|
133 |
|
|
|
134 |
|
|
### Running tests
|
135 |
|
|
|
136 |
|
|
Install dev dependencies:
|
137 |
|
|
|
138 |
|
|
```sh
|
139 |
|
|
$ npm install -d && npm test
|
140 |
|
|
```
|
141 |
|
|
|
142 |
|
|
### Author
|
143 |
|
|
|
144 |
|
|
**Jon Schlinkert**
|
145 |
|
|
|
146 |
|
|
* [github/jonschlinkert](https://github.com/jonschlinkert)
|
147 |
|
|
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
148 |
|
|
|
149 |
|
|
### License
|
150 |
|
|
|
151 |
|
|
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
|
152 |
|
|
Released under the [MIT license](https://github.com/jonschlinkert/fragment-cache/blob/master/LICENSE).
|
153 |
|
|
|
154 |
|
|
***
|
155 |
|
|
|
156 |
|
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on October 17, 2016._
|