https://github.com/sozialhelden/hamster-cache
A cache with LRU, TTL, and maximal size support. (TypeScript)
https://github.com/sozialhelden/hamster-cache
cache lru ttl
Last synced: 4 months ago
JSON representation
A cache with LRU, TTL, and maximal size support. (TypeScript)
- Host: GitHub
- URL: https://github.com/sozialhelden/hamster-cache
- Owner: sozialhelden
- License: mit
- Created: 2019-07-26T13:03:13.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:24:21.000Z (over 2 years ago)
- Last Synced: 2025-06-11T19:06:36.745Z (4 months ago)
- Topics: cache, lru, ttl
- Language: TypeScript
- Homepage:
- Size: 1.07 MB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hamster Cache 🐹
- Supports TypeScript
- Can limit number of cached items
- Can evic exceeding items by least recent use ('LRU')
- Supports individual time-to-live (TTL) for single items
- Gives you meta information about cached objects, for stats generation or debugging
- Allows you to bring your own internal cache (if it supports the ES6 `Map` interface)
- Lets you define a custom function to clean up, e.g. to close file handles or open connections, when it evicts an item## Installation
```bash
npm install --save @sozialhelden/hamster-cache
#or
yarn add @sozialhelden/hamster-cache
```## Usage examples
```typescript
import Cache from '@sozialhelden/hamster-cache';const cache = new Cache({
evictExceedingItemsBy: 'lru', // or 'age'
defaultTTL: 5000,
maximalItemCount: 100,
});cache.set('key', { some: 'object' }); // Add value to the cache
// Add value to the cache, but with more information
cache.set(
'key',
{ some: 'object' },
{
ttl: 1000,
dispose() {
console.log('Object disposed!');
},
}
);cache.peek('key'); // gets the cached value without side effects
cache.has('key'); // `true` if an item exists in the cache, `false` otherwise
cache.peekWithMetaInfo('key'); // same as `peek`, but returns value with meta information
cache.get('key'); // gets a value with side effect that the cache evicts the object if expired
cache.getMetaInfo('key'); // same as `get`, but returns value with meta information
cache.evictExpiredItems(); // Do this to save memory in a setInterval call - or whenever you need it!
cache.delete('key'); // removes an item from the cache
cache.clear(); // forgets all items
```## Contributors
- [@lennerd](https://github.com/lennerd)
- [@mutaphysis](https://github.com/mutaphysis)
- [@opyh](https://github.com/opyh)Supported by
![]()