Understanding PHP-FIG: Choosing Between PSR‑6 and PSR‑16 Caching Standards
This article explains the PHP Framework Interop Group (PHP‑FIG), outlines the goals and terminology of PSR‑6 and PSR‑16 caching standards, compares their interfaces with code examples, and concludes with guidance on when to use each approach in PHP projects.
PHP‑FIG Overview
PHP‑FIG (PHP Framework Interop Group) is a community‑driven consortium that publishes PHP Standard Recommendations (PSRs) to improve interoperability and portability across frameworks such as Symfony, Laravel, and Zend Framework.
PSR‑6 – Cache Interface
Goal
Provide a generic cache API that enables library authors to write cache‑aware code usable in any framework without writing framework‑specific adapters.
Key concepts
PSR‑6 introduces the notions of a cache pool (an abstraction of a backend such as Redis, APCu, Memcached) and cache items (objects representing individual key‑value entries). A pool manages a collection of items; each item implements CacheItemInterface.
Core interfaces
The Psr\Cache namespace defines four interfaces: CacheItemInterface – represents a single cache entry, provides get(), set(), isHit(), expiresAt(), expiresAfter(). CacheItemPoolInterface – manages a set of items, provides getItem(), getItems(), hasItem(), save(), saveDeferred(), commit(), deleteItem(), clear(). CacheException – base interface for all cache‑related exceptions. InvalidArgumentException – thrown when an invalid cache key is supplied.
Typical usage
$pool = new Psr6Implementation();
$item = $pool->getItem('foo');
if (!$item->isHit()) {
// Fetch or compute the value
$value = 'bar';
$item->set($value);
$pool->save($item);
} else {
$value = $item->get();
}
return $value;PSR‑6 supports deferred writes via saveDeferred() and batch commit with commit(), making it suitable for complex caching strategies.
PSR‑16 – Simple Cache
Goal
Offer a lightweight, easy‑to‑adopt key‑value cache API for straightforward use‑cases.
Key concepts
PSR‑16 does not expose cache pools or items; it treats the cache as a simple key‑value store.
Core interfaces
The Psr\SimpleCache namespace defines three interfaces: CacheInterface – primary API with methods get(), set(), delete(), clear(), has(), getMultiple(), setMultiple(), deleteMultiple() (all accept optional TTL). CacheException – base exception interface. InvalidArgumentException – thrown for invalid keys.
Typical usage
$cache = new Psr16Implementation();
if (!$cache->has('foo')) {
// Fetch or compute the value
$value = 'bar';
$cache->set('foo', $value); // optional TTL can be passed as third argument
} else {
$value = $cache->get('foo');
}
return $value;When a default value is acceptable on a miss, the code can be reduced to a single call:
$cache = new Psr16Implementation();
return $cache->get('foo', 'bar');PSR‑16 yields shorter code because it works with a single cache object and does not provide advanced features such as deferred writes.
Interoperability and Adoption
Both PSR‑6 and PSR‑16 declare a clear() method with identical signatures, allowing a single cache implementation to satisfy both interfaces on PHP 5.3.9+. Symfony 3.3+ ships adapters that convert a PSR‑6 CacheItemPoolInterface to a PSR‑16 CacheInterface and vice‑versa, facilitating gradual migration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
