This is as follow up on my previous blog post on “Why use an opcode cacher

In that post I mention you can store data in memory.
The reason why one should want to do this, is again “performance”

Just think about it, for each request, what amount of data is gotten over and over again?
Most of the time this will be data from the database. That’s why it is important to configure your database well, but that’s not what this blog post is about.

A use case where you’d want to use this technique is a configuration file you load in on each request.
As example, a small part out of the config file I use on Crazy’s Manga.

[global]
MangaLocation = /var/manga/
MangaImageLocation = /var/www/html/img/manga/
MangaImageUrl = http://img.crazytje.be/
 
[api]
ApiUrl = http://api.crazytje.be/
ApiUserName = ApiUserName
ApiPassword = ApiPassword
 
[DBConfig]
db_driver = mysqli
db_persistent = true
db_host = localhost
db_login = username
db_password = password
db_database = database
db_prefix =

This file is read in on each request, but what if we put all the variables in apc, then we can do the following:

$parmas = apc_fetch('CrazyConfig', $params_loaded);
if(!$params_loaded) {
    $params = parse_ini_file('/my/path/to/the/config', true);
    apc_store('CrazyConfig', $params);
}
....

Cool right?
So what do we save?
We save the time it takes to load and do the parse_ini_file.

This might sound ‘small’, but all small things help. On a site with allot of hits, all those small things make the difference.

© 2011 Crazy's Blog Suffusion theme by Sayontan Sinha