|
| 1 | +# LRUCache4j |
| 2 | + |
| 3 | +`LRUCache4j` is a thread-safe implementation of a Least Recently Used (LRU) cache in Java. This class extends the Map |
| 4 | +interface and provides a cache mechanism that evicts the least recently used items when a specified capacity is reached. |
| 5 | +The class ensures thread safety by using a ReentrantReadWriteLock around its read and write operations. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Thread-safe**: Uses ReentrantReadWriteLock to manage concurrent access. |
| 10 | +- **Eviction policy**: Removes the least recently used item when the cache exceeds its capacity. |
| 11 | +- **Implements Map interface**: Can be used as a drop-in replacement for a Map with LRU eviction. |
| 12 | +- **Customizable capacity**: Initialize with a specified capacity to control the maximum number of entries. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Initialization |
| 17 | + |
| 18 | +To create an instance of `LRUCache4j`, simply specify the maximum number of entries it can hold: |
| 19 | + |
| 20 | +```java |
| 21 | +LRUCache4j<String, Integer> cache = new LRUCache4j<>(5); |
| 22 | +``` |
| 23 | + |
| 24 | +### Basic Operations |
| 25 | + |
| 26 | +- Put an entry: |
| 27 | + |
| 28 | +```java |
| 29 | +cache.put("key1",1); |
| 30 | +``` |
| 31 | + |
| 32 | +- Get an entry: |
| 33 | + |
| 34 | +```java |
| 35 | +Integer value = cache.get("key1"); |
| 36 | +``` |
| 37 | + |
| 38 | +- Remove an entry: |
| 39 | + |
| 40 | +```java |
| 41 | +cache.remove("key1"); |
| 42 | +``` |
| 43 | + |
| 44 | +- Check if the cache contains a key or value: |
| 45 | + |
| 46 | +```java |
| 47 | +boolean containsKey = cache.containsKey("key1"); |
| 48 | +boolean containsValue = cache.containsValue(1); |
| 49 | +``` |
| 50 | + |
| 51 | +- Clear the cache: |
| 52 | + |
| 53 | +```java |
| 54 | +cache.clear(); |
| 55 | +``` |
| 56 | + |
| 57 | +### Advanced Operations |
| 58 | + |
| 59 | +- Put if absent: |
| 60 | + |
| 61 | +```java |
| 62 | +cache.putIfAbsent("key2",2); |
| 63 | +``` |
| 64 | + |
| 65 | +- Bulk operations: |
| 66 | + |
| 67 | +```java |
| 68 | +Map<String, Integer> map = new HashMap<>(); |
| 69 | +map. |
| 70 | + |
| 71 | +put("key3",3); |
| 72 | +map. |
| 73 | + |
| 74 | +put("key4",4); |
| 75 | +cache. |
| 76 | + |
| 77 | +putAll(map); |
| 78 | +``` |
| 79 | + |
| 80 | +## Thread Safety |
| 81 | + |
| 82 | +`LRUCache4j` uses read-write locks to ensure thread-safe operations. The `readOperation` and `writeOperation` methods |
| 83 | +encapsulate the lock management for read and write operations respectively. |
| 84 | + |
| 85 | +## Example |
| 86 | + |
| 87 | +```java |
| 88 | +public class Example { |
| 89 | + public static void main(String[] args) { |
| 90 | + LRUCache4j<String, String> cache = new LRUCache4j<>(3); |
| 91 | + cache.put("a", "apple"); |
| 92 | + cache.put("b", "banana"); |
| 93 | + cache.put("c", "cherry"); |
| 94 | + |
| 95 | + System.out.println("Cache size: " + cache.size()); // Output: 3 |
| 96 | + |
| 97 | + cache.get("a"); // Access "a" to make it recently used |
| 98 | + cache.put("d", "date"); // This will evict "b" as it is the least recently used |
| 99 | + |
| 100 | + System.out.println("Cache contains 'b': " + cache.containsKey("b")); // Output: false |
| 101 | + System.out.println("Cache contains 'a': " + cache.containsKey("a")); // Output: true |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments