I decided to write this post in english because it might be utile to more people this way.
Recent experience, tough me that a good database structure is not enough when your site increases number of visitors exponential without possibility to have any time to rewrite the code or restructure your database model.
The whole ideea is to make a decisional system (algorithm) that decides whatever you use: cache system or block system. If your system uses block system, then is mandatory to generate the cache.
In the next rows i will try to present you some cache methods that are quite useful.
1. Caching the whole page
This is not a good practice when you have an online store or you have an application that requires a lot of information from database, but it can be useful for the site applications that have a lot of pages and a big number of visitors. The whole ideea is to keep your database server as free as possible. This way you can cache any page that is not often changed. A page like that can be stored with a big time buffer (example you regenerate that page twice or once per day).
For a page that is often modified, you can set the cache buffer somewhere below, i mean you could regenerate that page 1 per hour or you can generate it 6 times per hour. If the page is very often modified or it need to take online users or a number that has a small interval of changing, this time can be reduced to any measure.
2. Caching page sections
This ideea is similar with first one, but instead caching all the page, you might simply cache just a part of your web page … that means you will be able to control your modules that are grouped in one page more easily and they could have separate cache lifetime. You might be intrested to cache 3 modules in this way. 1 module with a cache life time around 10 minutes, 2nd module with a cache lifetime of 1 day, and 3rd with a cache of 1 week.
3. Caching variables
This one might be a good option for the sites with a great number of visitors, but its content is changing often. in this case you can cache each variable as a file, or the entire set of variables into a file. Either you cache one variable or you write them all in a single file, result will be the same.
3.1. Storing all variables in a single file
If you choose this type of caching then you must be aware that is the best for you separate yor sql queries / results from the process code. This way you can have a bigger control over your cache system. Also is more easy to generate your cache. Must be aware that your php structure will create separate including files that will be generated at a specified time … or simply, to make them permanent, you set them to expire in 1.. 2 ..100 years.
<?php
$variable1 = “foo_value”;
$variable2 = “foo2_value”;
?>
3.2. Storing all variables in multiple files
This way, you will not be forced to separate your sql queries, but your code will be more complicated because you will be “forced” to write more decisional conditions in your code. This can be a mess when you try to echo more variables in a single row or something like that. A second strategy would be to make a function with some paramehers like:
function my_cache_function($path_to_file,$time,$expression){
// a decisional if
if (file_exists($path_to_file) && (filectime($path_to_file)>(date(”U”)-$time))){
$my_cached_var = file_get_contents($path_to_file);
}else{
eval(\$my_cached_var = $expession);
unlink($path_to_file);
file_put_contents($path_to_file,$my_cached_var);
}
return $my_cached_var;
}
this function will allow you to write in your code expressions like:
$messages = my_cache_function(”./cache_dir/messages.cache”,60,” mysql_result(mysql_query(\”SELECT count(*) as nr FROM my_messages_table WHERE my_field=’{$user_id}’\”),0,\”nr\”)”);
In the example below i’ve shown how can you cache a variable for a time of 60 seconds. Of course, the main ideea of a cache system is to get some variables that remain constant for a long period, or they consumes a lot of resources to be shown.
3.3 Using memcached.
By using memcached, you will be able to store the variable into ram memory for a long time period.
I haven’t checked all the versions, but, if you test my ideea/ script that is presented here, you are doing at your own risk.
Tags: database load,
php,
query,
site optimisation