|
| 1 | +.. index:: |
| 2 | + single: Cache |
| 3 | + single: Performance |
| 4 | + single: Components; Cache |
| 5 | + |
| 6 | +Adapters For Interoperability between PSR-6 and PSR-16 Cache |
| 7 | +============================================================ |
| 8 | + |
| 9 | +Sometimes, you may have a Cache object that implements the :ref:`PSR-16 <>` |
| 10 | +standard, but need to pass it to an object that expects a :ref:`PSR-6 <>` |
| 11 | +cache adapter. Or, you might have the opposite situation. The cache component contains |
| 12 | +two classes for bidirectional interoperability between PSR-6 and PSR-16 caches. |
| 13 | + |
| 14 | +Using a PSR-6 Cache Object as a PSR-16 Cache |
| 15 | +-------------------------------------------- |
| 16 | + |
| 17 | +Suppose you want to work with a class that requires a PSR-16 Cache pool object. For |
| 18 | +example:: |
| 19 | + |
| 20 | + use Psr\Cache\CacheItemPoolInterface; |
| 21 | + |
| 22 | + // just a made-up class for the example |
| 23 | + class GitHubApiClient |
| 24 | + { |
| 25 | + // ... |
| 26 | + |
| 27 | + // this requires a PSR-16 cache object |
| 28 | + public function __construct(CacheItemPoolInterface $cachePool) |
| 29 | + { |
| 30 | + // ... |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | +But, you already have a PSR-6 cache object, and you'd like to pass this to the class |
| 35 | +instead. No problem! The Cache component provides the |
| 36 | +:class:`Symfony\\Component\\Cache\\Adapter\\SimpleCacheAdapter` class for exactly |
| 37 | +this use-case:: |
| 38 | + |
| 39 | + use Symfony\Component\Cache\Simple\FilesystemCache; |
| 40 | + use Symfony\Component\Cache\Adapter\SimpleCacheAdapter |
| 41 | + |
| 42 | + // the PSR-6 cache object that you want to use |
| 43 | + $psr6Cache = new FilesystemCache(); |
| 44 | + |
| 45 | + // a PSR-16 cache that uses your cache internally! |
| 46 | + $psr16Cache = new SimpleCacheAdapter($psr6Cache); |
| 47 | + |
| 48 | + // now use this wherever you want |
| 49 | + $githubApiClient = new GitHubApiClient($psr16Cache); |
| 50 | + |
| 51 | +Using a PSR-16 Cache Object as a PSR-6 Cache |
| 52 | +-------------------------------------------- |
| 53 | + |
| 54 | +Suppose you want to work with a class that requires a PSR-6 Cache object. For |
| 55 | +example:: |
| 56 | + |
| 57 | + use Psr\SimpleCache\CacheInterface; |
| 58 | + |
| 59 | + // just a made-up class for the example |
| 60 | + class GitHubApiClient |
| 61 | + { |
| 62 | + // ... |
| 63 | + |
| 64 | + // this requires a PSR-6 cache object |
| 65 | + public function __construct(CacheInterface $cache) |
| 66 | + { |
| 67 | + // ... |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | +But, you already have a PSR-16 cache pool object, and you'd like to pass this to |
| 72 | +the class instead. No problem! The Cache component provides the |
| 73 | +:class:`Symfony\\Component\\Cache\\Simple\\Psr6Cache` class for exactly |
| 74 | +this use-case:: |
| 75 | + |
| 76 | + use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 77 | + use Symfony\Component\Cache\Simple\Psr6Cache; |
| 78 | + |
| 79 | + // the PSR-16 cache object that you want to use |
| 80 | + $psr16Cache = new FilesystemAdapter(); |
| 81 | + |
| 82 | + // a PSR-6 cache that uses your cache internally! |
| 83 | + $psr6Cache = new Psr6Cache($psr16Cache); |
| 84 | + |
| 85 | + // now use this wherever you want |
| 86 | + $githubApiClient = new GitHubApiClient($psr6Cache); |
0 commit comments