ED php framework :: Tutorial #7
Page caching :: Per page
The following code can be applied to any single page. Place at the very top of the page which you want to cache the following 2 constants: CACHE_ENABLED and CACHE_TTL.

snippet/page_caching/
product_info.php
<?php define("CACHE_ENABLED", TRUE); ?>
<?php define("CACHE_TTL", 24*60); ?>
 
<?php include "product_info.php.script"; $page = TPage::create(); ?>
 
<html>
<head>
<?php $page->head->render(Array(
                             "Title" => "Product Info"
                           ));
?>
</head>
 
<body>
 
<?php $page->control1->render(Array(
                                 "Text" => "Check counter",
                                 "Class" => "cssDemoButton",
                                 "Style" => "width:200px"
                                )); ?>
 
<?php $page->control2->render(Array(
                                  "Text" => "Increment counter",
                                  "Class" => "cssDemoButton",
                                  "Style" => "width:200px"
                                 )); ?>
 
<?php $page->controlN->render(Array(
                                   "Text" => "Remove counter",
                                   "Class" => "cssDemoButton",
                                   "Style" => "width:200px"
                                  )); ?>
 
<?php $page->finalize(); ?>
 
</body>
 
</html>
And that's it, the 2nd constant is optional. 24*60 means 1 hour. In case you don't set it, the framework will use 1 hour as default value. It means after 1 hour the page will expire from the cache.


Page caching :: Set global rule
The following code sets global rules. Open your appconfig.inc and place at the very top the following block.

snippet/page_caching/
appconfig.inc
<?php
 
    /*********************************************************************************
     *                             CACHE
     *********************************************************************************/
    if (!defined('CACHE_ENABLED')) define("CACHE_ENABLED", TRUE);
    if (!defined('CACHE_TTL')) define("CACHE_TTL", 24*60); // 24h
 
    define("CACHE_INCLUDE_LIST", serialize(Array(
           "/demo/",
           "/tutorials/"
    )));
 
    define("CACHE_EXCLUDE_LIST", serialize(Array(
           "/members/",
           "/admin/",
           "admin.mysite.com"
    )));
 
 
    /*********************************************************************************
     *                             LOAD CONTROLLER
     *********************************************************************************/
    include_once("system/controller.inc");
 
 
 
    /*********************************************************************************
     *                             CONFIG APPLICATION
     *********************************************************************************/
    $application = TApplication::createInstance();
    $application->setName("myapp");
    $application->setCheckForSqlInjection(TRUE);
    $application->setDevMode(TRUE); // TRUE for development, FALSE for production
    $application->setDefaultLocale("en");
 
    // more configuration
?>
If you don't set CACHE_INCLUDE_LIST and CACHE_EXCLUDE_LIST then every single page will be cached. If you set only CACHE_INCLUDE_LIST then only the pages that match it's list will be cached. Imagine somebody has called for

http://www.mysite.com/demo/index.php

then because "/demo/" is part of the url it will be cached. The comparison is done by the built-in PHP function "stripos" like:
if (stripos("http://www.mysite.com/demo/index.php", "/demo/") !== FALSE) {
    // eligible for the cache
}
The same logic is applied for the exclusion list as well.


How it works
When a page is eligible for the cach the framework saves it under "protected/persistent/cache_state/". For every page it saves 2 files - xxx.info and xxx.cache. The first one contains the key(url) and the 2nd the content. You can go and delete them any time you want.

If a page is eligible then it won't be saved there if:
- $_POST contains any parameter
- it is an Ajax request

When creating the key for the url the framework checks for 2 more things:
- current locale(if the current language is "en", "fr", "de", etc.)
- if the site visitor is logged-in.

It is very important, when the developer authenticates the visitor to set a unique ID in the session through $page->user->setId("123456");

At the end for a visitor who used the french locale and is logged-in the key will look like:

(fr,123456)http://www.mysite.com/demo/index.php

and the framework will start caching the pages only for him/her. This may result in a 1000s of pages. In general it is not a good idea to cache such kind of pages.