Click here to Skip to main content
15,393,377 members
Articles / Programming Languages / C++
Technical Blog
Posted 26 Oct 2014

Tagged as


1 bookmarked

Caching Using Zend Framework (1st Step Towards Zero To Hero)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Oct 2014CPOL2 min read
In this blog, we will discuss how to cache your whole project.

In this blog, we will discuss how to cache your whole project. When we run our project, PHP interpreter enters into the public/index.php file and after interpreting the project source code, again it comes out through index.php file.


As in the above image, we can see index.php is the entry point and exit point so if we will start Caching and end Caching in index.php file, then our whole project will be cached. We will see how it can be possible through demo.


1st Step

You should have created project using zend framework. Here, we are using zend framework 1.12 for easy understanding. If you want to install framework and create project, so check these 2 links:


2nd Step

Create a clone of the project from which we will use for demo.

3rd Step

As we are caching whole project, we will follow #Zend_Cache_Frontend_Output method.

4th Step

Create directory called cache inside public directory and give full read write permission to cache directory.

5th Step

Add the below code in mentioned files:

  1. quickstart/public/index.php

Add this code before $application->bootstrap()->run();

// auto loader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

// config
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

// Setup caching option
$frontendOptions = array(
    'lifetime' => 7200, // cache lifetime of 2 hours
    'automatic_serialization' => true
$backendOptions = array(
    'cache_dir' => 'cache/' // Directory where to put the cache files
$cache = Zend_Cache::factory('Output',
$registry->set('appCacheObj', $cache);

$appCache = Zend_Registry::get('appCacheObj');

$cacheId = 'myCacheId'.str_replace("/", "_", $_SERVER['REQUEST_URI']);

if(strpos($cacheId, ".php") !== -1) {
    $cacheId = explode(".php", $cacheId)[0];

//Clear cache for dynamic page
if($_SERVER['REQUEST_URI'] == "/guestbook/sign") {

//Cache start
if($appCache->start($cacheId)) {

After $application->bootstrap()->run(); add the below line:

//Cache end

2. Add the below code inside end of the public function save() of models/GuestbookMapper.php file:

//Clear cache on save action to show new data to user instead of cached data
$appCache = Zend_Registry::get('appCacheObj');

When you will browse http://quickstart.local/guestbook/sign, you will get the page like below:


You can check the cache directory which contains the cached files. After clicking on Sign Guestbook, it will clear the cache first and then it will create new cached files.

FYI: So for your project, you have to clear cache on any save action so that user will get new fetched records, otherwise you can show data from cached records.

After completing all the above steps, when you will browse your project, you will find that each page of your application is cached. Only the cached record will be cleaned if you click on any save action. So enjoy! :)

/*Hope you will love it */


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

GeneralMy vote of 5 Pin
Gaurav Aroraa27-Oct-14 9:59
professionalGaurav Aroraa27-Oct-14 9:59 
GeneralRe: My vote of 5 Pin
Biswal Tanmaya27-Oct-14 21:04
professionalBiswal Tanmaya27-Oct-14 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.