How interesting is APC to use? Is it recommended to use it with objects?

8

I have read about the PHP opcode cache, the APC. Most of the materials I found to read, although good, only explain the installation / use of APC functions.

What I would like to know is:

  • How interesting is APC to use?
  • Is it recommended to use the APC with objects?

Example:

<?php
class Test
{
    private $someAttribute;

    public function __construct( $someAttribute )
    {
        $this->someAttribute = $someAttribute;
    }

    public function getSomeAttribute()
    {
        return $this->someAttribute;
    }
}

if ( !apc_exists( 'test' ) ) {
    $data = new Test( 'testing...' );
    apc_add( 'test', $data, 120 );
} else {
    $data = apc_fetch( 'test' );
}

By imagining that I can create more than one instance of my object, such as to persist data in methods of update() , insert() , etc, would this "kill" the cache idea in this example?

I'm confused by the use of APC;

    
asked by anonymous 11.02.2014 / 21:17

2 answers

8

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

    
12.02.2014 / 01:23
0

The APC will only create a cache in local memory, ie if your site expands and needs to have multiple servers, a server's cache will not be seen by other servers.

In this case it might be better to use memcached or even MySQL with HEAP tables. The problem with this solution is that it consumes RAM which if you have too much data can be missing from the servers. Also, if you have to restart the memcached or MySQL server, the data stored in RAM is lost.

In this case you can use Redis which also stores on disk. However, if you are already using MySQL 5.6 or compatible, this version already offers memcached compatible interface for data stored in INNODB tables. In this case it is the most recommended.

We commented on this last year on the podcast Lately in PHP 32

    
12.02.2014 / 06:57