Caching
Caching
Scopes
In most web platforms there are different scopes for variables and data beyond
the variable and class scopes. There are normally 4 different
scopes: Request, Session, Application and Server.
Request Scope
The request scope variables are the ones that remain since a client request was
issued until the request ends.
This is the normal level we have in PHP when we use static variables, patterns
like Singleton and any other attempt by code to keep a data alive in memory.
Session variables
In PHP you can save data on the users session variable and these data are only
for that user in that session and they will remain alive during the complete time
of that session.
This is very handy to keep user information that may be required to keep
continuty to the users experience in the site.
Server variables
PHP is not able to keep data at server level by default like other platforms can,
but that doesnt means it cant be achieved.
In PHP you can emulate server variables saving data in the database (that works
only for that server), saving the data in files or using memory dictionaries like
Redit, APC, Memcache or alike (if they are restricted to that server only).
The server variables are identified because they only belong to the server that is
running on, not necesary exclusive of the application, for which it could be used
to share data between applications on the same server.
The data use to be volatil by definition and therefore is lost when the server is
restarted.
Application variables
These are very similar to server variables and they are not supported by PHP by
default either, but they can be implemented in the same way that server
variables are.
The conceptual difference of these variables is that they are not shared between
applications, but on the other side they are shared between servers that are
running the same application.
Each time we do a data request or some data calculations that require resources.
The requests to a database require memory, CPU and in most cases disk
operations as well.
The processing of files or complex calculus also could have a big weight in
performance.
This may not be a big issue once, but when we are serving a site with high traffic
this could impact the servers performance and the response time.
Reducing the number of requests and/or calculus that are made we multiply the
improvement in performance and response time.
From there we get that if we coiuld reduce the amout of code files or templates
to 10% on 90% of the requests we could see a drastically improvement of the
site performance, or in its defect, not doing so would mean having the site slow
or not able to server the amount of requests of a decent trafic.
Of course, everything has a cost, keeping more data on cache would usually
means that we need more memory to keep them available, which could be
limited by the servers RAM that we have.
For this reason we must have careful and concience of the resources or we could
start having a problem of availablebily of cache, forcing the server to use swap
memory/virtual memory on disk, making the server go slower and heavier.
Static data
Now that we know the different levels for the variables lets see static data.
Using the functionality static in PHP we can keep data in a persistent way
during the code execution (request variables), allowing us to save on requests or
calculus that are required several times during the code execution of the request.
In Drupal 7 you can use the function drupal_static to keep those data. The
function drupal_static receives the name or key of the data, the default
value and an indication if is must clean the existing data.
In that sense this function can be seeing like a similar functionality of the set
Drupal variable_get/set set of functions.
// Use $data_list
}
?>
function another_funcion() {
$data_list = &drupal_static("shared data");
// Use $data_list and modify it in the variable
}
?>
// Use the data form $data_list and save modifications to the variable
}
?>
function another_funcion() {
global $data_list
// Use $data_list and modify it in the variable
}
?>
Drupals Cache
Drupal has a series of functions to use the generic cache system that is
implemented.
By default Drupals cache is implemented in the database, but this can be
modified so that it uses another implementation like APC, Memcache, Redit, file
system, etc.
In a future article Ill explain how to configure different type of cache in Drupal.
The advantage of Drupal cache is that the code is agnostic of the
implementation of the cache, when you program the code you dont need to
know which kind of cache are you using.
If we need to do a series of heavy calculations over some data and these data
will stay constant in the site for certain time, we can do something like the
following example to avoid calculating it on every request:
<?php
function calculate_heavy_data() {
$data_calculated = drupal_static(__FUNCTION__); // Drupal 7 version
//static $data_calculated; // Drupal 6 version
if (!isset($data_calculated)) {
if ($cache = cache_get("heavy_data")) {
$data_calculated = $cache->data;
} else {
// Do all the necesary calculations of the data and place them
again in $data_calculated
cache_set("heavy_data", $data_calculated, "cache");
}
}
return $data_calculated;
}
?>
This way the data will not be calculated on each request nor serveral times on
one request.
The use of cache is recommended, but without abuse and having in mind
estimations of how this can impact the memory and improvement on
performance, but this could be the difference between a heavy and slow site or a
fast and fully available site.