Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added the docs for the cache items
  • Loading branch information
javiereguiluz committed Apr 25, 2016
commit 401410da364294506afd600ef2cb5133f3002048
118 changes: 118 additions & 0 deletions components/cache/cache_items.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
.. index::
single: Cache Item
single: Cache Expiration
single: Cache Exceptions

Cache Items
===========

Cache items are each one of the information units stored in the cache as a
Copy link
Member

Choose a reason for hiding this comment

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

Cache items are each one of the information units (or looks strange to me)

key/value pair. In the Cache component they are represented by the
:class:`Symfony\Component\Cache\CacheItem` class.

Cache Item Keys and Values
--------------------------

The **key** of a cache item is a UTF-8 encoded string which acts as its
identifier, so it must be unique for each cache pool. The PSR-6 standard limits
the key length to 64 characters, but Symfony allows to use longer keys (they are
encoded internally to reduce their size).
Copy link
Member

Choose a reason for hiding this comment

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

They are not encoded for all adapters but still the length is not limited. I suggest removing these brackets.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd advocate for keeping to 64 characters anyway, for interoperability (the whole point of PSR interfaces...)

Copy link
Member Author

Choose a reason for hiding this comment

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

If Symfony follows this 64-char limit internally and it doesn't enforce it externally, I think we should remove this information.

Copy link
Contributor

Choose a reason for hiding this comment

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

It does not matter what Symfony Cache does internally. The interface only guarantees that keys up to 64 characters will work. Say, the user swap out Symfony Cache for another PSR-6 cache, things might break if they go over that limit. And we should encourage users to stay within the limit - warn them of the risk of doing otherwise.


You can freely chose the keys, but they can only contain letters (A-Z, a-z),
Copy link
Member

Choose a reason for hiding this comment

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

they can -> they should? (because we allow any other non-reserved chars)

Copy link
Contributor

Choose a reason for hiding this comment

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

chose -> choose

numbers (0-9) and the ``_`` and ``.`` symbols. Other common symbols (such as
``{``, ``}``, ``(``, ``)``, ``/``, ``\`` and ``@``) are reserved for future uses.
Copy link
Member

Choose a reason for hiding this comment

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

by PSR-6?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we link to the relevant sections in PSR-6?


The **value** of a cache item can be any data represented by a type which is
serializable by PHP, such as basic types (strings, integers, floats, boolean,
nulls), arrays and objects.
Copy link
Member

Choose a reason for hiding this comment

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

nulls (not s)


If it is not possible to return the exact saved value for any reason, implementing libraries MUST respond with a cache miss rather than corrupted data.

Creating Cache Items
--------------------

Cache items are created with the ``getItem($key)`` method of the cache pool. The
argument is the key of the item::

// $cache pool object was created before
$cachedNumProducts = $cache->getItem('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.

$pool->getItem(...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does num stand for a total quantity or an identifier? In case of quantity I'd name it 'stats.count_products'.


Then, use the ``set($value)`` method to set the data stored in the cache item::
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 an API link here


// storing a simple integer
$cachedNumProducts->set(4711);

// storing an array
$cachedNumProducts->set(array(
'category1' => 4711,
'category2' => 2387,
));

.. note::

Creating a cache item and setting its value is not enough to save it in the
cache. You must execute the ``save($cacheItem)`` method explicitly on the
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 put arguments in the parenthesis when refering to methods

cache pool.

The key and the value of any given cache item can be obtained with the
corresponding *getter* methods::

$cacheItem = $cache->getItem('logged_users');
// ...
$key = $cacheItem->getKey();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a difference between $key and 'logged_users'?

$value = $cacheItem->get();

Cache Item Expiration
~~~~~~~~~~~~~~~~~~~~~

By default cache items are stored "permanently", which in practice means "as long
as allowed by the cache implementation used".
Copy link
Member

Choose a reason for hiding this comment

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

as long as the cache pool is not cleared also?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's already understood. This is talking about expiration. Clearing is a different matter.


However, in some applications it's common to use cache items with a shorter
lifespan. Consider for example an application which caches the latest news just
for one minute. In those cases, use the ``expiresAfter()`` method to set the
number of seconds to cache the item::

$latestNews = $cache->getItem('latest_news');
$latestNews->expiresAfter(60); // 60 seconds = 1 minute

// this method also accepts \DateInterval instances
$latestNews->expiresAfter(DateInterval::createFromDateString('1 hour'));

Cache items define another related method called ``expiresAt()`` to set the
exact date and time when the item will expire::

$mostPopularNews = $cache->getItem('popular_news');
$mostPopularNews->expiresAt(new \DateTime('tomorrow'));

Cache Item Hits and Misses
--------------------------

Using a cache mechanism is important to improve the application performance, but
it should not be required to make the application work. In fact, the Cache
Copy link
Member

Choose a reason for hiding this comment

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

What's the "cache standard"?

standard states that caching errors should not result in application failures.

In practice this means that the ``getItem()`` method always returns an object
which implements the ``Psr\Cache\CacheItemInterface`` interface, even when the
cache item doesn't exist. Therefore, you don't have to deal with ``null`` values.
Copy link
Member

Choose a reason for hiding this comment

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

This allows caching e.g. null or false values?


In order to decide if the returned object is correct or not, caches use the
concept of hits and misses:

* **Cache Hits** occur when the requested item is found in the cache, its value
is not corrupted or invalid and it hasn't expired;
* **Cache Misses** are the opposite of hits, so they occur when the item is not
found in the cache, its value is corrupted or invalid for any reason or the
item has expired.

Cache item objects define a boolean ``isHit()`` method which returns ``true``
for cache hits::

$latestNews = $cache->getItem('latest_news');
$latestNews->expiresAfter(60);
Copy link
Member

Choose a reason for hiding this comment

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

not sure this example is the good one here, i'd suggest something like the following, which is the canonical snippet when dealing with arbitrary cached values to me:

$latestNews = $cache->getItem('latest_news'); if (!$latestNews->isHit()) { $value = //... do some heavy computation $pool->save($latestNews->set($value)); } else { $value = $latestNews->get(); } 

// check the item a few seconds after creating it
$isHit = $latestNews->isHit(); // true

// check the item 10 minutes after creating it
$isHit = $latestNews->isHit(); // false
1 change: 1 addition & 0 deletions components/cache/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cache
:maxdepth: 2

introduction
cache_items
7 changes: 3 additions & 4 deletions components/cache/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ 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;
* **Item**, a single unit of information stored as a key/value pair, where the
key is the unique identifier of the information and the value is its contents;
* **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.
Expand All @@ -58,7 +57,7 @@ Now you can create, retrieve, updated and delete items using this cache pool::
$cachedNumProducts = $cache->getItem('stats.num_products');

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

// retrieve the cache item
Expand Down