Skip to content

Commit

Permalink
docs: add Quickstart section (#34)
Browse files Browse the repository at this point in the history
* docs: add Quickstart section

* docs: use `js` example

* docs: add up-to-date recommendation

* docs: improve `evict` documentation
  • Loading branch information
wellwelwel authored Aug 28, 2024
1 parent dba9083 commit 61afeb4
Showing 1 changed file with 38 additions and 2 deletions.
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

0 comments on commit 61afeb4

Please sign in to comment.