# 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.

```php
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

```php
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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://senens-organization.gitbook.io/senenhermida.docs/symfony/cache-component.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
