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
Tweaked some examples
  • Loading branch information
javiereguiluz committed Apr 27, 2016
commit e1bce892addd56d4f89190b5745ff60652178fc0
8 changes: 4 additions & 4 deletions components/cache/cache_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ 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');
$numProducts = $cache->getItem('stats.num_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);
$numProducts->set(4711);

// storing an array
$cachedNumProducts->set(array(
$numProducts->set(array(
'category1' => 4711,
'category2' => 2387,
));
Expand All @@ -56,7 +56,7 @@ Then, use the ``set($value)`` method to set the data stored in the cache item::
The key and the value of any given cache item can be obtained with the
corresponding *getter* methods::

$cacheItem = $cache->getItem('logged_users');
$cacheItem = $cache->getItem('exchange_rate');
// ...
$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();
Expand Down
2 changes: 1 addition & 1 deletion components/cache/cache_pools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
are independent from the actual cache implementation. Therefore, applications
can keep using the same cache pool even if the underlying cache mechanism
changes from a filesystem based cache to a Redis or database based cache.
changes from a file system based cache to a Redis or database based cache.

Creating Cache Pools
--------------------
Expand Down
12 changes: 6 additions & 6 deletions components/cache/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ a filesystem-based cache, instantiate :class:`Symfony\\Component\\Cache\\Adapter
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
$cachedNumProducts = $cache->getItem('stats.num_products');
$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
$cachedNumProducts->set(4711);
$cache->save($cachedNumProducts);
$numProducts->set(4711);
$cache->save($numProducts);

// retrieve the cache item
$cachedNumProducts = $cache->getItem('stats.num_products');
$numProducts = $cache->getItem('stats.num_products');
// check whether the item exists in the cache
$isCached = $cachedNumProducts->isHit();
$isCached = $numProducts->isHit();
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 check anything here, we just simply store a boolean. What about:

// retrieve the cache item $numProducts = $cache->getItem('stats.num_products'); if (!$numProducts->isHit()) { // ... item does not exists in the cache } // retrieve the value stored by the item $total = $numProducts->get();
// retrieve the value stored by the item
$numProducts = $cachedNumProducts->get();
$total = $numProducts->get();

// remove the cache item
$cache->deleteItem('stats.num_products');
Expand Down