{"id":28629758,"url":"https://github.com/fibjs/fib-cache","last_synced_at":"2025-06-12T12:13:30.888Z","repository":{"id":241839958,"uuid":"807873499","full_name":"fibjs/fib-cache","owner":"fibjs","description":"LRU Cache for fibjs","archived":false,"fork":false,"pushed_at":"2024-12-13T13:28:40.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-14T17:49:42.968Z","etag":null,"topics":[],"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/fibjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-30T00:12:43.000Z","updated_at":"2024-12-13T13:28:44.000Z","dependencies_parsed_at":"2024-07-10T21:09:59.954Z","dependency_job_id":null,"html_url":"https://github.com/fibjs/fib-cache","commit_stats":null,"previous_names":["fibjs/fib-cache"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fibjs/fib-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibjs","download_url":"https://codeload.github.com/fibjs/fib-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259462578,"owners_count":22861514,"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":"2025-06-12T12:13:29.890Z","updated_at":"2025-06-12T12:13:30.874Z","avatar_url":"https://github.com/fibjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LRU Cache for fibjs\n\nA high-performance, thread-safe LRU (Least Recently Used) cache implementation for fibjs with TTL support and resolver functionality.\n\n## Features\n\n- **Size-based Eviction**: Automatically removes least recently used items when cache reaches its maximum size\n- **TTL Support**: Time-based expiration for cache entries\n- **Thread Safety**: Safe for concurrent access in multi-fiber environments\n- **Resolver Function**: Automatic value resolution for cache misses\n- **Resource Management**: Efficient memory usage and automatic cleanup\n- **High Performance**: Optimized for both read and write operations\n\n## Installation\n\n```sh\nfibjs --install fib-cache\n```\n\n## Basic Usage\n\n```javascript\nconst { LRU } = require('fib-cache');\n\n// Create a cache with max 100 items and 5s TTL\nconst cache = new LRU({ \n    max: 100,\n    ttl: 5000 \n});\n\n// Basic operations\ncache.set('key1', 'value1');\nconsole.log(cache.get('key1')); // 'value1'\n\n// The item will be automatically removed after 5 seconds\ncoroutine.sleep(5000);\nconsole.log(cache.get('key1')); // undefined\n```\n\n## Advanced Usage\n\n### Using Resolver Function\n\n```javascript\nconst cache = new LRU({\n    max: 1000,\n    ttl: 3600000, // 1 hour\n    resolver: (key) =\u003e {\n        // Automatically fetch value if not in cache\n        const value = fetchDataFromDatabase(key);\n        return value;\n    }\n});\n\n// Will automatically fetch from database if not in cache\nconst value = cache.get('user:123');\n\n// Example with sleep\nconst slowCache = new LRU({\n    max: 100,\n    resolver: (key) =\u003e {\n        // Simulate slow operation\n        coroutine.sleep(100);\n        return `computed_${key}`;\n    }\n});\n\n// This will wait for resolver\nconsole.log(slowCache.get('test')); // 'computed_test'\n```\n\n### Managing Cache Size\n\n```javascript\nconst cache = new LRU({ max: 2 });\n\ncache.set('a', 1);\ncache.set('b', 2);\ncache.set('c', 3); // 'a' will be evicted\n\nconsole.log(cache.get('a')); // undefined\nconsole.log(cache.get('b')); // 2\nconsole.log(cache.get('c')); // 3\n```\n\n## API Reference\n\n### Constructor Options\n\n- `max` (number, optional): Maximum number of items in cache. Default: 0 (no limit)\n- `ttl` (number, optional): Time-to-live in milliseconds. Default: 0 (no expiration)\n- `resolver` (Function, optional): Function to resolve cache misses\n\n### Methods\n\n#### Basic Operations\n\n- `get(key, [resolver])`: Get value by key\n  - `key`: The key to look up\n  - `resolver` (optional): A one-time resolver function for this specific get operation\n- `set(key, value)`: Set value for key\n- `delete(key)`: Remove item by key\n- `has(key)`: Check if key exists\n- `clear()`: Remove all items\n\n#### Cache Information\n\n- `keys()`: Get all keys\n- `values()`: Get all values\n- `entries()`: Get all key-value pairs\n- `size`: Get current cache size\n\n## Thread Safety\n\nThe cache is designed to be thread-safe in fibjs environment:\n\n```javascript\nconst cache = new LRU({ max: 100 });\n\n// Safe for concurrent access\ncoroutine.parallel([\n    () =\u003e cache.set('key1', 'value1'),\n    () =\u003e cache.get('key1'),\n    () =\u003e cache.delete('key1')\n]);\n```\n\n## Performance Considerations\n\n- Cache operations are O(1) for get/set operations\n- Automatic cleanup of expired items during operations\n- Efficient memory usage with proper garbage collection\n- Thread-safe operations with minimal locking\n\n## Using Resolver Function\n\n```javascript\nconst cache = new LRU({\n    max: 1000,\n    ttl: 3600000, // 1 hour\n    // Default resolver\n    resolver: (key) =\u003e {\n        return fetchFromMainDB(key);\n    }\n});\n\n// Using default resolver\nconst value1 = cache.get('user:123');\n\n// Using one-time custom resolver\nconst value2 = cache.get('user:456', (key) =\u003e {\n    return fetchFromBackupDB(key);\n});\n\n// Example with different resolvers\nconst slowCache = new LRU({\n    max: 100,\n    // Default slow resolver\n    resolver: (key) =\u003e {\n        coroutine.sleep(100);\n        return `slow_${key}`;\n    }\n});\n\n// Using default slow resolver\nconsole.log(slowCache.get('test')); // 'slow_test'\n\n// Using fast one-time resolver\nconsole.log(slowCache.get('test', key =\u003e `fast_${key}`)); // 'fast_test'\n```\n\n## License\n\nMIT License\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibjs%2Ffib-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-cache/lists"}