Skip to content

Adding documentation about the simple cache PSR-16 implementation #7417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 117 additions & 13 deletions components/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
The Cache Component
===================

The Cache component provides an extended `PSR-6`_ implementation for adding
cache to your applications. It is designed to have a low overhead and it
ships with ready to use adapters for the most common caching backends.
The Cache component provides an extended `PSR-6`_ implementation as well as
a `PSR-16`_ "Simple Cache" implementation for adding cache to your applications.
It is designed to have a low overhead and it ships with ready to use adapters
for the most common caching backends.

.. versionadded:: 3.1
The Cache component was introduced in Symfony 3.1.

.. versionadded:: 3.3
The PSR-16 "Simple Cache" implementation was introduced in Symfony 3.3.

Installation
------------

Expand All @@ -21,11 +25,27 @@ You can install the component in 2 different ways:
* :doc:`Install it via Composer </components/using_components>` (``symfony/cache`` on `Packagist`_);
* Use the official Git repository (https://github.com/symfony/cache).

Key Concepts
------------
Cache (PSR-6) Versus Simple Cache (PSR-16)
------------------------------------------
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Is this small section accurate? Everything else is straightforward - I just wanted you to check this one spot! Thanks!


This component includes *two* different approaches to caching:

:ref:`PSR-6 Caching <cache-component-psr6-caching>`:
A fully-featured cache system, which includes cache "pools", more advanced
cache "items", and :ref:`cache tagging for invalidation <TODO>`.

:ref:`PSR-16 Simple Caching <cache-component-psr16-caching>`:
A simple way to store, fetch and remove items from a cache.

Both methods support the *same* cache adapters and will give you very similar performance.

.. _cache-component-psr6-caching:

Before starting to use the Cache component, it's important that you learn the
meaning of some key concepts:
More Advanced Caching (PSR-6)
-----------------------------

To use the more-advanced, PSR-6 Caching abilities, you'll need to learn its key
concepts:

**Item**
A single unit of information stored as a key/value pair, where the key is
Expand All @@ -39,11 +59,11 @@ meaning of some key concepts:
filesystem, in a database, etc. The component provides several ready to use
adapters for common caching backends (Redis, APCu, etc.)

Basic Usage
-----------
Basic Usage (PSR-6)
-------------------

This component is an implementation of `PSR-6`_, which means that its basic API
is the same as defined in the standard. Before starting to cache information,
This part of the component is an implementation of `PSR-6`_, which means that its
basic API is the same as defined in the standard. Before starting to cache information,
create the cache pool using any of the built-in adapters. For example, to create
a filesystem-based cache, instantiate :class:`Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter`::

Expand Down Expand Up @@ -71,14 +91,98 @@ Now you can create, retrieve, update and delete items using this cache pool::
// remove the cache item
$cache->deleteItem('stats.num_products');

Advanced Usage
--------------
For a list of all of the supported adapters, see :doc:`/components/cache/cache_pools`.

Advanced Usage (PSR-6)
----------------------

.. toctree::
:glob:
:maxdepth: 1

cache/*

.. _cache-component-psr16-caching:

Simple Caching (PSR-16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move the simple cache part (PSF-16) before the complex cache part (PSR-6). That's the usual practice in docs: simple first and then build up to complex.

-----------------------

This part of the component is an implementation of `PSR-16`_, which means that its
basic API is the same as defined in the standard. First, create a cache object from
one of the built-in cache clases. For example, to create a filesystem-based cache,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clases -> classes

instantiate :class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache`::

use Symfony\Component\Cache\Simple\FilesystemCache;

$cache = new FilesystemCache();

Now you can create, retrieve, update and delete items using this object::

// save a new item in the cache
$cache->set('stats.num_products', 4711);

// or set it with a custom ttl
// $cache->set('stats.num_products', 4711, 3600);

// retrieve the cache item
if (!$cache->has('stats.num_products')) {
// ... item does not exists in the cache
}

// retrieve the value stored by the item
$numProducts = $cache->get('stats.num_products');

// or specify a default value, if the key doesn't exist
// $numProducts = $cache->get('stats.num_products', 100);

// remove the cache key
$cache->deleteItem('stats.num_products');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleteItem() should be delete() ?


// clear *all* cache keys
$cache->clear();

You can also work with multiple items at once::

$cache->setMultiple([
'stats.num_products' => 4711,
'stats.num_users' => 1356,
]);

$stats = $cache->getMultiple([
'stats.num_products',
'stats.num_users',
]);

$cache->deleteMultiple([
'stats.num_products',
'stats.num_users',
]);

Available Simple Cache (PSR-16) Classes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following cache adapters are available:

.. tip::

To find out more about each of these classes, you can read th
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can read theee

:doc:`PSR-6 Cache Pool </components/cache/cache_pools>` page. These "Simple"
(PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but
each share constructor arguments and use-cases.

* :class:`Symfony\\Component\\Cache\\Simple\\ApcuCache`
* :class:`Symfony\\Component\\Cache\\Simple\\ArrayCache`
* :class:`Symfony\\Component\\Cache\\Simple\\ChainCache`
* :class:`Symfony\\Component\\Cache\\Simple\\DoctrineCache`
* :class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache`
* :class:`Symfony\\Component\\Cache\\Simple\\MemcachedCache`
* :class:`Symfony\\Component\\Cache\\Simple\\NullCache`
* :class:`Symfony\\Component\\Cache\\Simple\\PdoCache`
* :class:`Symfony\\Component\\Cache\\Simple\\PhpArrayCache`
* :class:`Symfony\\Component\\Cache\\Simple\\PhpFilesCache`
* :class:`Symfony\\Component\\Cache\\Simple\\RedisCache`
* :class:`Symfony\\Component\\Cache\\Simple\\TraceableCache`

.. _`PSR-6`: http://www.php-fig.org/psr/psr-6/
.. _`PSR-16`: http://www.php-fig.org/psr/psr-16/
.. _Packagist: https://packagist.org/packages/symfony/cache
69 changes: 65 additions & 4 deletions components/cache/cache_pools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
single: Redis Cache
single: PDO Cache, Doctrine DBAL Cache

Cache Pools
===========
Cache Pools & Supported Adapters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

& -> and ?

================================

Cache Pools are the logical repositories of cache items. They perform all the
common operations on items, such as saving them or looking for them. Cache pools
Expand Down Expand Up @@ -88,8 +88,9 @@ This adapter stores the contents in the memory of a Redis server. Unlike the APC
adapter, it's not limited to the shared memory of the current server, so you can
store contents in a cluster of servers if needed.

It requires to have installed Redis and have created a connection that implements
the ``\Redis``, ``\RedisArray``, ``\RedisCluster`` or ``\Predis`` classes::
Before you start, make sure you have Redis running and have created a connection
that implements the ``\Redis``, ``\RedisArray``, ``\RedisCluster`` or ``\Predis``
classes::

use Symfony\Component\Cache\Adapter\RedisAdapter;

Expand All @@ -108,6 +109,46 @@ helper allows creating a connection to a Redis server using a DSN configuration:

$redisConnection = RedisAdapter::createConnection('redis://localhost');

$cache = new RedisAdapter($redisConnection);

See the method's docblock for more options.

Memcached Cache Adapter
~~~~~~~~~~~~~~~~~~~~~~~

This adapter stores the contents into a set of `Memcached`_ servers.

Before you start, make sure you have Memcached running and have created a
:phpclass:`Memcached` object::

use Symfony\Component\Cache\Adapter\MemcachedAdapter;

$cache = new MemcachedAdapter(
// the object that stores a valid connection to your Memcached servers
\Memcached $client,
// the string prefixed to the keys of the items stored in this cache
$namespace = '',
// in seconds; applied to cache items that don't define their own lifetime
// 0 means to store the cache items indefinitely (i.e. until the Memcached memory is deleted)
$defaultLifetime = 0
);

The :method:`Symfony\\Component\\Cache\\Adapter\\Memcached::createConnection`
helper allows creating a connection to a pool of Memcached server using a DSN configuration::

$memcachedClient = MemcachedAdapter::createConnection(
'memcached://user:pass@localhost?weight=33',

// options, including username, password and Memcached::OPT_* options
['persistent_id' => '_products_cache']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long array syntax here.

);
$memcachedClient = MemcachedAdapter::createConnection([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here too, long array syntax

array('192.168.1.100', 11211, 33),
array('192.168.1.101', 11211, 33)
]);

$cache = new MemcachedAdapter($memcachedClient);

See the method's docblock for more options.

PDO & Doctrine DBAL Cache Adapter
Expand Down Expand Up @@ -156,6 +197,25 @@ where it was missing. Since it's not possible to know the expiry date and time
of a cache item, the second optional argument of ``ChainAdapter`` is the default
lifetime applied to those cache items (by default it's ``0``).

Traceable Adapter
~~~~~~~~~~~~~~~~~

This adapter wraps another adapter, and allows you to read a report of all of the
"calls" made to the adapter, including how long actions took, cache hits, misses
and more::

use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$fileCache = new FilesystemAdapter();

$cache = new TraceableAdapter(array($fileCache));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No array wrapper


// work with the $cache adapter like normal

// returns an array of TraceableAdapterEvent describing the calls
$events = $cache->getCalls();

Proxy Cache Adapter
~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -279,3 +339,4 @@ when all items are successfully deleted)::
$cacheIsEmpty = $cache->clear();

.. _`Doctrine Cache`: https://github.com/doctrine/cache
.. _`Memcached`: http://php.net/manual/en/book.memcached.php