mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-22 13:13:05 +02:00
Add comments to code sample, making more clear that 'expensive_data' is a key for data in cache
This commit is contained in:
@@ -7,7 +7,7 @@ isChild: true
|
|||||||
There are times when it can be beneficial to cache individual objects in your code, such as with data that is expensive
|
There are times when it can be beneficial to cache individual objects in your code, such as with data that is expensive
|
||||||
to get or database calls where the result is unlikely to change. You can use object caching software to hold these
|
to get or database calls where the result is unlikely to change. You can use object caching software to hold these
|
||||||
pieces of data in memory for extremely fast access later on. If you save these items to a data store after you retrieve
|
pieces of data in memory for extremely fast access later on. If you save these items to a data store after you retrieve
|
||||||
them, then pull them directly from the cache for following requests you can gain a significant improvement in
|
them, then pull them directly from the cache for following requests, you can gain a significant improvement in
|
||||||
performance as well as reduce the load on your database servers.
|
performance as well as reduce the load on your database servers.
|
||||||
|
|
||||||
Many of the popular bytecode caching solutions let you cache custom data as well, so there's even more reason to take
|
Many of the popular bytecode caching solutions let you cache custom data as well, so there's even more reason to take
|
||||||
@@ -27,12 +27,16 @@ Example logic using APC:
|
|||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
|
// check if there is data saved as 'expensive_data' in cache
|
||||||
$data = apc_fetch('expensive_data');
|
$data = apc_fetch('expensive_data');
|
||||||
if (!$data)
|
if (!$data)
|
||||||
{
|
{
|
||||||
|
// data not in cache, do expensive call and save for later use
|
||||||
$data = get_expensive_data();
|
$data = get_expensive_data();
|
||||||
apc_store('expensive_data', $data);
|
apc_store('expensive_data', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print_r($data);
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Learn more about popular object caching systems:
|
Learn more about popular object caching systems:
|
||||||
|
Reference in New Issue
Block a user