We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Memcache Start vs. Save

I am having some trouble trying to make a cacheableModel, when ever I try to save to cache (memcache), I get an error: "The cache must be started first"? My Frontend is Data, and the backend is memcache. Is there something I am not getting?



2.9k
edited Aug '14

This is the code in the Model, where I extend override findFirts()

public static function findFirst($params = null)
{
        $key = self::createKey($params);
        $di = Phalcon\Di::getDefault()->get('modelsCache');
        $cacheResults = $di->get($key);
        if (isset($cacheResults)) {
            return $cacheResults;
        }
        $result = parent::findFirst($params);
        $di->save($key, $result);
        return $result;
}


2.9k
Accepted
answer

Okay, I found the issue,

Other controllers were also extending this model, and createKey() function didn't cater for their params.

edited Aug '14

For next It is not good practice to cache rows by your hands (your redeclare method)

Use something like this

$products = Products::find(array(
    "cache" => array("key" => "my-cache", "lifetime" => 300)
));


2.9k

Thanks, implemented that now.