Skip to content
Shesh Ghimire edited this page Jun 14, 2024 Β· 7 revisions

Welcome!

This package aims to provide a better Developer Experience (DX) to interact with various Cache Adapters required by a PHP Project. Powered by Symfony Cache, this library provides support for Stampede Prevention out of the Box by opting to use Symfony Cache Contracts instead of PSR-6 Cache Interface.

Quick Setup

Installation

composer require thewebsolver/cache

Bootstrap

use TheWebSolver\Codegarage\Lib\Cache\Cache;
use TheWebSolver\Codegarage\Lib\Cache\Factory;
use TheWebSolver\Codegarage\Lib\Cache\Data\PdoDsn;
use TheWebSolver\Codegarage\Lib\Cache\Data\Directory;

// If project has PSR-11 Container, then in the application bootstrap file,
// the container must auto-wire factory following the singleton pattern.
$container->singleton(Factory::class, static fn(): Factory => new Factory());

// Then, initialize driver factory. If project does not have container,
// starting factory is not required during bootstrap It will be
// instantiated on first call to Cache Facade.
Factory::start($container);

// Finally, bootstrap with default (required) Cache Pool Type
// & additional (optional) Cache Pool Type configurations.
Cache::configure(
    // $default
    new Directory(location: 'your/desired/cache/directory'),
    // ...$additional
    new PdoDsn(dsn: 'mysql:host=localhost;dbname=testDatabase')
);

Usage

After bootstrap, use Cache Facade within application where applicable. All Facade calls will route through default driver, i.e. Filesystem Cache Pool (Directory passed as $default's value during bootstrap above).

use TheWebSolver\Codegarage\Lib\Cache\Cache;
use TheWebSolver\Codegarage\Lib\Cache\Data\PoolType;

// Using default Pool:
$item = Cache::add('cacheKey', 'cacheVale');

Using any other registered Pool other than default (passed as first $additional's value during bootstrap).

$item = Cache::driver(PoolType::Database)->add('cacheKey', 'cacheValue');

Clone this wiki locally