Skip to content

Commit 9e48fc6

Browse files
authored
Update CacheManager.cpp
1 parent 8b4db4a commit 9e48fc6

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

src/CacheManager.cpp

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
#include "CacheManager.h"
22
#include <iostream>
33
#include <thread>
4+
#include <iomanip> // For std::fixed and std::setprecision
45

5-
CacheManager::CacheManager(int capacity)
6-
: lfu_cache(capacity), lru_cache(capacity), current_policy(LRU) {}
6+
CacheManager::CacheManager(size_t capacity)
7+
: lfu_cache(capacity), lru_cache(capacity), current_policy(LRU_P) {}
78

8-
void CacheManager::setPolicy(int policy) {
9-
std::lock_guard<std::mutex> lock(policy_mutex);
10-
current_policy = policy;
11-
}
9+
// NEW: Send GET traffic to both caches to compare performance
10+
int CacheManager::get(int key) {
11+
// Both caches process the get request to update their internal state and stats
12+
int lru_val = lru_cache.get(key);
13+
int lfu_val = lfu_cache.get(key);
1214

13-
void CacheManager::accessCache(int key, int value) {
14-
std::lock_guard<std::mutex> lock(access_mutex);
15-
if (current_policy == LFU) {
16-
lfu_cache.put(key, value);
17-
} else if (current_policy == LRU) {
18-
lru_cache.put(key, value);
15+
// Return the value from the currently active policy
16+
if (current_policy.load() == LRU_P) {
17+
return lru_val;
18+
} else {
19+
return lfu_val;
1920
}
2021
}
2122

22-
// Modified to use the stop flag
23+
// NEW: Send PUT traffic to both caches
24+
void CacheManager::put(int key, int value) {
25+
lru_cache.put(key, value);
26+
lfu_cache.put(key, value);
27+
}
28+
29+
std::string CacheManager::getCurrentPolicyName() const {
30+
return (current_policy.load() == LRU_P) ? "LRU" : "LFU";
31+
}
32+
33+
// REWRITTEN: The adaptive switching logic
2334
void CacheManager::switchPolicy(std::atomic<bool>& stop_flag) {
24-
// Loop only until the flag is set to true
2535
while (!stop_flag.load()) {
26-
std::this_thread::sleep_for(std::chrono::seconds(2)); // Shortened for test
27-
28-
// Check the flag again in case it was set while sleeping
36+
std::this_thread::sleep_for(std::chrono::seconds(5));
2937
if (stop_flag.load()) break;
3038

31-
setPolicy(current_policy == LFU ? LRU : LFU);
32-
std::cout << "Cache policy switched!" << std::endl;
39+
std::lock_guard<std::mutex> lock(policy_mutex);
40+
41+
double lru_hit_rate = lru_cache.getHitRate();
42+
double lfu_hit_rate = lfu_cache.getHitRate();
43+
44+
std::cout << "\n--- Policy Check ---" << std::endl;
45+
std::cout << std::fixed << std::setprecision(2);
46+
std::cout << "Current Policy: " << getCurrentPolicyName() << std::endl;
47+
std::cout << "LRU Hit Rate: " << lru_hit_rate * 100 << "%" << std::endl;
48+
std::cout << "LFU Hit Rate: " << lfu_hit_rate * 100 << "%" << std::endl;
49+
50+
// Adaptive switching condition
51+
// Switch if the other policy is performing better by a meaningful margin (e.g., 5%)
52+
if (current_policy == LRU_P && lfu_hit_rate > lru_hit_rate + 0.05) {
53+
current_policy = LFU_P;
54+
std::cout << "Switching to LFU policy due to better performance." << std::endl;
55+
// Reset stats after switching to start a new measurement period
56+
lru_cache.resetStats();
57+
lfu_cache.resetStats();
58+
} else if (current_policy == LFU_P && lru_hit_rate > lfu_hit_rate + 0.05) {
59+
current_policy = LRU_P;
60+
std::cout << "Switching to LRU policy due to better performance." << std::endl;
61+
lru_cache.resetStats();
62+
lfu_cache.resetStats();
63+
} else {
64+
std::cout << "No policy change needed." << std::endl;
65+
}
66+
std::cout << "--------------------\n" << std::endl;
3367
}
3468
}

0 commit comments

Comments
 (0)