ray/psr-cache-module

1.1.2 2021-11-10 15:35 UTC

This package is auto-updated.

Last update: 2022-05-10 16:56:44 UTC


README

codecov Type Coverage Continuous Integration Psalm level

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);

Installation

composer require ray/psr-cache-module

Module install

PSR-6

Psr6NullModule

This module is for the development.

  • Local: Null
  • Shared: Null
use Ray\PsrCacheModule\Psr6NullModule;

new Psr6NullModule();

Psr6ArrayModule

This module is for the development.

  • Local: Array
  • Shared: Array
use Ray\PsrCacheModule\Psr6ArrayModule;

new Psr6ArrayModule();

Psr6ApcuModule

This module is for a standalone server

  • Local: Chain(APC, File)
  • Shared: Chain(APC, File)
use Ray\PsrCacheModule\Psr6ApcuModule;

new Psr6ApcuModule();

Psr6RedisModule

This module is for multiple servers.

  • Local: Chain(APC, File)
  • Shared: Redis
use Ray\PsrCacheModule\Psr6RedisModule;

new Psr6RedisModule('redis1:6379:1'); // host:port:dbIndex

Psr6MemcachedModule

This module is for multiple servers.

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

PSR-16

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();

Common Configuration Module

CacheDirModule

Specifies the cache directory. Optional.

use Ray\PsrCacheModule\CacheDirModule;

new CacheDirModule('path/to/dir');

CacheNamespaceModule

Specifies the cache namespace (when multiple applications are placed on a single cache server). Optional.

use Ray\PsrCacheModule\CacheNamespaceModule;

new CacheNamespaceModule('app1');