• Home
  • /
  • Blog
  • /
  • A guideline to handle Cache with your CakePHP set up to increase speed
Cache with your CakePHP

A guideline to handle Cache with your CakePHP set up to increase speed

Spread the love

Advertise HereCaching is an indispensable component of every web app development project handled using the CakePHP framework. Especially, if you’re inclined on developing apps that need to handle large data, caching is something that will reduce the load on the site’s server, thereby increasing its speed and performance by great bounds and leaps. If you’re the one who has just started working with CakePHP and wants to dig deeper into handling cache, then this is a blog just for you.

What will this tutorial showcase?

Dedicated to all CakePHP developers, this tutorial will serve as your guide in handling Cache with the CakePHP setup. I’ll be focusing on the process of installing memory based caching, followed by offering you a deeper understanding on caching the query results.

Firstly, let’s get to know about installing Memory Based Caching

Cache with your CakePHP

A guideline to handle Cache with your CakePHP set up to increase speed

If you consider the default functioning of PHP, you’ll find that all the PHP sessions are being stored on disk using a temp folder. In other words, every time you access a session, PHP is supposed to open the session’s file, followed by decoding the information included within it. Since disk I/O can be a costly affair, it is better to allow PHP to access the items directly from the database instead of the desk. However, if handled effectively, session-related disk I/O can do the magic for your site. Here are two approaches to handling the same:

  • Configure RAM disk on the server

Once you’re done with configuring the RAM disk, you can choose to mount the drive by updating the session.save path value in php.ini to the added disk.

  • Install a third-party software

As the second approach, you can opt for installing a third-party software like Memcached which serves as an open source caching system, allowing the storage of objects in the system memory. Once you’ve completed the successful installation of Memcached software, you can go ahead with configuring PHP for storing sessions in memory. For this, you’ll be required to update your php.ini as shown below:

session.save_handler = memcache

session.save_path = ‘tcp://127.0.0.1:11211’

Here, you need to modify the entries if you’ve chosen to install Memcached on a different host or port. Next, you need to install the PHP memcache module. In accordance to your operating system, any of the below mentioned commands will do the job:

pecl install memcache

or:

sudo aptitude install php5-memcache

Do note that your decision to choose any one of the above explained session-related disk I/O approaches will be based on whether a single or multiple servers are required for accessing the memory. If you’re looking for speeding up your single web server, then the RAM disk option can turn befitting. On the contrary, if you require more than one server for accessing the memory simultaneously, then installing the Memcached software will do the trick.

A detailed overview on caching query results

While developing web apps using CakePHP, you might have encountered several queries entering into the database which shouldn’t be happening. This increases unnecessary load on the server, making the app run slow. A viable solution for this is to override the default find() function available within the app/Model/AppModel.php file. Doing this would save the processing time that’s required by CakePHP for converting the query results into an array. The code snippet associated with the same is shown below:

<?php

class AppModel extends Model

{

public $recursive = -1;

function find($conditions = null, $fields = array(), $order = null, $recursive = null) {

$doQuery = true;

// check if we want the cache

if (!empty($fields[‘cache’])) {

$cacheConfig = null;

// check if we have specified a custom config

if (!empty($fields[‘cacheConfig’])) {$cacheConfig = $fields[‘cacheConfig’];

}

$cacheName = $this->name . ‘-‘ . $fields[‘cache’];

// if so, check if the cache exists

$data = Cache::read($cacheName, $cacheConfig);

if ($data == false) {

$data = parent::find($conditions, $fields,

$order, $recursive);

Cache::write($cacheName, $data, $cacheConfig);

}

$doQuery = false;

}

if ($doQuery) {

$data = parent::find($conditions, $fields, $order,

$recursive);

}

return $data;

}

}

Talking about the query structure, a basic query which looks like this:

<?php

$this->User->find(‘list’);

must be updated to include some caching information like this:

<?php

$this->User->find(‘list’,

array(‘cache’ => ‘userList’, ‘cacheConfig’ => ‘short’)

);

If you look at the above code, you’ll find the addition of two values viz: the cache name and cache config. Now, two final changes need to be made to app/Config/core.php. Firstly, the caching should be turned on and the cacheConfig value should be defined. Follow the below steps for implementing the same:

Step 1- Uncomment the below line of code:

<?php

Configure::write(‘Cache.check’, true);

Step 2- Add a new cache config as explained below:

<?php

Cache::config(‘short’, array(

‘engine’ => ‘File’,

‘duration’=> ‘+10 minutes’,

‘probability’=> 100,

‘path’ => CACHE,

‘prefix’ => ‘cache_short_’

));

In the above code, you can change the duration, name and define a different location for storing the cache.

That’s it!

Conclusion

Hopefully by now the process of handling cache in CakePHP would be clear to you. Do share your reviews and comments/suggestions for the above post using the comments box below.

About the Author:

Amanda Cline is serving as a professional developer at Xicom Technologies Ltd – A renowned Web Development Outsourcing Services Company. If you need to Hire CakePHP Programmer then simply get in touch with Amanda via Twitter

Advertise Here

Stanislaus Okwor is a Web Designer / Developer based in Lagos - Nigeria. He is the Director at Stanrich Online Technologies. He is knowledgeable in Content management System - Wordpress, Joomla and PHP/MySQL etc

Leave a Reply

WhatsApp chat
Verified by MonsterInsights