我正在尝试使用APCu,我最近重新安装了一切,但似乎APCu无法正常工作。
我正在使用以下脚本来测试它是否可以写入缓存:
<?php
// Simple Person class
class Person {
private $name;
private $age;
public function setName($name) {
$this->name = $name;
}
public function setAge($age) {
$this->age = $age;
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
}
// Check if Person object found from cache
if ($obj = apc_fetch('person')) {
echo "Person data from cache: ", "<br />";
echo "Name: ", $obj->getName(), "<br />";
echo "Age: ", $obj->getAge(), "<br />";
}
else {
echo 'Person data not found from cache...saving', "<br />";
$obj = new Person;
$obj->setName('Test Person');
$obj->setAge(35);
apc_add('person', $obj, 3600);
}
?>
运行此功能并不会给我任何错误,但是alwasy说:&#34;未找到人员数据......&#34;这意味着缓存不能正常工作。
在我的php.ini中,我已将其配置为:
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525"
extension=apcu.so
[apcu]
apc.enabled = 1
apc.shm_size=1024M
apc.max_file_size=10M
apc.num_files_hint=20000
apc.user_entries_hint=20000
我已经重新编译了所有内容,但仍然没有运气。
我正在运行最新的cpanel版本。和phpinfo()也说安装了apcu,我在CentOS上运行。
答案 0 :(得分:2)
这项工作很适合我试试
<?php
// Simple Person class
class Person {
private $name;
private $age;
public function setName($name) {
$this->name = $name;
}
public function setAge($age) {
$this->age = $age;
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
}
// Check if Person object found from cache
$obj = new Person;
if (apc_fetch('person')) {
$obj = apc_fetch('person');
echo "Person data from cache: ", "<br />";
echo "Name: ", $obj->getName(), "<br />";
echo "Age: ", $obj->getAge(), "<br />";
}
else {
echo 'Person data not found from cache...saving', "<br />";
$obj->setName('Test Person');
$obj->setAge(35);
apc_add('person', $obj);
}
?>