存储对象时的PHP APC问题

时间:2013-07-15 17:13:07

标签: php apc

我正在尝试为我的应用程序构建一个配置解析器我今天安装了APC,但是每当我尝试将一个序列化对象放在商店中时,它就不会进入那里而不会进入。 (我在PHP 5.3.16 [My Dev Environment]上用apc.php检查我的版本[3.1.8-dev],所以我确信数据不在缓存中)。这就是我将数据传递给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";
        }

    }
 }

我做了一些测试,MD5()键(我在编写这个问题时想到的)和APC本身没有问题。我真的被困在这一个,在日志中没什么奇怪的,所以如果有人能给我至少一些方向将非常感激。

提前致谢!

1 个答案:

答案 0 :(得分:2)

问题出现在我的代码中:\

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

注意: 如果你仍然无法找到任何东西从PC上取下并给自己休息,那么一定要思考并睁大眼睛。 10-15分钟后回来并显示代码。它有助于! :d