PHP code example of germania-kg / namespaced-cache

1. Go to this page and download the library: Download germania-kg/namespaced-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

germania-kg / namespaced-cache example snippets



Germania\NamespacedCache\PsrCacheItemPoolFactoryInterface;

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


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);
}



use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory;
use Germania\NamespacedCache\DefaultLifeTimeAware;

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


use Germania\NamespacedCache\PsrCacheItemPoolFactory;

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


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"


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");


use Germania\NamespacedCache\StashFileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

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


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"


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");


use Germania\NamespacedCache\StashSqliteCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

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