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

How to cache the Resultset

I am try to cache all sql queries, bud have some problems with Resultset, the code


 $countries= new Country();

return new Resultset(null, $countries, $countries->getReadConnection()->query($sql),$this->modelsCache);

doesn't save in cache



43.9k

Hi,

do you get any error message ?

Are you sure that $countries->getReadConnection()->query($sql) return a valid resultset ?

Please show us your frontend/backend cache settings.



17.5k
edited Feb '17

The cache correct work in find and findOne of model.

//service.php
$di->set(
    "modelsCache",
    function () use ($config){
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
            "lifetime" => $config->redis->lifetime
        ));

        $cache = new \Phalcon\Cache\Backend\Redis($frontCache, [
            'prefix'     =>$config->redis->prefix.'db_',
            'lifetime'   => $config->redis->lifetime,
            'host'       => $config->redis->host,
            'port'       => $config->redis->port,
            'persistent' => $config->redis->persistent,
        ]);

        return $cache;
    }
);
//model Country

  public static function getAll($lang='en',$cache)
    {
      $sql="select c.*,COALESCE(transliterate((select a.alternate_name...";

        $countries= new Country();

        return new Resultset(null, $countries, $countries->getReadConnection()->query($sql),$cache);
    }

Call model from controller

$time_start = microtime(true);

Country::getAll($this->lang,$this->modelsCache);

$time_end = microtime(true);
 $time = $time_end - $time_start;
 echo $time;

The result 1.8994641304016 to 2.0657551288605 seconds. The result corect return list of countries.

Connection like

 var_dump($countries->getReadConnection()->query($sql));

Return

 object(Phalcon\Db\Result\Pdo)#256 (8) { ["_connection":protected]=> object(Phalcon\Db\Adapter\Pdo\Postgresql)#253 (13) { ["_eventsManager":protected]=>

Phalcon 3.0.3 Ubuntu php7.0



17.5k
edited Feb '17

No this cache only find and findOne or DQL, not help with raw SQL on Resultset.

maybe this can help: https://docs.phalcon.io/en/latest/reference/models-cache.html#forcing-cache