Skip to content

JoelLefkowitz/caches

Repository files navigation

Caches

Extensible cache templates.

Review Quality

Motivation

Consider the following scenario:

  • You need to generate textures for a game renderer
  • The textures can be given unique names so they can be reused
  • Eventually each texture won't be needed when the game scene moves on

You need to store the textures in a map but you need to remove the oldest textures when adding new ones to limit the container size.

This is a sufficiently frequent scenario for a generic template: caches::StoreCache tracks the least recently used key and allows access by reference:

#include <string>
#include <caches/stores/store_cache.tpp>

class Texture() { ... }

caches::StoreCache<Texture> cache(2);

Texture texture_a;
cache.store("a", texture_a);

Texture texture_b;
cache.store("b", texture_b);

Texture texture_c;

// This pops texture_a
cache.store("c", texture_c);

cache.size() -> 2UL
cache.at("b") -> texture_b
cache.at("c") -> texture_c

Installing

You can download the sources.

Documentation

Documentation and more detailed examples are hosted on Github Pages.

Usage

FIFOCache

  • First in, first out
  • Fixed size cache with generic T elements
  • Access by value with T next() const and T last() const
FIFOCache
#include <string>
#include <caches/queues/fifo_cache.tpp>

caches::FIFOCache<std::string> cache(2);

First in, first out, with a fixed size:

cache.push("a");
cache.push("b");
cache.push("c");

cache.size() -> 2UL
cache.next() -> "b"
cache.last() -> "c"

LIFOCache

  • Last in, first out
  • Fixed size cache with generic T elements
  • Access by value with T next() const and T last() const
LIFOCache
#include <string>
#include <caches/queues/lifo_cache.tpp>

caches::LIFOCache<std::string> cache(2);

cache.push("a");
cache.push("b");
cache.push("c");

cache.size() -> 2UL
cache.next() -> "c"
cache.last() -> "b"

LRUCache

  • Pops the least recently accessed key
  • Fixed size cache of generic K: V pairs
  • Access by value with operator[](const K &key)
LRUCache
#include <string>
#include <caches/stores/lru_cache.tpp>

caches::LRUCache<std::string, int> cache(2);

cache.store("a", 1);
cache.store("b", 2);
cache.store("c", 3);

cache.size() -> 2UL
cache["b"] ->  2
cache["c"] ->  3

StoreCache

  • Pops the least recently accessed key
  • Fixed size cache of string: T pairs
  • Access by reference with T &at(const std::string &key)
StoreCache
#include <string>
#include <caches/stores/store_cache.tpp>

caches::StoreCache<int> cache(2);

cache.store("a", 1);
cache.store("b", 2);
cache.store("c", 3);

cache.size() -> 2UL
cache.at("b") -> 2
cache.at("c") -> 3

Tooling

Dependencies

To install dependencies:

yarn install
pip install .[all]
conan install .

Tests

To run tests:

scons test

Documentation

To generate the documentation locally:

scons docs

Linters

To run linters:

scons lint

Formatters

To run formatters:

scons format

Contributing

Please read this repository's Code of Conduct which outlines our collaboration standards and the Changelog for details on breaking changes that have been made.

This repository adheres to semantic versioning standards. For more information on semantic versioning visit SemVer.

Bump2version is used to version and tag changes. For example:

bump2version patch

Contributors

Remarks

Lots of love to the open source community!

Be kind to your mind Love each other It's ok to have a bad day