There are some concepts you should understand about using cache in PHP that have helped identify the advantages of each of the possibilities.
Types of cache
- Archives
- Database
- Shared memory
- Memory RAM
- Objects and variables
- Opcode
Archives
File caching is used in cases where a processing always generates the same result. The cache is generated every time there is an update, and the system will verify the existence of the cache avoiding unnecessary processing.
This type of cache is used in photo thumbnails, database pages, but they are not constantly updated as blogs, etc.
Database
Database caching can be done in several ways, one of them is by SGBD
. It is used when a database query is performed many times and the result brings back a considerable amount of records.
An example of using Mysql is using the SQL_CACHE command
Replacing a query like
SELECT id, item, valor FROM vendas WHERE id > 0;
To
SELECT SQL_CACHE id, item, valor FROM vendas WHERE id > 0;
Note: This is just one of the ways.
Shared memory
It is the possibility to share the data in memory with various running processes, thus facilitating that an already processed data does not need to be processed again by another process.
RAM Disk
Often the OS misuses RAM memory, using only part of the existing one leaving the other part standing still. Therefore, it is possible to use this "stopped" memory as a storage medium creating virtual disks. These disks have much higher read and write speeds because they are primary memory. What makes access to information 50 to 300 times faster than reading on a hard drive.
Objects and variables
The cache of objects and variables also have several ways to be done. It can be through file serialization, storage in memory whether on the server or even on the client, etc.
The use of object and variable caching requires care with the reliability and accuracy of the stored data. If objects can have updated parameters, look for a consistent way to keep the updated cache as well.
To keep the cache up to date, use non-redundant keys that make it easy to identify an object. This enables methods update
or insert
can delete and re-cache, or just overwrite depending on the cache system adopted.
Caching entire objects can cause problems by storing not only information, but everything about the object. You can use methods such as object serialization using magic methods __sleep
and __wakeup
to store only object attributes. When adding to the cache, use the function serialize()
to obtain the object string, and unserialize()
to obtain the object instance through a serialized string.
Opcode
Every time a PHP script is run the interpreter will compile into code that the machine is able to understand. The result is a bytecode called opcode (Operation Code). In any request the PHP file is compiled.
Now imagine the situation of an application made in a very large framework, which performs several processes until the end of the request. Will be multiple files compiled at any request for a often simple task.
Memcache and APC
Memcache and APC make it possible to cache in primary memory to store various types of data, such as variables, objects, session data, photo thumbnails, etc. Allows caching on distributed servers, but has no Opcode cache.
Material about Memcache 01
APC has the same features as memcache, but does not support distributed servers, and makes it possible to use Opcode caching.
Material on APC
Each application may vary by application or server.
An observation of your code is in place of apc_add
use apc_store
, for the apc_add
just add, while the apc_store
override if key already exists.
Script to compare speed between accelerators
List of "accelerators" for PHP
Forget about APC. It won’t be supported in php 5.5. sad end, but reality. If you’re looking for alternatives, try xcache and eacelerator.
– BrenoZan
APC will not die with PHP 5.5. Simply from this version there will be another method natively opcode caching, Zend Optimizer+, integrating the core of the language. It is almost the same as what happened with the PDO that from a library/module to the part was integrated to the 5.3 being provided only its drivers.
– Bruno Augusto
Yes I know, but functions with specific calls to apc functions like apc_store() will be discontinued, so whoever has them has to refactor the code by migrating to the equivalent in zend Optimizer, starting a code now with apc would not be a good one
– BrenoZan
Indeed, but every programmer should always keep an eye on the changelog. Programmers who host applications on shared servers even have a much longer time to adapt given the shameful speed with which versions are updated here. And maybe I’m talking nonsense but it seems to me that this option will be native, even native, without it being necessary that programmer do anything.
– Bruno Augusto