Search code examples
phpcentoscpanelapc

APCu not working on webserver


I am trying to use APCu, i have re-installed everything lately but it seems as if APCu is not working.

I am using the following script to test if it can write the cache:

<?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);
    }
?>

Running this doesn't give me any errors but it alwasy says: "Person data not found..." which implicates that the cache is not working as it is supposed to.

In my php.ini i have configured it like this:

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

I have recompiled everything but still no luck.

I am running the latest cpanel version. and phpinfo() also says that apcu is installed, i am running on CentOS.


Solution

  • this work fine for me lets try

    <?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);
        }
    ?>