Skip to content

Commit 88ae80d

Browse files
authored
Update README.md
1 parent 9f7413b commit 88ae80d

File tree

1 file changed

+190
-9
lines changed

1 file changed

+190
-9
lines changed

README.md

Lines changed: 190 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,197 @@
11
# CacheCraft [![Java CI](https://github.com/0mega28/CacheCraft/actions/workflows/ci.yml/badge.svg)](https://github.com/0mega28/CacheCraft/actions/workflows/ci.yml)
22

3+
A flexible and extensible in-memory cache implementation in Java with support for multiple eviction policies.
4+
35
## Overview
4-
**CacheCraft** is a comprehensive repository dedicated to the implementation and study of various caching mechanisms and design patterns. This project aims to provide a learning platform for developers to understand and apply different caching strategies and design patterns in real-world scenarios.
56

6-
## Objectives
7-
- **Educational Resource**: Serve as a learning tool for developers to grasp the concepts of caching and design patterns.
8-
- **Practical Implementations**: Provide hands-on examples and code implementations of various caching strategies.
9-
- **Design Patterns**: Explore how different design patterns can be applied to enhance caching mechanisms.
10-
- **Best Practices**: Highlight best practices and common pitfalls in cache design and implementation.
7+
This library provides a generic cache implementation with pluggable eviction policies. It allows developers to store key-value pairs in memory with automatic eviction of entries when the cache reaches its capacity limit.
8+
9+
## Features
10+
11+
- Generic type support for both keys and values
12+
- Multiple eviction policies:
13+
- LRU (Least Recently Used)
14+
- LFU (Least Frequently Used)
15+
- FIFO (First In First Out)
16+
- LIFO (Last In First Out)
17+
- Random
18+
- Configurable capacity
19+
- Extensible design for custom eviction policies
20+
21+
## Getting Started
22+
23+
### Creating a Cache
24+
25+
```java
26+
// Create an LRU cache with capacity of 100
27+
Cache<String, User> userCache = CacheFactory.getCache(EvictionPolicy.LRU, 100);
28+
29+
// Create an LFU cache with capacity of 50
30+
Cache<Integer, String> dataCache = CacheFactory.getCache(EvictionPolicy.LFU, 50);
31+
```
32+
33+
### Using the Cache
34+
35+
```java
36+
// Store a value
37+
userCache.put("user123", new User("John Doe"));
38+
39+
// Retrieve a value
40+
Optional<User> user = userCache.get("user123");
41+
42+
// Check cache size
43+
int cacheSize = userCache.size();
44+
```
45+
46+
## Eviction Policies
47+
48+
The library supports the following eviction policies:
49+
50+
### LRU (Least Recently Used)
51+
Evicts the least recently accessed entries first. Best for access patterns with temporal locality.
52+
53+
### LFU (Least Frequently Used)
54+
Evicts entries that are used least frequently. Good for access patterns where popularity matters more than recency.
55+
56+
### FIFO (First In First Out)
57+
Evicts the oldest entries first, operating like a queue. Simple and predictable.
58+
59+
### LIFO (Last In First Out)
60+
Evicts the newest entries first, operating like a stack.
61+
62+
### Random
63+
Evicts entries at random. Simple but can be useful in certain scenarios.
64+
65+
## Architecture
66+
67+
The project follows a clean, modular architecture:
68+
69+
- `Cache`: Main interface for cache operations
70+
- `CacheFactory`: Factory to create appropriate cache instances
71+
- `CacheImpl`: Core implementation of the Cache interface
72+
- `EvictionPolicy`: Interface for pluggable eviction strategies
73+
- `Storage`: Interface for underlying data storage
74+
75+
### Class Diagram
76+
77+
```
78+
Cache (Interface)
79+
80+
CacheImpl ──────► Storage ◄─── HashMapStorage
81+
│ ↑
82+
│ └─── ArrayListStorage
83+
84+
└───► EvictionPolicy ◄─── OrderBasedEvictionPolicy ◄─── LRUEvictionPolicy
85+
↑ ↑ └─── FIFOEvictionPolicy
86+
│ └─── LIFOEvictionPolicy
87+
│ └─── DefaultEvictionPolicy
88+
└─── LFUEvictionPolicy
89+
```
90+
91+
## Data Structures
92+
93+
The library uses the following data structures:
94+
95+
- `HashMap`: For O(1) key-value lookups
96+
- `DoublyLinkedList`: For efficient implementation of ordering-based policies
97+
- Custom data structures for frequency tracking in LFU
98+
99+
## Performance
100+
101+
The cache operations have the following time complexities:
11102

