Skip to content

Commit e7dfb5a

Browse files
authored
Create README.md
1 parent 19dabf2 commit e7dfb5a

File tree

1 file changed

+36
-0
lines changed
  • src/main/java/dataStructures/lruCache

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# LRU Cache
2+
3+
## Background
4+
5+
Assuming that software engineers develop their applications using well-structured design patterns, programs tend to reuse data and instructions they've recently accessed (temporal locality) or access data elements that are close together in memory (spatial locality).
6+
7+
### Temporal Locality
8+
9+
The Least Recently Used (LRU) Cache operates on the principle that the data most recently accessed is likely to be accessed again in the near future (temporal locality). By evicting the least recently accessed items first, LRU cache ensures that the most relevant data remains available in the cache.
10+
11+
### Data Structures
12+
13+
Implementing an LRU cache typically involves using a combination of data structures. A common approach is to use a doubly-linked list to maintain the order of items based on access recency, and a hash map to achieve constant-time access to any item in the cache. This combination effectively creates a data structure that supports the operations required for LRU cache.
14+
15+
### Cache Key
16+
17+
The hash map values are accessed through a cache key, which are unique references to the cached items in a LRU cache.
18+
19+
### Eviction
20+
21+
When the cache is full and a new item needs to be added, the eviction process is triggered. The item at the back of the list, which represents the least recently used data, is removed from both the list and the hash map. The new item is then added to the front of the list and the cache key is stored in the hash map along with its corresponding cache value. If a cached item is accessed through a read-only operation, we still move the cached item to the front of the list without any eviction.
22+
23+
## Complexity Analysis
24+
25+
**Time**: O(1) **average** complexity
26+
27+
**Space**: O(cache capacity)
28+
29+
## Notes
30+
31+
<ol>
32+
<li>Cache hit/miss ratio: A simple metric for measuring the effectiveness of the cache is the cache hit ratio. It is represented by the percentage of requests that are served from the cache without needing to access the original data store. Generally speaking, for most applications, a hit ratio of 95 - 99% is ideal.</li>
33+
<li>Outdated cached data: A cached item which is constantly accessed and remains in cache for too long may become outdated.</li>
34+
<li>Thread safety: When working with parallel computation, careful considerations have to be made when multiple threads try to access the cache at the same time. Thread-safe caching mechanisms may involve the proper use of mutex locks.</li>
35+
<li>Other caching algorithms: First-In-First-Out (FIFO) cache, Least Frequently Used (LFU) cache, Most Recently Used (MRU) cache, and Random Replacement (RR) cache.</li>
36+
</ol>

0 commit comments

Comments
 (0)