PHP code example of wshafer / psr11-phpcache

1. Go to this page and download the library: Download wshafer/psr11-phpcache 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/ */

    

wshafer / psr11-phpcache example snippets




// Get a pool
$pool = $container->get('myCacheServiceName');

// Get an item (existing or new)
$item = $pool->getItem('cache_key');

// Set some values and store
$item->set('value');
$item->expiresAfter(60);
$pool->save($item);

// Verify existence
$pool->hasItem('cache_key'); // True
$item->isHit(); // True

// Get stored values
$myValue = $item->get();
echo $myValue; // "value"

// Delete
$pool->deleteItem('cache_key');
$pool->hasItem('cache_key'); // False

// Create Container
$container = new \Xtreamwayz\Pimple\Container([
    // Cache using the default keys.
    'cache' => new \WShafer\PSR11PhpCache\PhpCacheFactory(),
    
    // Another Cache using a different cache configuration
    'otherCache' => function($c) {
        return \WShafer\PSR11PhpCache\PhpCacheFactory::cacheTwo($c);
    },

    'config' => [
        'caches' => [
            /*
             * At the bare minimum you must ace will me used instead.
                'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
                'options'   => [],             // Optional : Adapter Specific Options
            ],
            
            // Another Cache
            'cacheTwo' => [
                'type'      => 'memcached',    // Required : Type of adapter
                'namespace' => 'my-namespace', // Optional : Namespace
                'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
                'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
                'options'   => [],             // Optional : Adapter Specific Options
            ],
            
            // Cache Chain
            'chained' => [
                'type' => 'chain',                              // Required : Type of adapter
                'options' => [
                    'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                    'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
                ]
            ],
        ],
    ],
]);

$container = new \Zend\ServiceManager\ServiceManager([
    'factories' => [
        // Cache using the default keys.
        'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
        
        // Another Cache using a different cache configuration
        'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
    ]
]);

$container->setService('config', [
    'caches' => [
        /*
         * At the bare minimum you must ger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
]);

$container = new \Laminas\ServiceManager\ServiceManager([
    'factories' => [
        // Cache using the default keys.
        'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
        
        // Another Cache using a different cache configuration
        'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
    ]
]);

$container->setService('config', [
    'caches' => [
        /*
         * At the bare minimum you must '    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
]);


return [
    'dependencies' => [
       'factories' => [
           // Cache using the default keys.
           'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
           
           // Another Cache using a different cache configuration
           'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
       ]
    ],
    
    'caches' => [
        /*
         * At the bare minimum you must stead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
];


return [
    'service_manager' => [
       'factories' => [
           // Cache using the default keys.
           'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
           
           // Another Cache using a different cache configuration
           'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
       ]
    ],
    
    'caches' => [
        /*
         * At the bare minimum you must ad.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
];


return [
    'dependencies' => [
       'factories' => [
           // Cache using the default keys.
           'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
           
           // Another Cache using a different cache configuration
           'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
       ]
    ],
    
    'caches' => [
        /*
         * At the bare minimum you must stead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
];



return [
    // ... Previously registered modules here
    'WShafer\\PSR11PhpCache',
];



return [
    'modules' => [
        // ... Previously registered modules here
        'WShafer\\PSR11PhpCache',
    ]
];


use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

e minimum you must     'default' => [
                'type'      => 'void',         // Required : Type of adapter
                'namespace' => 'my-namespace', // Optional : Namespace
                'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
                'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
                'options'   => [],             // Optional : Adapter Specific Options
            ],
            
            // Another Cache
            'cacheTwo' => [
                'type'      => 'memcached',    // Required : Type of adapter
                'namespace' => 'my-namespace', // Optional : Namespace
                'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
                'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
                'options'   => [],             // Optional : Adapter Specific Options
            ],
            
            // Cache Chain
            'chained' => [
                'type' => 'chain',                              // Required : Type of adapter
                'options' => [
                    'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                    'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
                ]
            ],
        ],
    ],
];

$app = new \Slim\App($config);

// Wire up the factory
$container = $app->getContainer();

// Register the service with the container.
$container['cache'] = new \WShafer\PSR11PhpCache\PhpCacheFactory();
$container['otherCache'] = function($c) {
    return WShafer\PSR11PhpCache\PhpCacheFactory::cacheTwo($c);
};




return [
    'dependencies' => [
       'factories' => [
           // Cache using the default keys.
           'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
       ]
    ],
    
    'caches' => [
        /*
         * At the bare minimum you must ions
        ],
    ],
];




return [
    'dependencies' => [
       'factories' => [
           // Cache using the default keys.
           'cache' => \WShafer\PSR11PhpCache\PhpCacheFactory::class,
           
           // Another Cache using a different cache configuration
           'otherCache' => [\WShafer\PSR11PhpCache\PhpCacheFactory::class, 'cacheTwo'],
       ]
    ],
    
    'caches' => [
        /*
         * At the bare minimum you must tead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Another Cache
        'cacheTwo' => [
            'type'      => 'memcached',    // Required : Type of adapter
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [],             // Optional : Adapter Specific Options
        ],
        
        // Cache Chain
        'chained' => [
            'type' => 'chain',                              // Required : Type of adapter
            'options' => [
                'services'      => ['default', 'cacheTwo'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                   // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'apc',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'skipOnCli' => false, // Optional : Skip cache with CLI
            ],
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'apcu',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'skipOnCli' => false, // Optional : Skip cache with CLI
            ],
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'array',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => []           // No options available,
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'fileSystem',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'flySystemService' => 'my-service', // Required : Pre-configured FlySystem service from the container
                'folder'           => 'cache',      // Optional : Folder.  Default: 'cache'
            ]
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'illuminate',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'store' => 'my-service', // Required : Pre-configured illuminate store service from the container
            ]
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'      => 'memcached',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [
                // A container service is options.  See: http://php.net/manual/en/memcached.setoption.php
                // Only set if servers are provided.
                'memcachedOptions' => [
                    \Memcached::OPT_HASH => Memcached::HASH_MURMUR
                ],
                
                // Optional :  Persistent Id.  Only used if servers are provided.
                'persistentId' => 'some_id',  
            ]
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'    => 'mongodb',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                // A container service is       'database' => 'some-db-name',
                
                // Required if no service is provided : Collection name.
                'collection' => 'some_collection',  
            ]
        ],
    ],
];



return [
    'caches' => [
        'fromService' => [
            'type'      => 'predis',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [
                // A container service is  PSR-1 Logger Service Name
            'options'   => [
                // Required if no service is provided : server(s)
                'servers'      => [
                    'tcp:/127.0.0.1:6379'
                ],
                
                // Optional : Array of options to pass to the client
                'connectionOptions' => [],
            ]
        ],
        
        'singleConnectionUsingConnectionParams' => [
            'type'      => 'predis',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [
                // Required if no service is provided : server(s)
                'servers'      => [
                    [
                        'scheme' => 'tcp',
                        'host'   => '10.0.0.1',
                        'port'   => 6379,
                    ]
                ],
                
                // Optional : Array of options to pass to the client
                'connectionOptions' => [],
            ],
        ],
        
        'cluster' => [
            'type'      => 'predis',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [
                // Required if no service is provided : server(s)
                'servers'      => [
                    'tcp://10.0.0.1?alias=first-node',
                    ['host' => '10.0.0.2', 'alias' => 'second-node'],
                ],
                
                // Optional : Array of options to pass to the client
                'connectionOptions' => ['cluster' => 'redis'],
            ],
        ]
    ],
];



return [
    'caches' => [
        'fromService' => [
            'type'      => 'redis',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => [
                // A container service is  Logger Service Name
            'options'   => [
                // Required if no service is provided : server(s)
                'server'      => [
                    'host' => '127.0.0.1',  // Required : Hostname
                    'port' => 6379,         // Optional : Port (Default: 6379)
                    'timeout' => 0.0,       // Optional : Timeout (Default: 0.0)
                    'persistent' => true,   // Optional : Use persistent connections (Default: true)
                    'persistentId' => null, // Optional : Persistent Id (Default: 'phpcache')
                ],
            ],
        ],
    ],
];



return [
    'caches' => [
        'myHandlerName' => [
            'type'      => 'void',
            'namespace' => 'my-namespace', // Optional : Namespace
            'prefix'    => 'prefix_',      // Optional : Prefix.  If a Namespace is configured and the adapter supports it, the Namespace will me used instead.
            'logger'    => 'my-logger',    // Optional : PSR-1 Logger Service Name
            'options'   => []              // No options available,
        ],
    ],
];



return [
    'caches' => [
        'fromService' => [
            'type'    => 'doctrine',
            'prefix'  => 'prefix_',   // Optional : Prefix.  Namespaces are not supported on this adapter.
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'service'  => 'my-service', // Required : A pre-configured doctrine cache service name
            ]
        ],
    ],
];



return [
    'caches' => [
        'fromService' => [
            'type'    => 'chain',
            'logger'  => 'my-logger', // Optional : PSR-1 Logger Service Name
            'options' => [
                'services'      => ['service-one', 'service-two'], // Required : An array of pre-configured cache service names
                'skipOnFailure' => false,                          // Optional : If true we will remove a pool form the chain if it fails. (Default: false)
            ]
        ],
    ],
];