diff --git a/README.md b/README.md index 12778f6..5234eee 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/cached.js b/lib/cached.js index 02fe1f3..b265151 100644 --- a/lib/cached.js +++ b/lib/cached.js @@ -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({