This package is the Ray.Di module that performs the PSR-6 / PSR-16 interface binding.
You can use the PSR6 cache interface in two ways: Local
and Public
.
Local
is for caches that do not need to be shared among multiple web servers, and Public
is for caches that need to be shared.
PHP8
use Psr\Cache\CacheItemPoolInterface;
use Ray\PsrCacheModule\Annotation\Local;
use Ray\PsrCacheModule\Annotation\Shared;
class Foo
{
public function __construct(
#[Local] private CacheItemPoolInterface $localPool,
#[Shared] private CacheItemPoolInterface $sharedPool
){}
}
PHP7.4
use Psr\Cache\CacheItemPoolInterface;
use Ray\PsrCacheModule\Annotation\Local;
use Ray\PsrCacheModule\Annotation\Shared;
class Foo
{
private CacheItemPoolInterface $localPool;
private CacheItemPoolInterface $sharedPool;
/**
* @Local('localPool')
* @Shared('sharedPool')
*/
public function __construct(
CacheItemPoolInterface $localPool,
CacheItemPoolInterface $sharedPool
){
$this->localPool = $localPool;
$this->sharedPool = $sharedPool;
}
}
Create object graph
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\PsrCacheModule\Psr6ArrayModule;
$foo = (new Injector(new class extends AbstractModule {
protected function configure()
{
$this->install(new Psr6ArrayModule()); // PSR-6
// $this->install(new Psr16CacheModule()); // PSR-16
}
}))->getInstance(Foo::class);
assert($foo instanceof Foo);
composer require ray/psr-cache-module
This module is for the development.
- Local: Null
- Shared: Null
use Ray\PsrCacheModule\Psr6NullModule;
new Psr6NullModule();
This module is for the development.
- Local: Array
- Shared: Array
use Ray\PsrCacheModule\Psr6ArrayModule;
new Psr6ArrayModule();
This module is for a standalone server
- Local: Chain(APC, File)
- Shared: Chain(APC, File)
use Ray\PsrCacheModule\Psr6ApcuModule;
new Psr6ApcuModule();
This module is for multiple servers.
- Local: Chain(APC, File)
- Shared: Redis
use Ray\PsrCacheModule\Psr6RedisModule;
new Psr6RedisModule('redis1:6379:1'); // host:port:dbIndex
This module is for multiple servers.
- Local: Chain(APC, File)
- Shared: Memcached
use Ray\PsrCacheModule\Psr6MemcachedModule;
new Psr6MemcachedModule('memcached1:11211:60,memcached2:11211:33'); // host:port:weight
See https://www.php.net/manual/en/memcached.addservers.php
If you install Psr16CacheModule, the cache engine installed with Psr6*Module can be used with PSR-16 interface. PSR-16 bindings use PSR-6 bindings.
use Ray\PsrCacheModule\Psr16CacheModule;
new Psr16CacheModule();
Specifies the cache directory. Optional.
use Ray\PsrCacheModule\CacheDirModule;
new CacheDirModule('path/to/dir');
Specifies the cache namespace (when multiple applications are placed on a single cache server). Optional.
use Ray\PsrCacheModule\CacheNamespaceModule;
new CacheNamespaceModule('app1');
Redis, Memcached classes and symfony/cache adapters are not serializable, but RedisAdapter and MemcachedAdapter, which inherit from symfony/cache and are provided in this package, are.