https://github.com/vinitshahdeo/lrucache
Implementation of LRUCache using Map and Doubly Linked List in JavaScript
https://github.com/vinitshahdeo/lrucache
doubly-linked-list lru-cache map
Last synced: 7 months ago
JSON representation
Implementation of LRUCache using Map and Doubly Linked List in JavaScript
- Host: GitHub
- URL: https://github.com/vinitshahdeo/lrucache
- Owner: vinitshahdeo
- License: mit
- Created: 2023-03-11T20:29:25.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-14T10:00:21.000Z (over 2 years ago)
- Last Synced: 2025-01-24T08:43:44.078Z (9 months ago)
- Topics: doubly-linked-list, lru-cache, map
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](#license) [](https://twitter.com/Vinit_Shahdeo)
[](https://github.com/vinitshahdeo/LRUCache)# LRU Cache
A basic implementation of LRU cache in JavaScript using Map and a doubly linked list.
- `get(key)` — Get the value of key from cache in `O(1)` time.
- `set(key, value)` — Set the key-value to the cache in `O(1)` time.
- `print()` — Print key-value present in the cache## Usage
```javascript
const cache = new LRUCache(3); // 3 is the max-size of cache
cache.set(1, 'a');
cache.set(2, 'b');
cache.set(3, 'c');/**
1 => a
2 => b
3 => c*/
cache.print();cache.get(1); //=> a
cache.set(4, 'e');/**
3 => c
1 => a
4 => e*/
cache.print();```
## Examples
Please refer to the examples:
1. [DLL.js](./examples/DLL.js) — Implementation using doubly linked list.
2. [map.js](./examples/map.js) — Implementation using map.## License
Released under [MIT](/LICENSE) by [@vinitshahdeo](https://github.com/vinitshahdeo)