Cache a class instance by key. Creates a new instance if the key doesn't exist, otherwise returns the cached instance. Uses the prototype chain to delegate options. Optional LRU garbage collection. Built as a general base class for nanocomponent-cache.
Usage
const ClassCache = const DefaultClass = const SomeClass = const AnotherClass = const AThirdClass = const c = lru: 1000 c const a = c // Creates and returns an instance of DefaultClassconst b = cconsole // true c c // return new SomeClass instancec // return same instance as abovec // Create AnotherClass instance and replace the SomeClass instance stored at 'some-instance'
Installation
$ npm install class-cache
API
ClassCache = require('class-cache
)
Import ClassCache
class.
c = new ClassCache([opts])
Create a new component.
opts
include:
false // a default garbage collection function args: // Default args used for instantiating all classes, lru: 0 // Enable LRU gc by setting this to an integer greater than 0
c.register([typeKey = 'default'], Class, [opts])
Define a Class
for the optional typeKey
. The default typeKey
is default
, which is used whenever a typeKey
is omitted during get
s and set
s. opts
include:
gc: undefined // a typeKey specific GC function, args: undefined // default arguments instance arguments // These options delegate to the top level options if left un-implemented
This is a shortcut for defining with a typeObject:
c
c.register({ typeObject })
Define class 'type's using a typeObject
definition. A typeObject is an object who's keys define the type name which are associated with a Class
and optionally args
and a type specific gc
function.
c
Types are Object.assign
ed over previously registered types.
c.unregister(...types)
Pass typeKeys as arguments to un-register them. Instances are untouched during this process.
c.get(key, [Class || typeKey], [opts])
Return instance of Class
or defined type
class at key
. If an instance does not yet exist at key
, it will be instantiated with args
along with a key
specific gc
function. If type
is not defined, this method will throw.
Omitting optional method arguments delegates to the next most specific option.
c // Return or create the 'default' Classcc // Return the default registered class with specific argsc // Return the `some-type` class at `some-key`.c
If key
is already instantiated, args
is ignored. Pass changing properties as subsequent calls to the returned instance. If type
or Class
changes, the key
instance is re-instantiated.
c.set(key, [Class || type], [opts])
Force instantiate the class instance at key
. Follows the same override behavior as get
. If you must change args
on a key, this is the safest way to do that.
Returns the newly created instance.s
c.gc()
Run the various gc
functions defined. For each key, only the most specific gc
function set is run. Return true
from the gc
functions to garbage collect that instance, and false
to preserve.
This is used to clean out instances you no longer need.
c.clear()
Clear all key
instances. The gc
functions for each instance will be run receiving the following signature: (instance, key, true) => {}
. If your instance needs to let go of resources, watch for the second argument to equal true, indicating tht the instance will be deleted.
c.delete(key)
Delete specific key
instance. Will run the gc
function passing true
as the second argument ((instance, key, true) => {}
).
c.has(key)
Return true if key
exists.
See examples for more details.
c.cache
Getter that returns a snapshot of the cache for inspection.
See Also
- nanocomponent-cache: A class-cache subclass that ships sensible defaults for nanocomponents. If you are using this with choo, you should probably use that instead.
- Choo component discussions: There are a number of ideas on how to solve this class of problems. See discusson here.