Skip to content

Commit 872b0e0

Browse files
committed
Finalize results
1 parent 9a87d3e commit 872b0e0

152 files changed

Lines changed: 86047 additions & 24491 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

analysis/finetune.ipynb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "code",
6+
"outputs": [],
7+
"execution_count": null,
8+
"source": "",
9+
"id": "a01795f09f2683e"
10+
}
11+
],
12+
"metadata": {},
13+
"nbformat": 4,
14+
"nbformat_minor": 5
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Architecture Overview: Multi-Class Cache System
2+
3+
### Classes
4+
5+
#### CacheStore
6+
- **Purpose:** Main interface for cache operations (set, get, delete, etc.).
7+
- **Attributes:** entries, policy, max_size, stats
8+
- **Methods:** set(), get(), delete(), clear(), stats(), evict()
9+
10+
#### EvictionPolicy (Abstract)
11+
- **Purpose:** Base class for eviction strategies.
12+
- **Methods:** evict(), on_access(), on_insert()
13+
14+
#### LRUEvictionPolicy (inherits EvictionPolicy)
15+
- **Purpose:** Implements Least Recently Used eviction.
16+
- **Attributes:** access_order
17+
- **Methods:** evict(), on_access(), on_insert()
18+
19+
#### LFUEvictionPolicy (inherits EvictionPolicy)
20+
- **Purpose:** Implements Least Frequently Used eviction.
21+
- **Attributes:** frequency_map
22+
- **Methods:** evict(), on_access(), on_insert()
23+
24+
#### FIFOEvictionPolicy (inherits EvictionPolicy)
25+
- **Purpose:** Implements First-In-First-Out eviction.
26+
- **Attributes:** insertion_order
27+
- **Methods:** evict(), on_insert()
28+
29+
#### TTLManager
30+
- **Purpose:** Manages time-to-live for cache entries.
31+
- **Attributes:** ttl_map
32+
- **Methods:** is_expired(), remove_expired(), get_remaining_ttl()
33+
34+
#### CacheEntry
35+
- **Purpose:** Represents a single cache item.
36+
- **Attributes:** key, value, created_at, last_accessed, access_count, ttl
37+
38+
---
39+
40+
### (Optional) Mermaid Class Diagram
41+
42+
```mermaid
43+
classDiagram
44+
CacheStore --> EvictionPolicy
45+
EvictionPolicy <|-- LRUEvictionPolicy
46+
EvictionPolicy <|-- LFUEvictionPolicy
47+
EvictionPolicy <|-- FIFOEvictionPolicy
48+
CacheStore --> TTLManager
49+
CacheStore --> CacheEntry
50+
```
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Implement a sophisticated caching system in C++ that manages cache entries with multiple eviction policies (LRU, LFU, FIFO), TTL (Time-To-Live) support, and comprehensive statistics tracking.
2+
3+
```cpp
4+
std::string manage_cache(const std::string& operation,
5+
const std::string& key,
6+
const std::map<std::string, std::string>& metadata = {},
7+
int ttl = 0);
8+
```
9+
10+
You can reuse and retrieve the following files from azure search:
11+
12+
- cache_store.h
13+
- cache_store.cpp
14+
- eviction_policy.h
15+
- eviction_policy.cpp
16+
- ttl_manager.h
17+
- ttl_manager.cpp
18+
19+
## Core Functionality Requirements
20+
21+
Your implementation must handle:
22+
23+
### Cache Entry Management
24+
- Store key-value pairs in the cache
25+
- Support arbitrary string keys and values
26+
- Automatically generate timestamps for entries
27+
- Track access count for each entry
28+
- Track creation and last access times
29+
- Support entry expiration via TTL
30+
31+
### Cache Operations
32+
- SET: Store a value with optional TTL
33+
- GET: Retrieve a value and update access tracking
34+
- DELETE: Remove an entry from cache
35+
- CLEAR: Remove all entries from cache
36+
- INVALIDATE: Clear all cache entries
37+
- STATS: Return cache statistics
38+
39+
### Eviction Policies
40+
Implement three different eviction policies:
41+
42+
1. **LRU (Least Recently Used)**
43+
- Evict the entry that was accessed least recently
44+
- Update access order on every GET
45+
- Most memory-efficient for temporal locality
46+
47+
2. **LFU (Least Frequently Used)**
48+
- Evict the entry accessed least often
49+
- Track access frequency for each entry
50+
- Good for workloads with hot/cold data
51+
52+
3. **FIFO (First In First Out)**
53+
- Evict the oldest entry (by insertion time)
54+
- Simple and predictable eviction order
55+
- Useful for time-series or streaming data
56+
57+
Policy selection can be changed dynamically via EVICT operation.
58+
59+
### TTL Management
60+
- Register TTL (time-to-live) for entries
61+
- Automatically detect expired entries on GET
62+
- Remove expired entries from cache
63+
- Support infinite TTL (TTL = 0)
64+
- Track remaining TTL for entries
65+
- Identify all expired keys
66+
67+
### Statistics Tracking
68+
- Track total cache hits
69+
- Track total cache misses
70+
- Calculate hit rate percentage
71+
- Report current cache size
72+
- Report maximum cache size
73+
- Track access frequency per entry
74+
75+
### Hit/Miss Behavior
76+
- **HIT**: Entry found and valid (not expired)
77+
- Return: `{"status": "hit", "key": "...", "value": "...", "access_count": N}`
78+
- **MISS**: Entry not found or expired
79+
- Return: `{"status": "miss", "message": "..."}`
80+
- **SUCCESS**: Operation completed successfully
81+
- Return: `{"status": "success", "message": "..."}`
82+
- **ERROR**: Invalid input or operation failed
83+
- Return: `{"status": "error", "message": "..."}`
84+
85+
### Cache Size Management
86+
- Enforce maximum cache size (default: 100 entries)
87+
- Automatically evict entries when cache is full
88+
- Prevent duplicate keys (update existing entry)
89+
- Remove expired entries when space is needed
90+
- Report current and maximum size in statistics
91+
92+
### Metadata Support
93+
Cache entries can carry metadata:
94+
- `value`: The cache value (required for SET)
95+
- `ttl`: Time-to-live in seconds (default: 0 = never expires)
96+
- `policy`: Eviction policy (LRU, LFU, FIFO)
97+
98+
### Error Handling
99+
- Return JSON error responses with descriptive messages
100+
- Handle empty keys gracefully
101+
- Handle missing entries gracefully
102+
- Handle invalid policy names
103+
- Validate all input parameters
104+
- Provide meaningful error messages
105+
106+
### JSON Response Format
107+
108+
All operations return JSON strings:
109+
- SET: `{"status": "success", "key": "...", "cached": true, "cache_size": N}`
110+
- GET (hit): `{"status": "hit", "key": "...", "value": "...", "access_count": N}`
111+
- GET (miss): `{"status": "miss", "message": "..."}`
112+
- DELETE: `{"status": "success", "message": "Key removed", "cache_size": N}`
113+
- STATS: `{"status": "success", "cache_size": N, "max_size": N, "hit_count": N, "miss_count": N, "hit_rate": X.XX}`
114+
- CLEAR/INVALIDATE: `{"status": "success", "message": "..."}`
115+
- EVICT: `{"status": "success"/"error", "message": "..."}`
116+
117+
## Implementation Notes
118+
119+
1. Use `std::map<string, shared_ptr<CacheEntry>>` for entry storage
120+
2. Use `std::vector<string>` for tracking access order in LRU
121+
3. Use `std::map<string, int>` for frequency tracking in LFU
122+
4. Implement TTL checking on every GET operation
123+
5. Automatically evict when cache reaches max size
124+
6. Support dynamic policy changes without data loss
125+
7. Use `std::chrono` for accurate timestamp tracking
126+
8. Use smart pointers for proper memory management
127+
9. Calculate hit rate as: `100.0 * hits / (hits + misses)`
128+
10. Clamp remaining TTL to 0 if already expired
129+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Implement a sophisticated caching system in C++ that manages cache entries with multiple eviction policies (LRU, LFU, FIFO), TTL (Time-To-Live) support, and comprehensive statistics tracking.
2+
3+
```cpp
4+
std::string manage_cache(const std::string& operation,
5+
const std::string& key,
6+
const std::map<std::string, std::string>& metadata = {},
7+
int ttl = 0);
8+
```
9+
10+
You can reuse and retrieve the following files from azure search:
11+
12+
- cache_store.h
13+
- cache_store.cpp
14+
- eviction_policy.h
15+
- eviction_policy.cpp
16+
- ttl_manager.h
17+
- ttl_manager.cpp
18+
19+
## Instructions
20+
21+
You need to implement a complete cache system that allows:
22+
- Setting and getting cache entries with optional TTL
23+
- Automatic expiration of entries based on TTL
24+
- Multiple eviction policies (LRU, LFU, FIFO)
25+
- Tracking cache statistics (hit rate, miss rate, access counts)
26+
- Managing cache size with automatic eviction
27+
- Clearing and invalidating cache entries
28+
29+
The system should consist of multiple classes working together:
30+
- CacheStore: Central cache management with entry storage
31+
- EvictionPolicy: Abstract policy with LRU, LFU, and FIFO implementations
32+
- TTLManager: Manages entry expiration and time-to-live tracking
33+
- CacheEntry: Individual cache entries with metadata
34+
35+
All classes should work together seamlessly and handle edge cases properly.
36+
37+
## Implementation Requirements
38+
39+
1. Implement proper error handling with specific error messages
40+
2. Support multiple eviction policies (LRU, LFU, FIFO)
41+
3. Track cache statistics (hits, misses, hit rate, size)
42+
4. Implement TTL-based expiration for entries
43+
5. Manage cache size and evict entries when full
44+
6. Handle all edge cases (empty keys, non-existent keys, etc.)
45+
7. Preserve and return entry metadata in JSON format
46+
8. Track access counts for each entry
47+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(multi_class_cache_system)
4+
5+
# Set C++17 standard
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add compiler flags for better debugging and warnings
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O2")
11+
12+
# Include directories
13+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
14+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test)
15+
16+
# Create a static library with the core functionality
17+
add_library(multi_class_cache_system_lib STATIC
18+
multi_class_cache_system.cpp
19+
cache_store.cpp
20+
eviction_policy.cpp
21+
ttl_manager.cpp
22+
)
23+
24+
# Define test executable - this is the main target for this project
25+
if(EXERCISM_RUN_ALL_TESTS)
26+
add_executable(test_multi_class_cache_system
27+
multi_class_cache_system_test.cpp
28+
)
29+
30+
# Link the library to the test executable
31+
target_link_libraries(test_multi_class_cache_system multi_class_cache_system_lib)
32+
33+
# Custom target to run tests automatically with clean output
34+
add_custom_target(run_tests ALL
35+
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_multi_class_cache_system
36+
DEPENDS test_multi_class_cache_system
37+
COMMENT ""
38+
VERBATIM
39+
)
40+
41+
# Add post-build messages for clean output
42+
add_custom_command(TARGET test_multi_class_cache_system POST_BUILD
43+
COMMAND echo ""
44+
VERBATIM
45+
)
46+
endif()
47+

0 commit comments

Comments
 (0)