-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from 2 commits
7582785
21da04e
05b7ba9
401410d
6acd591
b1c5b12
c6917bd
fc84df8
599ed0a
687910d
2d71222
6d21b01
e1bce89
81ec988
c44b7c2
11549e2
930a2d1
94011a7
cf07d4a
f2234ed
1628349
657659f
50f0d58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Cache | ||
===== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
introduction |
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. | ||
|
||
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.) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 [...] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adapters are pools, should we document that as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable naming is confusing... What about There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/ |
There was a problem hiding this comment.
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?