In-memory Cache Management

Components that manage caching of in-memory objects.

Cache Interface

Sneaker provides a generic and extensible cache interface that allows the use of different caching schemes by the choices of users. This is the public interface that users whom wish to use the caching mechanism will interact with.

Header file: sneaker/cache/cache_interface.h

class sneaker::cache::cache_interface<class CacheScheme, class OnInsert, class OnErase>

This class allows users to define a cache scheme, as well as handler types that are to be invoked during cache value insertions and erasures.

type key_type

Type of the keys in the cache.

type value_type

Type of the values in the cache.

cache_interface(const OnInsert &on_insert, const OnErase &on_erase)

Constructor that takes a reference of OnInsert and OnErase instances each.

bool empty() const

Determines whether the cache has any elements. Returns true if the cache has any element, false otherwise.

bool full() const

Determines whether the cache is full.

size_t size() const

Gets the number of elements in the cache.

bool find(key_type) const

Determines if the value associated with the specified key is in the cache. Returns true if there is a value associated with the key in the cache, false otherwise.

bool get(key_type, value_type&)

Retrieves the element associated with the specified key in the cache. The first argument is the key, and the second argument is a reference of the result value associated with the key. Returns true if the value is found, false otherwise.

void insert(key_type, const value_type&)

Inserts a key-value pair into the cache.

Cache Schemes

Sneaker provides abstractions of some of the most well-known caching schemes to users. These abstractions are meant to be used with the cache interface described above.

LRU Cache

This class encapsulates the logic of the Least-Recently Used caching scheme.

Header file: sneaker/cache/lru_cache.h

sneaker::cache::lru_cache<typename K, typename V, size_t N>

type key_type

Type of the keys in the cache.

type value_type

Type of the values in the cache.

size_t N

The size of the cache.

bool empty() const

Determines whether the cache has any elements. Returns true if the cache has any element, false otherwise.

bool full() const

Determines whether the cache is full.

size_t size() const

Gets the number of elements in the cache.

bool find(key_type) const

Determines if the value associated with the specified key is in the cache. Returns true if there is a value associated with the key in the cache, false otherwise.

bool get(key_type, value_type&)

Retrieves the element associated with the specified key in the cache. The first argument is the key, and the second argument is a reference of the result value associated with the key. Returns true if the value is found, false otherwise.

void next_erasure_pair(key_type **key_ptr, value_type **value_ptr)

Gets the next key-value pair to be erased when inserting a new key-value pair while the cache is full.

void insert(key_type, const value_type&)

Inserts a key-value pair into the cache.

bool erase(key_type)

Erase the element associated with the specified key in the cache. The first argument is the key associated with the value that needs to be erased. Returns true is the key-value pair is erased, false otherwise.

void clear()

Clears the cache by erasing all elements within.

Example showing using lru_cache with cache_interface:

#include <sneaker/cache/cache_interface.h>
#include <sneaker/cache/lru_cache.h>

typedef int KeyType;
typedef const char* ValueType;

struct InsertHandler
{
    bool operator()(KeyType key, const ValueType& value)
    {
        // Do something here.
    }
};

struct EraseHandler
{
    bool operator()(KeyType key, const ValueType& value)
    {
        // Do something here.
    }
};

typedef sneaker::cache::cache_interface<
  sneaker::cache::lru_cache<KeyType, ValueType, 10>, InsertHandler, EraseHandler> CacheType;

// Make use of CacheType..
CacheType cache;
cache.insert(1, "Hello world");