Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/freddiehaddad/lrucache
Least recently used (LRU) in-memory cache.
https://github.com/freddiehaddad/lrucache
go golang leetcode leetcode-go leetcode-golang leetcode-solution lru-algorithm lru-cache lru-eviction lru-implementation lru-replacement-algorithm lrucache
Last synced: 16 days ago
JSON representation
Least recently used (LRU) in-memory cache.
- Host: GitHub
- URL: https://github.com/freddiehaddad/lrucache
- Owner: freddiehaddad
- Created: 2024-02-16T18:50:51.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2024-02-25T01:19:06.000Z (11 months ago)
- Last Synced: 2025-01-03T03:48:42.259Z (24 days ago)
- Topics: go, golang, leetcode, leetcode-go, leetcode-golang, leetcode-solution, lru-algorithm, lru-cache, lru-eviction, lru-implementation, lru-replacement-algorithm, lrucache
- Language: Go
- Homepage: https://leetcode.com/problems/lru-cache/?envType=study-plan-v2&envId=top-interview-150
- Size: 3.91 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LRU Cache
Fully associative and in-memory least recently used (LRU) cache implementation.
The cache works by using two data structures:
- Map
- Linked ListCache entries are fetched and stored in the map using the user-defined key.
Bookkeeping
The linked list is used for constant-time tracking of the least and most
recently used entries. The head of the list serves as the least recently used
object and the tail as most recently used.For each Get operation that results in a cache hit, the element is moved to the
end of the linked list to reflect it's most recently used status.For Put operations, whether its a new entry being added or an update to an
existing element, the entry is moved to the tail of the list.## Demonstration
```text
LRUCache(3)+-----+-----+-----+
| | | |
+-----+-----+-----+Put(1,1)
+-----+-----+-----+
| | | 1,1 |
+-----+-----+-----+Put(2,2)
+-----+-----+-----+
| | 1,1 | 2,2 |
+-----+-----+-----+Put(3,3)
+-----+-----+-----+
| 1,1 | 2,2 | 3,3 |
+-----+-----+-----+Get(1)
+-----+-----+-----+
| 2,2 | 3,3 | 1,1 |
+-----+-----+-----+Put(3,4)
+-----+-----+-----+
| 2,2 | 1,1 | 3,4 |
+-----+-----+-----+Put(5,5)
+-----+-----+-----+
| 1,1 | 3,4 | 5,5 |
+-----+-----+-----+
```