PHP code example of psr-discovery / cache-implementations

1. Go to this page and download the library: Download psr-discovery/cache-implementations 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/ */

    

psr-discovery / cache-implementations example snippets


use PsrDiscovery\Discover;

// Return an instance of the first discovered PSR-6 Cache implementation.
$cache = Discover::cache();

$cache->set('foo', 'bar');

use PsrDiscovery\Discover;

$caches = Discover::caches();

foreach ($caches as $cache) {
    echo sprintf('Discovered %s v%s', $cache->getPackage(), $cache->getVersion());
}

use PsrDiscovery\Discover;

$cache = Discover::cache();

if ($cache === null) {
    // No suitable Cache implementation was discovered.
    // Fall back to a default implementation.
    $cache = new DefaultCache();
}

use PsrDiscovery\Discover;

// $cache1 !== $cache2 (default)
$cache1 = Discover::cache();
$cache2 = Discover::cache();

// $cache1 === $cache2
$cache1 = Discover::cache(singleton: true);
$cache2 = Discover::cache(singleton: true);

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr6\Caches;

// Prefer the a specific implementation of PSR-6 over others.
Caches::prefer('league/container');

// Return an instance of League\Container\Container,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$cache = Discover::cache();

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr6\Caches;

// Only discover a specific implementation of PSR-6.
Caches::use('league/container');

// Return an instance of League\Container\Container,
// or null if it is not available.
$cache = Discover::cache();