germania-kg/namespaced-cache

Factory classes for namespaced PSR-6 CacheItemPools

1.4.0 2021-09-07 07:53 UTC

This package is auto-updated.

Last update: 2024-03-07 13:09:53 UTC


README

68747470733a2f2f7374617469632e6765726d616e69612d6b672e636f6d2f6c6f676f732f67612d6c6f676f2d323031362d7765622e7376677a

Namespaced CacheItemPool Factory

Installation

$ composer require germania-kg/namespaced-cache

One of these libraries is required to be installed manually:

$ composer require symfony/cache
$ composer require tedivm/stash

Interfaces

Factory Interface

Classes implementing the PsrCacheItemPoolFactoryInterface are callable. Their invokation method accepts a namespace string.

<?php
Germania\NamespacedCache\PsrCacheItemPoolFactoryInterface;

interface PsrCacheItemPoolFactoryInterface
{
    public function __invoke( string $namespace) : \Psr\Cache\CacheItemPoolInterface;
}

DefaultLifeTimeAware

Define a default lifetime for cache items. It can be used on those PSR-6 libraries that support default life times on cache item pools.

<?php
use Germania\NamespacedCache\DefaultLifeTimeAware;

interface DefaultLifeTimeAware
{
    /**
     * Returns default cache item lifetime.
     *
     * @return int|null
     */
    public function getDefaultLifetime() : ?int;


    /**
     * Sets default cache item lifetime.
     *
     * @param int|null $lifetime Default cache item lifetime
     */
    public function setDefaultLifetime( ?int $lifetime);
}

Example:

<?php
use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory;
use Germania\NamespacedCache\DefaultLifeTimeAware;

$factory = new SymfonyFileCacheItemPoolFactory($directory);
if ($factory instanceOf DefaultLifeTimeAware::class) {
    $factory->setDefaultLifetime( 3600 );
}

Auto-discovering

Abstract class PsrCacheItemPoolFactory provides a static autodiscover method which will create SQLite or Filesystem cache factory, depending on if SQLite being available.

This is an experimental feature.

<?php
use Germania\NamespacedCache\PsrCacheItemPoolFactory;

$factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path);
$factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path, $default_lifetime);

Filesystem caches

Auto-discover Symfony Cache or Stash

Use this when migrating from one cache engine to another. It internally uses SymfonyFileCacheItemPoolFactory or StashFileCacheItemPoolFactory, whichever library is installed.

Callable class FileCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

<?php
use Germania\NamespacedCache\FileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 
$default_lifetime = 0;

$factory = new FileCacheItemPoolFactory();
$factory = new FileCacheItemPoolFactory($directory, $default_lifetime);

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

echo get_class($cache);
// "Stash\Pool" or 
// "Symfony\Component\Cache\Adapter\FilesystemAdapter"

Symfony Cache Component

Callable class SymfonyFileCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and DefaultLifeTimeAware:

<?php
use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 
$default_lifetime = 0;

$factory = new SymfonyFileCacheItemPoolFactory();
$factory = new SymfonyFileCacheItemPoolFactory($directory, $default_lifetime);
$factory = (new SymfonyFileCacheItemPoolFactory($directory))
           ->setDefaultLifetime( 3600 );

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Stash PHP Caching Library

Callable class StashFileCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface. Note that Stash caches do not provide setting default cache item lifetime.

<?php
use Germania\NamespacedCache\StashFileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

$factory = new StashFileCacheItemPoolFactory();
$factory = new StashFileCacheItemPoolFactory($directory);
  
// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Sqlite Caches

Auto-discover Symfony Cache or Stash

Use this when migrating from one cache engine to another. It internally uses SymfonySqliteCacheItemPoolFactory or StashSqliteCacheItemPoolFactory, whichever library is installed.

Callable class SqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

Please note:

  • Symfony/Cache requires a DSN string
  • Stash/Cache requires a directory.
<?php
use Germania\NamespacedCache\SqliteCacheItemPoolFactory;

# These are defaults
$directory_or_dsn = getcwd(); 
$default_lifetime = 0;

$factory = new SqliteCacheItemPoolFactory($directory_or_dsn);
$factory = new SqliteCacheItemPoolFactory($directory_or_dsn, $default_lifetime);

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

echo get_class($cache);
// "Stash\Pool" or 
// "Symfony\Component\Cache\Adapter\PdoAdapter"

Symfony Cache Component

Callable class SymfonySqliteCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and DefaultLifeTimeAware.

<?php
use Germania\NamespacedCache\SymfonySqliteCacheItemPoolFactory;

# These are defaults
$dsn_or_directory = "sqlite::memory:"; 
$dsn_or_directory = "sqlite:/path/to/mydb.sq3";
$dsn_or_directory = "/tmp";
$default_lifetime = 0;

$factory = new SymfonySqliteCacheItemPoolFactory();
$factory = new SymfonySqliteCacheItemPoolFactory($dsn_or_directory, $default_lifetime);
$factory = (new SymfonySqliteCacheItemPoolFactory($dsn_or_directory))
           ->setDefaultLifetime( 3600 );

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Stash PHP Caching Library

Callable class StashSqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

Note that Stash caches do not provide setting default cache item lifetime.

<?php
use Germania\NamespacedCache\StashSqliteCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

$factory = new StashSqliteCacheItemPoolFactory();
$factory = new StashSqliteCacheItemPoolFactory($directory);
  
// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Testing

$ composer phpunit