Search code examples
phpapc

PHP APC problems when storing objects


I am trying to build an configuration parser for my application I installed APC today, but everytime I try to put an serialized object in the store, it does not get in there and does not. (I am checking with apc.php for my version[3.1.8-dev] on PHP 5.3.16 [My Dev Environment], so I am sure that the data is not in the cache). this is how I pass the data to the cacher:

// The data before the caching
array (
'key' => md5($this->filename),
'value' => serialize($this->cfg)
);

// The caching interface
function($argc){
$key = $argc['key'];
Cache\APC::getInstance()->set($key,$argc['value']);
}

// The caching method described above
public function set($key, $val) {
    if (apc_exists($key)) {
        apc_delete ($key);
        return apc_store($key, $val);
    }
    else 
        return false;
}


// the constructor of the configuration class. 
// It 1st looks for the configuration in
// the cache if it is not present performs the reading from the file.
public function __construct($filename = '/application/config/application.ini', 
                              $type = self::CONFIG_INI)
{
    if (defined('SYSTEM_CACHE') && SYSTEM_CACHE === 'APC'){
        $key = md5($filename);
        $cfg = APC::getInstance()->get($key);

        if (!empty($cfg)) {

            print "From Cache";

            $this->cfg = unserialize($cfg);
            return;
        } else {
            print "From File";
        }

    }
 }

I did a few tests and there is not a problem with the MD5() key (which I thought while writing this question) nor with APC itself. I am really stuck on this one, nothing odd in the logs, so if anyone can give me at least some directions will be very appreciated.

Thanks in advance!


Solution

  • The problem is was in my code:\

    public function set($key, $val) {
        /*
         *
         * If the key exists in the cache delete it and store it again,
         * but how could it exist when the else clause is like that...
         */
        if (apc_exists($key)) {
            apc_delete ($key);
            return apc_store($key, $val);
        }
        // This is very wrong in the current case
        // cuz the function will never store and the if will
        // never match..
        else 
            return false;
    }
    

    NOTE: Always think and keep your eyes open, if you still can't find anything get off the PC and give yourself a rest. Get back after 10-15 minutes and pown the code. It helps! :D