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 I will use memcache , memcached and redis in my app?

Hello all, I have some misgiving, in my app I was done in view , models and controllers, but I dont konw more about memcache , memcached and redis ? some will help me ?



85.5k
Accepted
answer

Hi, i think redis is far the best

exmaple service:


use \Phalcon\Cache\Backend\Redis;

$this->di->set('modelsCache', function(){

            $frontCache = new FrontendData(
                array(
                    "lifetime" => 186400
                )
            );

            $cache = new Redis($frontCache, array(
                'host' => '127.0.0.1',
                'port' => 6379,
                "statsKey" => '_PHCM'
            ));

            if (APPLICATION_ENV === 'development'){
                $cache->flush();
            }

            return $cache;
        }, true);

example model find cache:


$products = Products::find(
    array(
        "cache" => array(
            "key"      => "my-cache", //each duifferent query has to have itts own key
            "lifetime" => 300
        )
    )
);

part of a view


$frontCache = new \Phalcon\Cache\Frontend\Output(array(
  "lifetime" => 172800 //2 days
));

$cache = new \Phalcon\Cache\Backend\Redis($frontCache, array(
  'host' => '127.0.0.1',
  'port' => 6379,
  "statsKey" => '_PHCM'
));

$content = $cache->start('main_rotation');

if ($content === null) {
    //ECHO BLA BLA BLA
} else {
    echo $content;
}

there is a whole page cahckign with key in phalcon/docs repo you can check it out.

All things that I posted exists in the docs aswell.

Have fun