malenki/apc

一些APC功能的简单包装器。

dev-master 2014-04-26 15:35 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:41:35 UTC


README

一些APC功能的简单包装器。

安装

您可以克隆此存储库或使用 composer,因为 APC 在 Packagist 上可用

{
    "require": {
        "malenki/apc": "dev-master",
    }
}

实例化

在实例化此类对象时,您必须至少提供一个参数来设置键名

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected'); // TTL = 0 by default here

您还可以提供一个第二个参数,一个正整数,用于设置生存时间(以秒为单位)

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 60); // Given TTL is one minute

设置值

非常简单,您可以通过使用 set() 方法或魔法设置器 value 来选择。

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->set('foo');
//or
$apc->value = 'foo';

获取值

正如您在前面的例子中看到的,您可以使用两种方式获取值:使用 get() 方法或魔法获取器 value

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
var_dump($apc->get());
//or
var_dump($apc->value);

您还可以在字符串上下文中打印内容

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->value = 'foo';
echo $apc; // will print "foo"

如果值不是标量,则使用 print_r() 函数。

删除值

您可以使用 delete() 方法或魔法 unset() 强制从APC缓存中删除值。

use \Malenki\Apc;
$apc = new Apc('key_id_you_have_selected', 3600);
$apc->set('foo');
$apc->delete();
// or
unset($apc->value);

测试值

您可以在对其进行操作之前测试值是否存在。您必须调用 exists() 方法或魔法 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';
}

清理缓存

您可以清理所有或仅清理部分,如 useropcode

use \Malenki\Apc;
Apc::clean(); // clean all
// or
Apc::clean('user'); // clean user cache type
// or
Apc::clean('opcode'); // clean opcode cache type