senenhermida.docs
  • Design Patterns
  • SOLID
  • PHP History
    • Late static binding
  • PHP Questions
  • Symfony
    • Service container
    • Event dispatcher
    • Security
    • Validation
    • Serializer
    • Http Kernel Component
    • Cache component
    • Symfony history
  • Algorithms
  • Leetcode
    • Codility
  • My CV
Powered by GitBook
On this page
  • Cache contracts
  • Generic Caching (PSR-6)
  • Basic usage
  • Cache invalidation
  • Using cache tags
  • Cache expiration
  1. Symfony

Cache component

Includes 2 approaches

  • Cache contracts: cache values based on recomputation callbacks

  • PSR-6 caching: a generic cache system, which involves cache pools annd cahce itmes

Cache contracts

All adapters support the Cache Contracts. They contain only two methods: get() and delete(). There's no set() method because the get() method both gets and sets the cache values.

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Contracts\Cache\ItemInterface;

$cache = new FilesystemAdapter();

// The callable will only be executed on a cache miss.
$value = $cache->get('my_cache_key', function (ItemInterface $item): string {
    $item->expiresAfter(3600);

    // ... do some HTTP request or heavy computations
    $computedValue = 'foobar';

    return $computedValue;
});

echo $value; // 'foobar'

// ... and to remove the cache key
$cache->delete('my_cache_key');

Cache stampede

When lots of users request data from a cache at the same time, right after it expires, it can cause a rush of work (a "stampede") for the server as it tries to recreate the data. This can slow down the server. To prevent this, there are two solutions:

  1. Locking: This is like saying only one person can update the cache at a time. This way, when the cache data expires, only one server process is allowed to make new data, and others have to wait. This is already set up for you, so you don't need to do anything extra.

  2. Early Recompute with Probabilistic Early Expiration: Instead of waiting for the cache data to expire, it's sometimes recreated earlier. This is done randomly for some users while others still get the old cache data.

Available cache adapters: apcy, array, memcahcd, pdo, redis…

Generic Caching (PSR-6)

Item

A single unit of information stored as a key/value pair

Pool

A logical repository of cache items. All cache operations (saving items, looking for items, etc.) are performed through the pool. Applications can define as many pools as needed.

Adapter

It implements the actual caching mechanism to store the information in the filesystem, in a database, etc.

Basic usage

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

// create a new item by trying to get it from the cache
$productsCount = $cache->getItem('stats.products_count');

// assign a value to the item and save it
$productsCount->set(4711);
$cache->save($productsCount);

// retrieve the cache item
$productsCount = $cache->getItem('stats.products_count');
if (!$productsCount->isHit()) {
    // ... item does not exist in the cache
}
// retrieve the value stored by the item
$total = $productsCount->get();

// remove the cache item
$cache->deleteItem('stats.products_count');

Cache invalidation

Using cache tags

Each tag is a plain string identifier that you can use at any time to trigger the removal of all items associated with this tag.

Cache expiration

PreviousHttp Kernel ComponentNextSymfony history