tobento/app-cache

1.0.2 2024-12-01 12:12 UTC

This package is auto-updated.

Last update: 2024-12-01 12:14:15 UTC


README

Cache support for the app.

Table of Contents

Getting Started

Add the latest version of the app cache project running this command.

composer require tobento/app-cache

Requirements

  • PHP 8.0 or greater

Documentation

App

Check out the App Skeleton if you are using the skeleton.

You may also check out the App to learn more about the app in general.

Cache Boot

The cache boot does the following:

  • installs and loads cache config file
  • implements PSR-6 and PSR-16 interfaces based on cache config
use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);

// Run the app:
$app->run();

You may check out the Cache Service to learn more about it.

Cache Config

The configuration for the cache is located in the app/config/cache.php file at the default App Skeleton config location where you can specify the pools and caches for your application.

Cache Usage

You can access the pools and caches in several ways:

Using the app

use Tobento\App\AppFactory;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);
$app->booting();

// PSR-6 cache:

// using the default pool:
$pool = $app->get(CacheItemPoolInterface::class);

// using the pools:
$pools = $app->get(CacheItemPoolsInterface::class);


// PSR-16 simple cache:

// using the default pool:
$cache = $app->get(CacheInterface::class);

// using the caches:
$caches = $app->get(CachesInterface::class);

// Run the app:
$app->run();

Check out the Cache Item Pools Interface to learn more about it.

Check out the Caches Interface to learn more about it.

Using autowiring

You can also request the interfaces in any class resolved by the app:

use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

class SomeService
{
    public function __construct(
        protected CacheItemPoolsInterface $pools,
        protected CacheItemPoolInterface $pool,
        protected CachesInterface $caches,
        protected CacheInterface $cache,
    ) {}
}

Adding and Registering Caches

You may add and register more pools and caches by the following way instead of using the cache config file:

use Tobento\App\AppFactory;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Tobento\Service\Cache\Simple\CachesInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// PSR-6:
$app->on(CacheItemPoolsInterface::class, function(CacheItemPoolsInterface $pools) {
    // using the add method:
    $pools->add(name: 'name', pool: $pool);
    
    // using the register method:
    $pools->register(
        name: 'name',
        pool: function(string $name): CacheItemPoolInterface {
            // create the pool:
            return $pool;
        },
    );
});

// PSR-16:
$app->on(CachesInterface::class, function(CachesInterface $caches) {
    // using the add method:
    $caches->add(name: 'name', cache: $cache);
    
    // using the register method:
    $caches->register(
        name: 'name',
        cache: function(string $name): CacheInterface {
            // create the cache:
            return $cache;
        },
    );
});

// Adding boots:
$app->boot(\Tobento\App\Cache\Boot\Cache::class);

// Run the app:
$app->run();

Deleting Expired Items

Some PSR 6 cache pools or PSR 16 caches do not include an automated mechanism for pruning expired cache items.

If you have installed the App Console you may easily delete expired items running the following commands:

PSR 6

php ap cache:pool:prune

PSR 16

php ap cache:prune

If you would like to automate this process, consider installing the App Schedule bundle and using a command task:

use Tobento\Service\Schedule\Task;
use Butschster\CronExpression\Generator;

$schedule->task(
    (new Task\CommandTask(
        command: 'cache:pool:prune',
    ))
    // schedule task:
    ->cron(Generator::create()->weekly())
);

Clearing Cache

If you have installed the App Console you may easily clear caches running the following commands:

PSR 6

php ap cache:pool:clear

Clearing specific pools only:

php ap cache:pool:clear --pool=file --pool=another

PSR 16

php ap cache:clear

Clearing specific pools only:

php ap cache:clear --cache=file --cache=another

Credits