Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add Quickstart section #34

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ deno add npm:lru.min

## Usage

### Quickstart

```js
import { createLRU } from 'lru.min';

const LRU = createLRU({
max: 2,
onEviction: (key, value) => {
console.log(`Key "${key}" with value "${value}" has been evicted.`);
},
});

LRU.set('A', 'My Value');
LRU.set('B', 'Other Value');
LRU.set('C', 'Another Value');

// => Key "A" with value "My Value" has been evicted.

LRU.has('B');
LRU.get('B');
LRU.delete('B');

// => Key "B" with value "Other Value" has been evicted.

LRU.clear();

// => Key "C" with value "Another Value" has been evicted.

LRU.set('D', "You're amazing 💛");
```

> For _up-to-date_ documentation, always follow the [**README.md**](https://github.com/wellwelwel/lru.min?tab=readme-ov-file#readme) in the **GitHub** repository.

### Import

#### ES Modules
Expand All @@ -63,8 +96,6 @@ const { createLRU } = require('lru.min');
> Set maximum size when creating **LRU**.

```ts
import { createLRU } from 'lru.min';

const LRU = createLRU({ max: 150_000 });
```

Expand Down Expand Up @@ -125,6 +156,11 @@ Evicts the specified number of the oldest items from the cache.
LRU.evict(1000);
```

> [!TIP]
>
> - Methods that perform eviction(s) when maximum size is reached: `set` and `resize`.
> - Methods that always perform eviction(s): `delete`, `clear`, and `evict` itself.

### Resize the cache

Resizes the cache to a new maximum size, evicting items if necessary.
Expand Down
Loading