Search by

stougeiro / cache

stougeiro

Performant cache implementation package providing file-based and SQLite-backed handlers. Features optimized I/O with line-based serialization, WAL-mode SQLite, and modular design for PHP applications.

Package info

github.com/stougeiro/cache

pkg:composer/stougeiro/cache

Fund package maintenance!

Buymeacoffee

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-09-14 01:22 UTC

This package is auto-updated.

Last update: 2026-09-14 01:24:51 UTC


README

PHP PHPStan-Level Pest-php License

Cache

A small, extensible cache library for PHP that abstracts storage behind a unified API. It ships with two handlers — FileCache and SQLite — both implementing TTL expiration, safe serialization, and predictable behavior. Swap handlers without touching your application code.

✨ Features

  • Unified API
    A single CacheInterface powering all handlers, keeping your application fully decoupled from the underlying storage engine.

  • Swappable Handlers
    Switch between FileCache and SQLite simply by changing the configuration — no application code needs to be modified.

  • TTL-Based Expiration
    Each entry automatically expires; stale items are removed on access to keep storage clean and predictable.

  • Safe Serialization
    Corrupted or unreadable values are safely discarded, preventing exceptions and inconsistent cache states.

  • File Sharding
    The file-based handler distributes cache entries into MD5‑based subdirectories, improving lookup performance and reducing filesystem contention.

  • Graceful Degradation
    Read/write failures or corrupted entries fall back to default values without breaking application flow.

  • Extensible Architecture
    Implement new handlers via CacheHandlerInterface and register them inside the createHandler() method of the Cache class.

📦 Installation

Install via Composer:

composer require stougeiro/cache

🚀 Usage Example

Basic Usage

use STDW\Cache\Cache;
use STDW\Cache\CacheConfig;

$config = [
    'handler' => 'file', // or 'sqlite'
    'storage' => __DIR__ . '/cache',
];

$cache = new Cache( new CacheConfig($config));

// Store a value for 5 minutes (300 seconds)
$cache->set('username', 'sidney', ttl: 300);

// Retrieve it
$user = $cache->get('username'); // "sidney"

// Check existence
if ($cache->has('username')) {
    echo "Cached!";
}

// Delete a single entry
$cache->delete('username');

// Clear all cache entries
$cache->clear();

🧠 Why?

Because cache libraries should be fast, simple, and extensible without being over-engineered. This package provides two production-ready handlers with different tradeoffs — file-based for simplicity, SQLite for concurrency — behind a single interface that can be swapped without changing application code.

The goal is to offer a cache layer that:

  • avoids unnecessary abstractions,
  • stays predictable and easy to debug,
  • works in any environment (CLI, web, microservices),
  • and can be extended with custom storage engines when needed.

🤝 Contributions

Contributions are welcome. Feel free to open issues or submit pull requests.