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.

<?php define("CACHE_ENABLED", TRUE); ?>
<?php define("CACHE_TTL", 24*60); ?>
<?php include "product_info.php.script"; $page = TPage::create(); ?>
<?php $page->head->render(Array(
                             "Title" => "Product Info"
<?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(); ?>
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.

     *                             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(
    define("CACHE_EXCLUDE_LIST", serialize(Array(
     *                             LOAD CONTROLLER
     *                             CONFIG APPLICATION
    $application = TApplication::createInstance();
    $application->setDevMode(TRUE); // TRUE for development, FALSE for production
    // 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


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:


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.