Skip to content

Commit

Permalink
[chore]: demo usage of memecache with fifo, lifo using multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
sanam2405 committed Feb 12, 2024
1 parent d915fe2 commit 2bf0278
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
30 changes: 23 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
#include "include/cache.hpp"
#include "include/fifo_cache_policy.hpp"
#include "include/lifo_cache_policy.hpp"
#include <iostream>

// alias for easy class typing
template <typename Key, typename Value>
using fifo_cache_t = typename caches::fixed_sized_cache<Key, Value, caches::FIFOCachePolicy>;
template <typename Key, typename Value>
using lifo_cache_t = typename caches::fixed_sized_cache<Key, Value, caches::LIFOCachePolicy>;

int main()
{

std::cout << "Hello Enterpret!\n";

constexpr std::size_t CACHE_SIZE = 256;
fifo_cache_t<std::string, int> cache(CACHE_SIZE);
fifo_cache_t<std::string, int> fifo_cache(CACHE_SIZE);
lifo_cache_t<std::string, int> lifo_cache(CACHE_SIZE);

cache.Put("Hello", 100);
cache.Put("world", 6996);
fifo_cache.Put("Hello", 80);
fifo_cache.Put("enterpret", 81);

std::cout << "Value for key '"
lifo_cache.Put("Backend", 40);
lifo_cache.Put("intern", 4);

std::cout << "Using FIFO Eviction Policy\n"
<< "Value for key '"
<< "Hello"
<< "': " << cache.Get("Hello") << '\n';
<< "': " << fifo_cache.Get("Hello") << '\n';
std::cout << "Value for key '"
<< "enterpret"
<< "': " << fifo_cache.Get("enterpret") << '\n';

std::cout << "Using LIFO Eviction Policy\n"
<< "Value for key '"
<< "Backend"
<< "': " << lifo_cache.Get("Backend") << '\n';
std::cout << "Value for key '"
<< "world"
<< "': " << cache.Get("world") << '\n';
<< "intern"
<< "': " << lifo_cache.Get("intern") << '\n';

return 0;
}
59 changes: 51 additions & 8 deletions mainthread.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "include/cache.hpp"
#include "include/fifo_cache_policy.hpp"
#include "include/lifo_cache_policy.hpp"
#include <iostream>
#include <mutex>
#include <thread>

// mutex to synchronize access to std::cout
std::mutex cout_mutex;

// alias for easy class typing
template <typename Key, typename Value>
using fifo_cache_t = typename caches::fixed_sized_cache<Key, Value, caches::FIFOCachePolicy>;
using fifo_cache_t = caches::fixed_sized_cache<Key, Value, caches::FIFOCachePolicy>;
template <typename Key, typename Value>
using lifo_cache_t = caches::fixed_sized_cache<Key, Value, caches::LIFOCachePolicy>;

void cache_operations(fifo_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
void cache_operations_fifo(fifo_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
{
if (insert)
{
Expand All @@ -17,7 +24,32 @@ void cache_operations(fifo_cache_t<std::string, int>& cache, const std::string&
{
try
{
std::cout << "Value for key '" << key << "': " << cache.Get(key) << '\n';
// lock cout access
std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << "Using FIFO Eviction Policy\n"
<< "Value for key '" << key << "': " << cache.Get(key) << '\n';
}
catch (const std::range_error& e)
{
std::cerr << e.what() << '\n';
}
}
}

void cache_operations_lifo(lifo_cache_t<std::string, int>& cache, const std::string& key, int value, bool insert)
{
if (insert)
{
cache.Put(key, value);
}
else
{
try
{
// lock cout access
std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << "Using LIFO Eviction Policy\n"
<< "Value for key '" << key << "': " << cache.Get(key) << '\n';
}
catch (const std::range_error& e)
{
Expand All @@ -31,19 +63,30 @@ int main()
std::cout << "Hello Enterpret!\n";

constexpr std::size_t CACHE_SIZE = 256;
fifo_cache_t<std::string, int> cache(CACHE_SIZE);
fifo_cache_t<std::string, int> fifo_cache(CACHE_SIZE);
lifo_cache_t<std::string, int> lifo_cache(CACHE_SIZE);

// creating multiple threads to perform cache operations concurrently
std::thread t1(cache_operations, std::ref(cache), "Hello", 100, true);
std::thread t2(cache_operations, std::ref(cache), "world", 6996, true);
std::thread t3(cache_operations, std::ref(cache), "Hello", 0, false);
std::thread t4(cache_operations, std::ref(cache), "world", 0, false);
std::thread t1(cache_operations_fifo, std::ref(fifo_cache), "Hello", 100, true);
std::thread t2(cache_operations_fifo, std::ref(fifo_cache), "world", 6996, true);
std::thread t3(cache_operations_fifo, std::ref(fifo_cache), "Hello", 0, false);
std::thread t4(cache_operations_fifo, std::ref(fifo_cache), "world", 0, false);

std::thread t5(cache_operations_lifo, std::ref(lifo_cache), "Hello", 200, true);
std::thread t6(cache_operations_lifo, std::ref(lifo_cache), "world", 9999, true);
std::thread t7(cache_operations_lifo, std::ref(lifo_cache), "Hello", 0, false);
std::thread t8(cache_operations_lifo, std::ref(lifo_cache), "world", 0, false);

// joining the threads and waiting for their completion
t1.join();
t2.join();
t3.join();
t4.join();

t5.join();
t6.join();
t7.join();
t8.join();

return 0;
}

0 comments on commit 2bf0278

Please sign in to comment.