Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit cc20e08

Browse files
committed
Merge pull request #27 from fdegiuli/better-docs
Update documentation for `cached` and backends
2 parents 4b4046c + e4bd666 commit cc20e08

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,43 @@ cached('myStuff', { backend: {
8888
}});
8989
```
9090

91+
### Memory
92+
93+
Stores all the data in an in-memory object.
94+
95+
#### Example
96+
97+
```js
98+
cached('myStuff', { backend: {
99+
type: 'memory'
100+
}});
101+
```
102+
103+
### Noop
104+
105+
Doesn't store data at all. All `set` operations succeed and `get` operations behave as if the value were not found in the cache.
106+
107+
#### Examples
108+
109+
```js
110+
cached('myStuff', { backend: {
111+
type: 'noop'
112+
}});
113+
```
114+
115+
```js
116+
cached('myStuff');
117+
```
118+
119+
91120
## API
92121

93122
### cached(name: string, options) -> Cache
94123

95124
Creates a new named cache or returns a previously initialized cache.
96125

97-
* **name:** A meaningful name for what is in the cache, default: `"default"`. This will also be used as a key-prefix. If the name is `"cars"`, all keys will be prefixed with `"cars:"`
98-
* **options:**
126+
* **name:** (required) A meaningful name for what is in the cache. This will also be used as a key-prefix. If the name is `"cars"`, all keys will be prefixed with `"cars:"`
127+
* **options:** (optional)
99128
* **backend:** An object that has at least a `type` property. If no backend is configured, the cache will run in "noop"-mode, not caching anything. All other properties are forwarded to the backend, see [using different backends](#supported-backends) for which backend types exist and what options they support.
100129
* **defaults:** Defaults to apply for all cache operations. See `Cache.setDefaults`
101130

lib/cached.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@
3333

3434
var Bluebird = require('bluebird');
3535
var _ = require('lodash');
36+
var util = require('util');
3637

3738
var Cache = require('./cache');
3839

3940
var namedCaches = Object.create(null);
4041

42+
var getName = util.deprecate(function getName(name) {
43+
return name || 'default';
44+
}, 'Unnamed caches and caches with non-string names are deprecated.');
45+
4146
function cached(name, options) {
42-
name = name || 'default';
47+
if (typeof name !== 'string') {
48+
name = getName(name);
49+
}
4350

4451
if (!(name in namedCaches)) {
4552
namedCaches[name] = cached.createCache(_.extend({

0 commit comments

Comments
 (0)