Skip to content

sarihammad/cacheit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CacheIt

A high-performance, concurrency-safe in-memory cache system with TTL support, persistence, and pluggable LRU & LFU eviction policies.

Features

  • Thread-safe caching with fine-grained locking.
  • Pluggable eviction strategies (LRU, LFU via factory).
  • TTL support with background sweeper.
  • Disk persistence via JSON serialization.
  • GoogleTest suite for unit and integration testing.
  • Benchmarking with Google Benchmark.
  • Clean architecture with SOLID principles.
  • Configurable via external JSON file.

System Overview

Exposes a thread-safe key-value cache with support for expiration and disk persistence. Allows switching between eviction strategies at runtime using the factory pattern.

Architecture

graph TD

    Main[Main.cpp] -->|load config| Factory[EvictionFactory]
    Factory --> Cache[Cache LRU/LFU]
    Cache --> Entry[CacheEntry]
    Cache --> Sweeper[CacheSweeper]
    Cache --> Persistence[PersistenceManager]
    Main --> Logger[Logger]
Loading

TTL Cleanup Flow

sequenceDiagram
    participant Main
    participant Cache
    participant Sweeper
    participant Entry

    Main->>Sweeper: Start cleanup thread
    loop every interval
        Sweeper->>Cache: snapshot()
        Sweeper->>Entry: check isExpired()
        alt expired
            Sweeper->>Cache: remove(key)
        end
    end
Loading

Core Components

Cache (Interface)

  • put(key, value, ttl)
  • get(key)
  • remove(key)
  • snapshot()

LRUCache / LFUCache

  • Eviction strategy implementations
  • Backed by unordered_map + list or priority structures

CacheEntry

  • Encapsulates value, TTL, and access metadata

EvictionFactory

  • Returns the appropriate cache policy at runtime

PersistenceManager

  • Saves and loads cache state as JSON

CacheSweeper

  • Background thread that removes expired keys

Logger

  • Thread-safe logger with log level control

Design Patterns Used

  • Factory – Eviction policy switching
  • Strategy – TTL vs non-TTL handling
  • Adapter – Persistence via JSON interface
  • RAII + Scoped Locking – Safe concurrency

Build Instructions

mkdir build && cd build
cmake ..
make

How to Run

./cacheit

Uses config.json to configure:

{
  "eviction_policy": "LRU",
  "capacity": 50000,
  "default_ttl": 10,
  "sweep_interval_seconds": 5,
  "log_level": "INFO",
  "persistence_file": "cache_dump.json"
}

Benchmarks

To run cache benchmarks:

./benchmarks

Output:

BM_Get/50000  20166475 ns  items_per_second=2.47M/s

Run Tests

./tests

About

In-Memory Caching System

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published