https://github.com/partman7/rotating-cache
Quick JS/TS rotating cache
https://github.com/partman7/rotating-cache
Last synced: 7 months ago
JSON representation
Quick JS/TS rotating cache
- Host: GitHub
- URL: https://github.com/partman7/rotating-cache
- Owner: PartMan7
- Created: 2025-07-30T13:30:35.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-07-31T08:52:12.000Z (9 months ago)
- Last Synced: 2025-09-05T19:25:25.339Z (8 months ago)
- Language: TypeScript
- Size: 28.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rotating-cache
Quick-and-dirty rotating cache for JS/TS. Caches a fixed amount of entries, and is pretty fast!
## Installation
```bash
# Any of the following
npm install rotating-cache
yarn add rotating-cache
bun add rotating-cache
```
## Usage
#### TypeScript
```ts
import createCache from 'rotating-cache';
const lookup = createCache({
computation: str => str.split('').reverse().join(''),
});
lookup('abc'); // 'cba', computed
lookup('bca'); // 'acb', computed
lookup('abc'); // 'cba', cached
lookup('bca'); // 'acb', cached
```
#### JavaScript (ESM)
```js
import createCache from 'rotating-cache';
const lookup = createCache({
computation: str => str.split('').reverse().join(''),
});
```
#### JavaScript (CJS)
```js
const createCache = require('rotating-cache');
const lookup = createCache({
computation: str => str.split('').reverse().join(''),
});
```
### Caching
`createCache` accepts `hash` and `cacheSize` as props. `hash` will take the given value and calculate a unique identifier from it (defaults to `String`) while `cacheSize` is the total size of cached entries.
While caching, each entry is checked in two parallel caches. The older cache will be cycled out and replaced with the newer cache every time the newer cache is full.