CakePHP and Redis cache engine on WAMP

by on August 14, 2015

cache all the things

I love cache! If you use them smart enough, caching engine could speed your application many times. I have a simple example. In one of my applications (with backend and frontend), a user could change the title of the application as an administrator (in backend). Now, every time someone opens a frontend, database must be asked for the title. I think that’s insane. Actually, almost every part of the system is configured throught the database and we can talk about tens or hundreds of configuration options (menu position, colors, default language, GoogleMaps API key, etc.). It’s important to notice that this kind of configuration is rarely changed (let’s say once a month).

In this scenario, caching engine is a must, and this is where systems like Redis are brilliant. Redis stores your data as a key-value pair. It supports not only strings, but also abstract data types like sets, lists, hastables, etc.

If any link in the article isn’t working anymore, feel free to contact me (davor [at] warriorkitty.com) and I will re-upload/change them.

Everything you will see in this article, I’ve tested on:

As you can see, it is tested for mostly x64 versions but everything should be working for x86 when you replace your x64 DLLs with x86 DLLs.

DLLs you need (special thanks to vostok4 for links):


(thread-safe – you probably want these)


(non-thread-safe – for FastCGI (with mod_fastcgi) or CLI)


After downloading DLLs, extract them and put them inside your extensions folder. (Probably in C:\wamp\bin\php\php5.5.12\ext). Also, rename them to “php_redis.dll” and “php_igbinary.dll”.

redis and igbinary in wamp

Now, when installing Redis, you have two options. Here is a link where are the installation .exe files which are pretty straight forward but the version is little outdated. Your other option is to compile Redis for Windows yourself. This article is more about connecting Redis with CakePHP so I presume you’ve already successfully installed Redis on your Windows machine. Open your redis-cli.exe (probably at C:\Program Files\Redis) in your command line and check with the KEYS * command if everything is working:

redis-cli test

OK, at this point, you need to have a working CakePHP application, WAMP and Redis installed.

The CakePHP part

Redis configuration goes to your app.php file inside config folder.

/**
 * Configure the cache adapters.
 */
'Cache' => [
        /*** edit this part ***/
	'default' => [
		'className' => 'Redis',
		'duration' => '+999 days'
	],

         ...

If you are curious what values you can configure, check the CakePHP source:

    'engine' => 'Redis',
    'prefix' => null,
    'server' => '127.0.0.1',
    'port' => 6379,
    'password' => false,
    'timeout' => 0,
    'persistent' => true

These values are pretty much OK with my development machine so I didn’t add anything more inside my app.php. Now let’s try it. It works mostly like this:

public function getTitle(){
	if(($title = Cache::read('Title')) === false){
		$title = $this->findYourTitle();
		Cache::write('Title', $title);
	}
	return $title;
}

First time you execute this function, it will ask the database for the value and store the value inside Redis. Next time you ask for the title, it will take the value from Redis and not from the database. What about changing the title? Well, inside app.php we wrote that the cache is valid for 999 days and you don’t want to wait that long. In a function which changes your title, invalidate the cache:

public function changeTitle($newTitle){
	// function which changes the title
	// don't forget to invalidate the title
	Cache::delete('Title');
}

If you didn’t understand some part of this article, you need help, or links are not working anymore, feel free to contact me.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *