Small wrapper of some apc features.
You can clone this repository or use composer, because APC is available on Packagist!
{
"require": {
"malenki/apc": "dev-master",
}
}
While you instanciate an object of this class, you must give at least one argument to set the key name:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected'); // TTL = 0 by default here
You can give second argument, an positive integer, for the time to live duration in seconds:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 60); // Given TTL is one minute
Very easy, you have the choice by using set()
method or the magick setter value
:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->set('foo');
//or
$apc->value = 'foo';
As you have seen into previous case, you can use for getting value two ways: get()
method or magic getter value
.
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
var_dump($apc->get());
//or
var_dump($apc->value);
You may print content into string context too:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->value = 'foo';
echo $apc; // will print "foo"
If the value is not a scalar, print_r()
function is used.
You can force removing value from APC cache using delete()
method or magic unset()
:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->set('foo');
$apc->delete();
// or
unset($apc->value);
You can test if a value exists before doing something with it. You have to call exists()
method or magic isset()
:
use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
// using method
if(!$apc->exists())
{
$apc->value = 'foo';
}
// or magic isset()
if(!isset($apc->value))
{
$apc->value = 'foo';
}
You can clean all or just some part like user
or opcode
:
use \Malenki\Apc;
Apc::clean(); // clean all
// or
Apc::clean('user'); // clean user cache type
// or
Apc::clean('opcode'); // clean opcode cache type