PHP Programming/Caching

A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache. In PHP, caching is used to minimize page generation time.

Classification

edit

PHP provides several cache systems[1]:

Name Data stored Flush
Instance cache PHP object. Ex:
if (null === $x) {
    $x = 1;
}
Restart the script (ex: refresh the Web page).
Session cache PHP object[2] Empty the navigator cookies.
OPcache Opcode[3] opcache_reset();
APCu RAM users variables[4] apc_clear_cache();
Navigator cache Rendering CTRL + F5
ESI Web page parts Depends on the CDN or proxy server used.
Framework cache Configurations, translations Example of Symfony: php bin/console cache:clear empty the temporary var/cache files.
Proxy Entire web pages For examples, see: Varnish, HAProxy.
NoSQL database Key-value pairs For examples, see: Memcached, Redis.
ORM cache Annotations, SQL requests or their results Example with Doctrine:
php bin/console doctrine:cache:clear-metadata 
php bin/console doctrine:cache:clear-query  
php bin/console doctrine:cache:clear-result

or:

bin/console cache:pool:clear doctrine.query_cache_pool doctrine.result_cache_pool doctrine.system_cache_pool
Chain cache Everything Use each included cache flushes.

PHP basically has two main types of caching: 'output caching' and 'parser caching'. PHP 'output caching' saves a chunk of data somewhere that can later be read by another script faster than it can generate it. 'Parser caching' is specific feature. PHP is a scripting language and code is not compiled or optimized to a particular computer. Every PHP file must be parsed and that takes time. This type of time minimization is 'parser caching'.

Parser caching

edit

Include caching

edit

Example:

File:class.test.php

<?php
class test
{
    function hello()
    {
        echo "Hello world!\n";
    }
}
echo "Class loaded\n";

File:program.php

<?php
require_once("class.test.php");
$obj1 = new test;
$obj1->hello();
require_once("class.test.php");
$obj2 = new test;
$obj2->hello();

output:

Class loaded
Hello world!
Hello world!

Array caching

edit

Example:

File:program.php

<?php
global $sum_cache; 
$sum_cache=array();
function sum($nr)
{
    global $sum_cache;
    
    if (isset($sum_cache[$nr])) {
       echo "sum(".$nr.")=" . $sum_cache[$nr] . " from cache\n";
       return $sum_cache[$nr];
    }

    if ($nr <= 0) {
       $sum_cache[$nr] = 0;
    } else {
       $sum_cache[$nr] = sum($nr - 1) + $nr;
    }

    echo "sum(".$nr.")=" . $sum_cache[$nr] . " computed\n";
    return $sum_cache[$nr];
}
sum(3);
sum(4);

output:

sum(0)=0 computed
sum(1)=1 computed
sum(2)=3 computed
sum(3)=6 computed
sum(3)=6 from cache
sum(4)=10 computed

Session caching

edit

Example:

file:program.php

<?php
session_start();
function find_my_name()
{
    //perhaps some time-consuming database queries
    return "Bill"; 
}
if (isset($_SESSION["hello"])) {
    echo "cached\n";
    session_destroy();
} else {
    echo "computed\n";
    $_SESSION["hello"] = "My Name is " . find_my_name() . ".\n";
}
echo $_SESSION["hello"];

output:

computed
My Name is Bill.

output after refresh:

cached
My Name is Bill.

Shared variables

edit

Example:

file:program.php

<?php
class test
{
    var $list = array();

    function load_list()
    {
        // some less time consuming database queries
        $this->list[0]["info"] = "small info nr 1";
        $this->list[1]["info"] = "small info nr 2";
        $this->list[2]["info"] = "small info nr 3";
    }

    function load_element_detail(&$data)
    {
        // some very time consuming database queries
        $data["big"] = "added big info, maybe structure of objects";
    }

    function get_element($nr)
    {
        return $this->list[$nr];
    }

    function print_element($nr)
    {
        echo "this->list[${nr}]['info'] = '" . $this->list[$nr]['info'] . "'\n";
        echo "this->list[${nr}]['big']  = '" . $this->list[$nr]['big'] . "'\n";
    }
}

$obj = new test;

$obj->load_list();
$obj->print_element(0);

$element = &$obj->get_element(0);

$obj->load_element_detail($element);
$obj->print_element(0);

output:

$this->list[0]["info"]="small info nr 1"
$this->list[0]["big"]=""
$this->list[0]["info"]="small info nr 1"
$this->list[0]["big"]="added big info, maybe structure of objects"

Output Caching

edit

The server cache policy is included into HTTP header, visible with cURL (with option "I" for header):

curl -I http://example.org

Example:

curl -I https://en.wikibooks.org/
HTTP/2 301 
date: Sun, 02 Jan 2022 11:50:58 GMT
server: mw1429.eqiad.wmnet
x-content-type-options: nosniff
p3p: CP="See https://en.wikibooks.org/wiki/Special:CentralAutoLogin/P3P for more info."
vary: Accept-Encoding,X-Forwarded-Proto,Cookie,Authorization
cache-control: s-maxage=1200, must-revalidate, max-age=0
last-modified: Sun, 02 Jan 2022 11:50:58 GMT
location: https://en.wikibooks.org/wiki/Main_Page
content-length: 0
content-type: text/html; charset=utf-8
age: 1139
x-cache: cp3062 miss, cp3060 hit/4
x-cache-status: hit-front
server-timing: cache;desc="hit-front", host;desc="cp3060"
strict-transport-security: max-age=106384710; includeSubDomains; preload
report-to: { "group": "wm_nel", "max_age": 86400, "endpoints": [{ "url": "https://intake-logging.wikimedia.org/v1/events?stream=w3c.reportingapi.network_error&schema_uri=/w3c/reportingapi/network_error/1.0.0" }] }
nel: { "report_to": "wm_nel", "max_age": 86400, "failure_fraction": 0.05, "success_fraction": 0.0}
permissions-policy: interest-cohort=()
set-cookie: WMF-Last-Access=02-Jan-2022;Path=/;HttpOnly;secure;Expires=Thu, 03 Feb 2022 12:00:00 GMT
set-cookie: WMF-Last-Access-Global=02-Jan-2022;Path=/;Domain=.wikibooks.org;HttpOnly;secure;Expires=Thu, 03 Feb 2022 12:00:00 GMT

References

edit