From 61afeb44ddd4537de643f337ed7e33740fd9bf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Wed, 28 Aug 2024 02:29:40 -0300 Subject: [PATCH] docs: add Quickstart section (#34) * docs: add Quickstart section * docs: use `js` example * docs: add up-to-date recommendation * docs: improve `evict` documentation --- README.md | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b35826e..eb291e4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 }); ``` @@ -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.