{"id":18573898,"url":"https://github.com/basedwon/cache","last_synced_at":"2025-07-23T11:04:28.809Z","repository":{"id":198645289,"uuid":"701125348","full_name":"basedwon/cache","owner":"basedwon","description":"A simple and extensible caching library built on top of a plain database.","archived":false,"fork":false,"pushed_at":"2023-10-06T08:36:31.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-05T02:24:25.795Z","etag":null,"topics":["cache","lfu","lru"],"latest_commit_sha":null,"homepage":"","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/basedwon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-06T01:18:02.000Z","updated_at":"2023-10-06T08:37:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"463ee6f4-8053-4bdf-b085-b65f7b8d33a9","html_url":"https://github.com/basedwon/cache","commit_stats":null,"previous_names":["basedwon/cache"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/basedwon/cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedwon%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedwon%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedwon%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedwon%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basedwon","download_url":"https://codeload.github.com/basedwon/cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedwon%2Fcache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266665780,"owners_count":23964971,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cache","lfu","lru"],"created_at":"2024-11-06T23:13:20.350Z","updated_at":"2025-07-23T11:04:28.777Z","avatar_url":"https://github.com/basedwon.png","language":"JavaScript","readme":"# Cache\n\n[![npm](https://img.shields.io/npm/v/@plaindb/cache?style=flat\u0026logo=npm)](https://www.npmjs.com/package/@plaindb/cache)\n[![pipeline](https://gitlab.com/frenware/framework/plaindb/cache/badges/master/pipeline.svg)](https://gitlab.com/frenware/framework/plaindb/cache/-/pipelines)\n[![license](https://img.shields.io/npm/l/@plaindb/cache)](https://gitlab.com/frenware/framework/plaindb/cache/-/blob/master/LICENSE)\n[![downloads](https://img.shields.io/npm/dw/@plaindb/cache)](https://www.npmjs.com/package/@plaindb/cache) \n\n[![Gitlab](https://img.shields.io/badge/Gitlab%20-%20?logo=gitlab\u0026color=%23383a40)](https://gitlab.com/frenware/framework/plaindb/cache)\n[![Github](https://img.shields.io/badge/Github%20-%20?logo=github\u0026color=%23383a40)](https://github.com/basedwon/cache)\n[![Twitter](https://img.shields.io/badge/@basdwon%20-%20?logo=twitter\u0026color=%23383a40)](https://twitter.com/basdwon)\n[![Discord](https://img.shields.io/badge/Basedwon%20-%20?logo=discord\u0026color=%23383a40)](https://discordapp.com/users/basedwon)\n\nA simple and extensible caching library built on top of a plain database (such as key-value store). It comes with a built-in eviction strategy and can be extended easily to fit various use-cases. It supports both Least Recently Used (LRU) and Least Frequently Used (LFU) eviction strategies out-of-the-box.\n\n## Features\n\n- Support for LRU and LFU eviction strategies\n- Size-based eviction\n- Event-driven architecture\n- Pruning of cache\n- Customizable cache options\n\n## Installation\n\nInstall the package with:\n\n```bash\nnpm install @plaindb/cache\n```\n\n## Usage\n\nFirst, import the `Cache` library.\n\n```js\nimport Cache from '@plaindb/cache'\n```\nor\n```js\nconst Cache = require('@plaindb/cache')\n```\n\n## Basic Usage\n\n```js\nconst Cache = require('@plaindb/cache')\n\n// Initialize cache with storage and options\nconst myCache = new Cache(storageInstance, {\n  strategy: 'LRU',\n  maxSizeBytes: 1024,\n  maxSizeItems: 50\n})\n\n// Put data into cache\nmyCache.put('key', 'value')\n\n// Get data from cache\nconst data = await myCache.get('key')\n\n// Delete data from cache\nmyCache.del('key')\n\n// Manually prune the cache\nmyCache.prune()\n```\n\n## Examples\n\n### Using LFU Strategy\n\n```js\nconst myCache = new Cache(storageInstance, {\n  strategy: 'LFU'\n})\n```\n\n### Creating a Custom Strategy\n\n```js\nclass MyCustomStrategy extends AbstractEvictionStrategy {\n  async get(key) {\n    // Custom logic\n  }\n  // ... implement other methods\n}\n\nEvictionStrategyFactory.strategyMap['MyCustomStrategy'] = MyCustomStrategy\n```\n\n## Events\n\nCache instances are event-driven. Currently, the `prune` event is supported, which is emitted when the cache is pruned.\n\n```js\nmyCache.on('prune', (keys) =\u003e {\n  console.log(`These keys were pruned: ${keys}`)\n})\n```\n\n## Documentation\n\n- [API Reference](/docs/api.md)\n\n### Cache\n\n#### `constructor(storage, [options])`\n\n- `storage`: The storage instance where the cache will be stored.\n- `options`: An optional object for cache settings, which includes:\n  - `strategy`: Eviction strategy. Default is `'LRU'`.\n  - `maxSizeBytes`: Maximum cache size in bytes.\n  - `maxSizeItems`: Maximum number of items in cache.\n\n#### Methods\n\n- `get(key)`: Fetches the item from cache by key.\n- `put(key, value)`: Inserts or updates an item into the cache.\n- `del(key)`: Removes an item from the cache by key.\n- `prune()`: Manually prunes the cache based on the eviction strategy.\n\n### Strategies\n\nYou can create your own eviction strategy by extending `AbstractEvictionStrategy`. Implement the following methods:\n\n- `put(key, value)`\n- `get(key)`\n- `del(key)`\n- `prune()`\n\nAfter creating, register your strategy like this:\n\n```js\nEvictionStrategyFactory.strategyMap['MyStrategy'] = MyStrategy\n```\n\n## Tests\n\nIn order to run the test suite, simply clone the repository and install its dependencies:\n\n```bash\ngit clone https://gitlab.com/frenware/framework/plaindb/cache.git\ncd cache\nnpm install\n```\n\nTo run the tests:\n\n```bash\nnpm test\n```\n\n## Contributing\n\nThank you! Please see our [contributing guidelines](/docs/contributing.md) for details.\n\n## Donations\n\nIf you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!\n\n**Bitcoin (BTC):**\n```\n1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF\n```\n\n**Monero (XMR):**\n```\n46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ\n```\n\n## License\n\n@plaindb/cache is [MIT licensed](https://gitlab.com/frenware/framework/plaindb/cache/-/blob/master/LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasedwon%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasedwon%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasedwon%2Fcache/lists"}