12-
## Contributions
13-
We welcome contributions! If you have ideas, improvements, or new caching strategies to add, please feel free to submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
103+
- `get`: O(1) for all policies
104+
- `put`: O(1) for all policies
105+
- Eviction: O(1) for all policies
106+
107+
## Examples
108+
109+
### LRU Cache Example
110+
111+
```java
112+
// Create an LRU cache with capacity of 3
113+
Cache<String, String> lruCache = CacheFactory.getCache(EvictionPolicy.LRU, 3);
114+
115+
// Add entries
116+
lruCache.put("key1", "value1");
117+
lruCache.put("key2", "value2");
118+
lruCache.put("key3", "value3");
119+
120+
// Access key1 (making key2 the least recently used)
121+
lruCache.get("key1");
122+
123+
// Add a new entry, which will evict key2
124+
lruCache.put("key4", "value4");
125+
126+
// key2 should be evicted
127+
assert lruCache.get("key2").isEmpty();
128+
```
129+
130+
### LFU Cache Example
131+
132+
```java
133+
// Create an LFU cache with capacity of 3
134+
Cache<Integer, Integer> lfuCache = CacheFactory.getCache(EvictionPolicy.LFU, 3);
135+
136+
// Add entries
137+
lfuCache.put(1, 10);
138+
lfuCache.put(2, 20);
139+
lfuCache.put(3, 30);
140+
141+
// Access key1 multiple times
142+
lfuCache.get(1);
143+
lfuCache.get(1);
144+
145+
// Access key2 once
146+
lfuCache.get(2);
147+
148+
// Add a new entry, which will evict key3 (least frequently used)
149+
lfuCache.put(4, 40);
150+
151+
// key3 should be evicted
152+
assert lfuCache.get(3).isEmpty();
153+
```
154+
155+
## Extension Points
156+
157+
This library is designed to be extensible. Here are some ways to extend it:
158+
159+
### Custom Eviction Policy
160+
161+
Implement the `EvictionPolicy` interface to create your own eviction strategy:
162+
163+
```java
164+
public class MyCustomEvictionPolicy<K> implements EvictionPolicy<K> {
165+
// Implement the required methods
166+
}
167+
```
168+
169+
### Custom Storage
170+
171+
You can also implement your own storage mechanism:
172+
173+
```java
174+
public class MyCustomStorage<K, V> implements Storage<K, V> {
175+
// Implement the required methods
176+
}
177+
```
178+
179+
## Testing
180+
181+
The library includes comprehensive unit tests for each eviction policy and core components:
182+
183+
- `DefaultCacheTest`: Basic cache functionality tests
184+
- `LRUCacheTest`: Tests specific to LRU eviction
185+
- `LFUCacheTest`: Tests specific to LFU eviction
186+
- `FIFOCacheTest`: Tests specific to FIFO eviction
187+
- `LIFOCacheTest`: Tests specific to LIFO eviction
188+
- `DoublyLinkedListTest`: Tests for the internal data structure
189+
190+
## Requirements
191+
192+
- Java 17 or higher
193+
- JUnit 5 for tests
14194

15195
## License
16-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
196+
197+
This project is licensed under the MIT License - see the LICENSE file for details.

0 commit comments

Comments
 (0)