{"id":15497510,"url":"https://github.com/bcomnes/class-cache","last_synced_at":"2025-04-22T21:44:44.273Z","repository":{"id":48119752,"uuid":"115240992","full_name":"bcomnes/class-cache","owner":"bcomnes","description":"🏫 Cache a class instance by key","archived":false,"fork":false,"pushed_at":"2020-05-25T04:07:28.000Z","size":39,"stargazers_count":7,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T01:22:45.885Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/class-cache","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-24T04:58:41.000Z","updated_at":"2020-08-27T10:54:22.000Z","dependencies_parsed_at":"2022-08-12T19:00:54.819Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/class-cache","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fclass-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fclass-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fclass-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fclass-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/class-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250330465,"owners_count":21412985,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:39:04.915Z","updated_at":"2025-04-22T21:44:44.244Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ClassCache [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nCache 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][ncc].\n\n## Usage\n\n```js\nconst ClassCache = require('class-cache')\nconst DefaultClass = require('default-class')\nconst SomeClass = require('some-class')\nconst AnotherClass = require('another-class')\nconst AThirdClass = require('a-third-class')\n\nconst c = new ClassCache({ lru: 1000 })\n\nc.register(DefaultClass, {args: ['some', {default: 'constructor-args'}]})\n\nconst a = c.get('my-instance') // Creates and returns an instance of DefaultClass\nconst b = c.get('my-instance')\nconsole.log(a === b) // true\n\nc.register({\n  'some-class': SomeClass,\n  'another-class': {class: AnotherClass, args: ['class', 'specific'], gc: instance =\u003e instance.coolToGC}\n})\n\nc.get('some-instance', 'some-class') // return new SomeClass instance\nc.get('some-instance', 'some-class') // return same instance as above\nc.get('some-instance', 'another-class') // Create AnotherClass instance and replace the SomeClass instance stored at 'some-instance'\n```\n\n- [example.js](example.js)\n\n## Installation\n```sh\n$ npm install class-cache\n```\n## API\n### `ClassCache = require('class-cache')`\nImport `ClassCache` class.\n\n### `c = new ClassCache([opts])`\nCreate a new component.\n\n`opts` include:\n\n```js\n{\n  gc: (instance) =\u003e false // a default garbage collection function\n  args: [] // Default args used for instantiating all classes,\n  lru: 0 // Enable LRU gc by setting this to an integer greater than 0\n}\n```\n\n### `c.register([typeKey = 'default'], Class, [opts])`\n\nDefine 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: \n\n```js\n{\n  gc: undefined // a typeKey specific GC function,\n  args: undefined // default arguments instance arguments\n  // These options delegate to the top level options if left un-implemented\n}\n```\n\nThis is a shortcut for defining with a typeObject:\n\n```js\nc.register({\n  typeKey: { class: Class, ...opts }\n})\n```\n\n### `c.register({ typeObject })`\n\nDefine 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.\n\n```js\nc.register({\n  default: Class, // Class with no args or gc.  Uses instance gc function.\n  baz: { class: Class, ...opts }\n})\n```\n\nTypes are `Object.assign`ed over previously registered types.\n\n### `c.unregister(...types)`\n\nPass typeKeys as arguments to un-register them.  Instances are untouched during this process. \n\n### `c.get(key, [Class || typeKey], [opts])`\n\nReturn 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.\n\nOmitting optional method arguments delegates to the next most specific option. \n\n```js\nc.get('some-key') // Return or create the 'default' Class\nc.get('some-key', {args: ['arg0', 'arg2']})\nc.get('some-key', null, {args: ['arg0', 'arg2']}) // Return the default registered class with specific args\nc.get('some-key', 'some-type', { args: ['arg0', 'arg2'] }) // Return the `some-type` class at `some-key`.\nc.get('some-key', SomeOtherClass, { args: ['arg0', 'arg2'], gc: instance =\u003e true })\n```\n\nIf `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.\n\n### `c.set(key, [Class || type], [opts])`\n\nForce 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.\n\nReturns the newly created instance.s\n\n### `c.gc()`\n\nRun 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.\n\nThis is used to clean out instances you no longer need.\n\n### `c.clear()`\n\nClear all `key` instances.  The `gc` functions for each instance will be run receiving the following signature: `(instance, key, true) =\u003e {}`.  If your instance needs to let go of resources, watch for the second argument to equal true, indicating tht the instance will be deleted.  \n\n### `c.delete(key)`\n\nDelete specific `key` instance.  Will run the `gc` function passing `true` as the second argument (`(instance, key, true) =\u003e {}`).\n\n### `c.has(key)`\n\nReturn true if `key` exists. \n\nSee examples for more details.\n\n### `c.cache`\n\nGetter that returns a snapshot of the cache for inspection.\n\n## See Also\n\n- [nanocomponent-cache][ncc]: A class-cache subclass that ships sensible defaults for [nanocomponents][nc].  If you are using this with [choo][choo], you should probably use that instead.\n- [Choo component discussions][choo-component]: There are a number of ideas on how to solve this class of problems.  See discusson here.\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/class-cache.svg?style=flat-square\n[3]: https://npmjs.org/package/class-cache\n[4]: https://img.shields.io/travis/bcomnes/class-cache/master.svg?style=flat-square\n[5]: https://travis-ci.org/bcomnes/class-cache\n[8]: http://img.shields.io/npm/dm/class-cache.svg?style=flat-square\n[9]: https://npmjs.org/package/class-cache\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[bel]: https://github.com/shama/bel\n[yoyoify]: https://github.com/shama/yo-yoify\n[md]: https://github.com/patrick-steele-idem/morphdom\n[210]: https://github.com/patrick-steele-idem/morphdom/pull/81\n[nm]: https://github.com/yoshuawuyts/nanomorph\n[ce]: https://github.com/yoshuawuyts/cache-element\n[class]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\n[isSameNode]: https://github.com/choojs/nanomorph#caching-dom-elements\n[onload]: https://github.com/shama/on-load\n[choo]: https://github.com/choojs/choo\n[nca]: https://github.com/choojs/nanocomponent-adapters\n[nc]: https://github.com/choojs/nanocomponent\n[ncc]: https://github.com/bcomnes/nanocomponent-cache\n[choo-component]: https://github.com/choojs/choo/issues/593#issuecomment-364555843\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fclass-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Fclass-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fclass-cache/lists"}