Skip to content

Added the documentation for the Cache component #6515

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 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions components/cache/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Cache
=====

.. toctree::
:maxdepth: 2

introduction
74 changes: 74 additions & 0 deletions components/cache/introduction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
.. index::
single: Cache
single: Performance
single: Components; Cache

The Cache Component
===================

The Cache component provides a strict `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.

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

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

.. note::

In Symfony applications this component is integrated (and enabled by
default) through the FrameworkBundle.
Copy link
Member

Choose a reason for hiding this comment

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

we don't have this note for other components as well, what about removing it here?


Key Concepts
------------

Before starting to use the Cache component, it's important that you learn the
meaning of some key concepts:

* **Item**, a single key/value pair stored in the cache. They key is the unique
identifier of the item and cannot be changed. The value can be changed at any
time and it can contain any value which can be serialized by PHP;
* **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. The component provides
several ready to use adapters for common caching backends (Redis, APCu, etc.)
Copy link
Member

Choose a reason for hiding this comment

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

let's use a definition list here:

**Item**
    A single unit [...]
**Pool**
    A logical repository [...]
**Adapter**
    A [...]

Copy link
Member

Choose a reason for hiding this comment

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

Adapters are pools, should we document that as well?

Copy link
Contributor

@teohhanhui teohhanhui Jun 14, 2016

Choose a reason for hiding this comment

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

It should be the other way round, i.e. pools (a concept) are actually adapters, but not all adapters are pools.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wait... Here we're not talking about the "pools" concept introduced in the FrameworkBundle. Overloaded terms are far too confusing...


Basic Usage
-----------

This component is a strict implementation of `PSR-6`_, which means that the 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`
and define the namespace used to store the items as the first argument::

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter('app.cache')

Now you can create, retrieve, updated and delete items using this cache pool::

// create a new item by trying to get it from the cache
$numProducts = $cache->getItem('stats.num_products');
Copy link
Contributor

Choose a reason for hiding this comment

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

The variable naming is confusing... What about $numProductsCacheItem?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch. I refactored this example before and forgot to update that part. It's updated now. Thanks.


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

// retrieve the cache item
$cachedNumProducts = $cache->getItem('stats.num_products');
// check whether the item exists in the cache
$isCached = $cachedNumProducts->isHit();
// retrieve the value stored by the item
$numProducts = $cachedNumProducts->get();

// remove the cache item
$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.

let's add links to the other sections in a new section at the end of the introduction

.. _`PSR-6`: http://www.php-fig.org/psr/psr-6/
1 change: 1 addition & 0 deletions components/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The Components
asset/index
browser_kit/index
class_loader/index
cache/index
config/index
console/index
css_selector
Expand Down
4 changes: 4 additions & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

* :doc:`/components/browser_kit/introduction`

* :doc:`/components/cache/index`

* :doc:`/components/cache/introduction`

* :doc:`/components/class_loader/index`

* :doc:`/components/class_loader/introduction`
Expand Down