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

Asked

Viewed 1,077 times

8

I’ve been reading about PHP’s opcode cache, APC. Most of the materials I found to read, although good, only explain the installation/use of APC functions.

What I’d like to know is:

  • How interesting it is to use APC?
  • It is recommended to use 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' );
}

Imagining that I can create more than one instance of my object, for example, to persist the data in methods update(), insert(), etc., This would "kill" the idea of caching in this example?

I am confused by the use of APC;

  • 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.

  • 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.

  • 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

  • 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.

2 answers

8

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

0

APC will only create a cache in local memory, that is, if your site expands and needs to have multiple servers, the cache of a server will not be viewed by other servers.

In this case it might be better to use memcached or even Mysql with HEAP tables. The problem of this solution is that it consumes RAM memory that if you have to store a lot of data may be missing on 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 recommendable.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.