Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. Cache
  5. Adapters For Interoperability between PSR-6 and PSR-16 Cache
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Using a PSR-16 Cache Object as a PSR-6 Cache
  • Using a PSR-6 Cache Object as a PSR-16 Cache

Adapters For Interoperability between PSR-6 and PSR-16 Cache

Edit this page

Warning: You are browsing the documentation for Symfony 4.1, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Adapters For Interoperability between PSR-6 and PSR-16 Cache

Sometimes, you may have a Cache object that implements the PSR-16 standard, but need to pass it to an object that expects a PSR-6 cache adapter. Or, you might have the opposite situation. The cache component contains two classes for bidirectional interoperability between PSR-6 and PSR-16 caches.

Using a PSR-16 Cache Object as a PSR-6 Cache

Suppose you want to work with a class that requires a PSR-6 Cache pool object. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
use Psr\Cache\CacheItemPoolInterface;

// just a made-up class for the example
class GitHubApiClient
{
    // ...

    // this requires a PSR-6 cache object
    public function __construct(CacheItemPoolInterface $cachePool)
    {
        // ...
    }
}

But, you already have a PSR-16 cache object, and you'd like to pass this to the class instead. No problem! The Cache component provides the SimpleCacheAdapter class for exactly this use-case:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Cache\Simple\FilesystemCache;
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;

// the PSR-16 cache object that you want to use
$psr16Cache = new FilesystemCache();

// a PSR-6 cache that uses your cache internally!
$psr6Cache = new SimpleCacheAdapter($psr16Cache);

// now use this wherever you want
$githubApiClient = new GitHubApiClient($psr6Cache);

Using a PSR-6 Cache Object as a PSR-16 Cache

Suppose you want to work with a class that requires a PSR-16 Cache object. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
use Psr\SimpleCache\CacheInterface;

// just a made-up class for the example
class GitHubApiClient
{
    // ...

    // this requires a PSR-16 cache object
    public function __construct(CacheInterface $cache)
    {
        // ...
    }
}

But, you already have a PSR-6 cache pool object, and you'd like to pass this to the class instead. No problem! The Cache component provides the Psr6Cache class for exactly this use-case:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Simple\Psr6Cache;

// the PSR-6 cache object that you want to use
$psr6Cache = new FilesystemAdapter();

// a PSR-16 cache that uses your cache internally!
$psr16Cache = new Psr6Cache($psr6Cache);

// now use this wherever you want
$githubApiClient = new GitHubApiClient($psr16Cache);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Antal Áron, a Symfony contributor

Thanks Antal Áron (@antalaron) for being a Symfony contributor

2 commits • 32 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia