switon/cache

Redis-backed PSR-16 cache with JSON values, prefix-scoped clear, and cache lifecycle events for Switon Framework

Maintainers

Package info

github.com/switon-php/cache

Documentation

pkg:composer/switon/cache

Statistics

Installs: 4

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-06 13:43 UTC

This package is auto-updated.

Last update: 2026-06-07 04:23:42 UTC


README

CI PHP 8.3+

Switon's PSR-16 Redis cache for batch reads and writes, prefix-scoped clearing, and cache lifecycle events.

Highlights

  • PSR-16 cache: CacheInterface is the main cache contract.
  • Redis-backed storage: SimpleCache stores values in Redis with prefix-scoped access.
  • Batch reads and writes: read or write multiple keys in one call.
  • Safe clearing: clear() requires a prefix so it does not wipe unrelated keys.
  • Lifecycle visibility: CacheHit, CacheMiss, and related lifecycle events can be observed.

Installation

composer require switon/cache

Quick Start

use Psr\SimpleCache\CacheInterface;
use Switon\Core\Attribute\Autowired;

class ProductService
{
    #[Autowired] protected CacheInterface $cache;

    public function getFeatured(): array
    {
        $miss = new \stdClass();
        $products = $this->cache->get('featured_products', $miss);

        if ($products === $miss) {
            $products = $this->loadFeaturedProducts();
            $this->cache->set('featured_products', $products, 1800);
        }

        return $products;
    }
}

Docs: https://docs.switon.dev/latest/cache

License

MIT.