There are some concepts you should understand about using caching in PHP that helped you identify the advantages of each of the possibilities.
Cache Types
- Files
- Database
- Shared Memory
- RAM memory
- Objects and variables
- Opcode
Files
File caching is used in cases where processing always generates the same result. The cache is generated every time there is an update, and the system will check for cache to avoid unnecessary processing.
This type of cache is used in photo thumbnails, pages with data stored in the database, but not constantly updated as blogs, etc.
Database
The database cache can be done in several ways, one of which is by SGBD
itself. It is used when a query in the database is performed many times and the result brings a considerable amount of records.
An example of using MySQL is the use of 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 possible to share data in memory with several processes running, thus making it easier for an already processed data to be processed by another process.
RAM Disk
Often the OS misuses the RAM, using only one part of the existing leaving the other part stopped. Therefore, you can use this "stop" memory as a storage medium by creating virtual disks. These drives have much higher read and write speeds because they are primary memory. What makes information access 50 to 300 times faster than reading on an HD.
Objects and variables
Object and variable caching also have many ways to do it. It can be by serialization in files, storage in memory either on the server or even on the client, etc.
Using 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 form of updated cache as well.
To keep the cache up-to-date, use non-redundant keys that make it easy to identify an object. This allows that update
or insert
methods can delete and re-cache, or just overwrite depending on the cache system adopted.
Storing cached whole objects can cause problems by saving not just information, but everything about the object. You can use methods like object serialization using magic methods __sleep
and __wakeup
to save only the attributes of the object. When adding in the cache, use the serialize()
function to get the object string, and unserialize()
to get the object's instance through a serialized string.
Opcode
Every time a PHP script is run the interpreter will compile into a code that the machine will be able to understand. The result is a bytecode called opcode. 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 it reaches the end of the request. It will be several files compiled every request for an often simple task.
Memcache and APC
Memcache and APC enable caching in primary memory to store various types of data, such as variables, objects, session data, thumbnails of photos, and so on. Allows caching on distributed servers, but does not have Opcode caching.
Memcache Material 01
APC has the same features as memcache, but does not support distributed servers, and enables the use of Opcode caching.
APC Material
The application of each can vary according to the application or server.
An observation of your code is instead of apc_add
using apc_store
, since apc_add
only adds, while apc_store
overrides if the key already exists.
Script to compare speed between accelerators
List of "accelerators" for PHP