thecodingmachine / stash-universal-module
Cross-framework module for Stash
Installs: 108 352
Dependents: 3
Suggesters: 2
Security: 0
Stars: 0
Watchers: 8
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
- container-interop/service-provider: ^0.4
- tedivm/stash: ~0.14.0
- thecodingmachine/common-factories: ^0.4
Requires (Dev)
- mnapoli/simplex: ~0.3.0
- phpunit/phpunit: ^5.0
- satooshi/php-coveralls: ^1.0
README
Stash universal module
This package integrates Stash (the PHP PSR-6 compatible cache library) in any container-interop/service-provider compatible framework/container.
Installation
composer require thecodingmachine/stash-universal-module
Once installed, you need to register the TheCodingMachine\StashServiceProvider
into your container.
If your container supports thecodingmachine/discovery integration, you have nothing to do. Otherwise, refer to your framework or container's documentation to learn how to register service providers.
Introduction
This service provider is meant to create a PSR-6 cache pool Psr\Cache\CacheItemPoolInterface
instance.
Out of the box, the instance should be usable with sensible defaults. We tried to keep the defaults usable for most of the developer, while still providing best performances for the server.
Usage
use Psr\Cache\CacheItemPoolInterface $cachePool = $container->get(CacheItemPoolInterface::class); echo $cachePool->getItem('my_cached_value')->get();
Default values
By default:
- The default cache pool is a composite pool made of:
- An ephemeral (in-memory) driver for fast access to already fetched values
- An APC driver (or a Filesystem driver as fallback if APC is not available)
Configuration
Important: This service provider accepts an optional parameter in the constructor: a "suffix" that can be used if you want many different instances.
use Psr\Cache\CacheItemPoolInterface // Let's assume we are using Simplex as our container $container = new Simplex\Container(); // Registers a default service provider $container->register(new StashServiceProvider()); // Registers another service provider for a shared memcache pool $container->register(new StashServiceProvider('shared')); // Lets configure the second service provider. $container['stash.shared.memcache.options'] = [ 'servers' => ['127.0.0.1', '11211'] ]; // Let's override the composite options to put an ephemeral driver and the memcache driver next. $container['stash.composite.options'] = function(ContainerInterface $container) { return [ $container->get(Ephemeral::class), $container->get(Memcache::class) ]; } $defaultCachePool = $container->get(CacheItemPoolInterface::class); //... do stuff with the default pool // The shared memcache pool can be accessed by suffixing the instance with ".shared". $sharedCachePool = $container->get(CacheItemPoolInterface::class.'.shared'); //... do stuff
When this service provider looks for a service, it will first look for the service prefixed with the package name, then for the service directly.
So if this documentation states that the stash.apc.options
entry is used, the service provider will first look into thecodingmachine.stash-universal-module.stash.apc.options
and then into stash.apc.options
.
This allows you to keep your container clean (with only one stash.apc.options
entry), and in case there are several service providers using that stash.apc.options
entry and you want to pass different values, you can still edit thecodingmachine.stash-universal-module.stash.apc.options
for this service provider only.
Expected values / services
This service provider expects the following configuration / services to be available:
Provided services
This service provider provides the following services:
Extended services
This service provider does not extend any service.