PHP code example of chh / cache-service-provider

1. Go to this page and download the library: Download chh/cache-service-provider 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/ */

    

chh / cache-service-provider example snippets




use Doctrine\Common\Cache\ApcuCache;

$app = new Silex\Application;

$app->register(new \CHH\Silex\CacheServiceProvider, [
    'cache.options' => [
        'default' => [
            'driver' => ApcuCache::class,
        ],
    ],
]);

if ($app['cache']->contains('foo')) {
    echo $app['cache']->fetch('foo'), "<br>";
} else {
    $app['cache']->save('foo', 'bar');
}

$app->register(new \CHH\Silex\CacheServiceProvider, [
    'cache.options' => [
        'default' => ['driver' => ApcuCache::class],
        'file' => [
            'driver' => 'filesystem',
            'directory' => '/tmp/myapp',
        ],
        'global' => [
            'driver' => function () use ($app) {
                $redis = new \Doctrine\Common\Cache\RedisCache;
                $redis->setRedis($app['redis']);

                return $redis;
            },
        ],
    ],
]);

$app['caches']['file']->save('foo', 'bar');
$app['caches']['default']->save('bar', 'baz');



$app['caches']['myext'] = $app['cache.factory']([
    'driver' => 'filesystem',
    'directory' => sys_get_temp_dir() . '/myext',
]);



use Pimple\Container;
use Pimple\ServiceProviderInterface;
use CHH\Silex\CacheServiceProvider\CacheNamespace;

class ExampleServiceProvider extends ServiceProviderInterface
{
    function register(Container $app)
    {
        // Check if Cache Service Provider is registered:
        if (isset($app['caches'])) {
            $app['caches'] = $app->extend(function ($caches) use ($app) {
                // Use a CacheNamespace to safely add keys to the default
                // cache.
                $caches['example'] = function () use ($caches) {
                    return new CacheNamespace('example', $caches['default']);
                };

                return $caches;
            });
        }
    }
}

// Check if Cache Service Provider is registered:
if (isset($app['caches'])) {
    $app['caches'] = $app->extend(function ($caches) use ($app) {
        // Use a CacheNamespace to safely add keys to the default cache
        $caches['example'] = $app['cache.namespace']('example');

        return $caches;
    });
}