1

有这堂课:

class Cacher
{
    private static $memcacheObj = NULL;

    public static function GetInstance()
    {
        if (self::$memcacheObj === NULL) 
        {
            self::$memcacheObj = new Memcache;
            self::$memcacheObj->addServer(Configs::memcache_addr);
        }
        return self::$memcacheObj;
    }

    public static function Set($key, $var, $flag = 0, $expire = 0)
    {
        self::GetInstance()->set($key, $var, 0, $expire);
    }

    public static function Get($key, $flag = 0)
    {
        self::GetInstance()->get($key);
    }
}

像这样使用,它不起作用,刷新我的页面时没有从 Cacher::Get 检索到:

$keyValue = Cacher::Get('key_name');
if(empty($keyValue))
{
    Cacher::Set('key_name', 'key_value', 0, 0);
}
else
{
    echo "Got value : $keyValue";
}

但是下面的代码将起作用:

$keyValue = Cacher::GetInstance()->get('key_name');
if(empty($keyValue))
{
    Cacher::GetInstance()->set('key_name', 'key_value', 0, 0);
}
else
{
    echo "Got value : $keyValue";
}

谁能帮我解决这个问题?非常感谢!

4

1 回答 1

2

您没有在 Get 中返回结果。应该:

public static function Get($key, $flag = 0)
{
    return self::GetInstance()->get($key);
}
于 2013-02-28T09:34:06.260 回答