Skip to content

Commit

Permalink
[feat]: add lifo eviction policy
Browse files Browse the repository at this point in the history
  • Loading branch information
sanam2405 committed Feb 12, 2024
1 parent 7cdcd52 commit 071c21a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions include/lifo_cache_policy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// LIFO cache policy implementation
#ifndef LIFO_CACHE_POLICY_HPP
#define LIFO_CACHE_POLICY_HPP

#include "cache_policy.hpp"
#include <list>
#include <unordered_map>

namespace caches
{
/**
* LIFO (Last in, first out) cache policy
* This policy removes the last added element when the cache is full.
* For example, if keys are added in the order A -> B -> C, and the cache is at capacity,
* the LIFO policy will choose C as the replacement candidate.
* Subsequent replacements will follow the reverse order of addition, such as B, A, and so on.
* Key - The type of key this policy works with
*/
template <typename Key> class LIFOCachePolicy : public ICachePolicy<Key>
{
public:
using lifo_iterator = typename std::list<Key>::const_iterator;

LIFOCachePolicy() = default;
~LIFOCachePolicy() = default;

// handles element insertion in the cache
void Insert(const Key& key) override
{
lifo_stack.emplace_front(key);
key_lookup[key] = lifo_stack.begin();
}

// handles request to the key-value in the cache
void Touch(const Key& key) noexcept override
{
// does not do anything in the LIFO strategy
(void)key;
}

// handles element deletion from the cache
void Erase(const Key& key) noexcept override
{
auto element = key_lookup[key];
lifo_stack.erase(element);
key_lookup.erase(key);
}

// returns the key of the replacement candidate for the FIFO policy
const Key& ReplacementCandidate() const noexcept override { return lifo_stack.front(); }

private:
std::list<Key> lifo_stack;
std::unordered_map<Key, lifo_iterator> key_lookup;
};
} // namespace caches

#endif // LIFO_CACHE_POLICY_HPP

0 comments on commit 071c21a

Please sign in to comment.