Basic PHP-APC Usage

apc is an in memory cache for PHP applications. It is similar to memcached.

Installing php-apc

  1. Install Alternative PHP Cache
    apt-get install php-apc

  2. Edit the apc configuration file
    nano /etc/php5/conf.d/20-apc.ini

You can increase the shm_size to something like 128 if you have enough memory.

1
2
3
4
extension=apc.so
apc.enabled=1
apc.shm_size=16
apc.write_lock=1
  1. Restart php-fpm
    /etc/init.d/php5-fpm restart

Code
http://www.php.net/manual/en/ref.apc.php

1
2
3
4
<?php
apc_store('key', 'value');
var_dump(apc_fetch('key'));
?>

How about an array?

1
2
3
4
<?php
apc_store('key', array('value 1', 'value 2'));
var_dump(apc_fetch('key'));
?>

Clearing the cache.

1
apc_clear_cache();

Or delete a specific key.

1
apc_delete('key');