Skip to content

Commit

Permalink
docs(cached): Deprecate unnamed caches
Browse files Browse the repository at this point in the history
Name is now listed as a required parameter, but current functionality is unchanged.
Add deprecation warning for caches with non-string names. (They're probably a mistake anyway.)
  • Loading branch information
Federico De Giuli committed May 17, 2016
1 parent b137f56 commit e4bd666
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ cached('myStuff');

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

* **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:"`
* **options:**
* **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:"`
* **options:** (optional)
* **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.
* **defaults:** Defaults to apply for all cache operations. See `Cache.setDefaults`

Expand Down
9 changes: 8 additions & 1 deletion lib/cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@

var Bluebird = require('bluebird');
var _ = require('lodash');
var util = require('util');

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

var namedCaches = Object.create(null);

var getName = util.deprecate(function getName(name) {
return name || 'default';
}, 'Unnamed caches and caches with non-string names are deprecated.');

function cached(name, options) {
name = name || 'default';
if (typeof name !== 'string') {
name = getName(name);
}

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

0 comments on commit e4bd666

Please sign in to comment.