Phalcon: Cache fragment, file cache, Memcache cache

Source: Internet
Author: User
Tags echo date

Several caches, need to use the front-end configuration, plus a backend instance with

Fragment cache:

Public Function indexaction ()    {        //render page        $this->view->settemplateafter (' common ');        Cache fragments
Front-end configuration $frontcache = new \phalcon\cache\frontend\output (Array ( "lifetime" = 86400 );
Back-end processing $cache = new \phalcon\cache\backend\file ($frontcache, Array ( "Cachedir" and "=". /app/cache/" )); Whether the query fragment exists $content = $cache->start ("Index.pthm"); if ($content = = null) { echo date ("R"); $this->view->pick ("Index/index"); Cache fragment $cache->save (); } else { echo $content; } }

  

File cache:

Public Function lastaction ()    {        ///Data cache        //front-end configuration        $frontcache = new \phalcon\cache\frontend\data (Array (            "Lifetime" =>86400        ));        Backend instance        $cache = new \phalcon\cache\backend\file ($frontcache, Array (            ' cachedir ' = ' = '). /app/cache/'        ));        $cacheKey = ' customer_10 ';        $customer = $cache->get ($cacheKey);        Read Cache       //$customer = $cache->get ($cacheKey, 3600) within 3600 validity period;        if ($customer = = null)        {            $customer = \syscustomer::find (Array (                "limit" =>10            ))->toarray ();            Save Cache            $cache->save ($cacheKey, $customer);            Set the cache's validity period individually            //$cache->save ($cacheKey, $customer, 3600);        }        Var_dump ($customer);    }

  

Memcache cache, this example is under Windows:

Public Function memcacheaction ()    {        //memcache cache        //front-end configuration validity period        $frontcache = new \phalcon\cache\frontend \data (Array (            "lifetime" =>86400        ));        Backend instantiation        $memcache =  new \phalcon\cache\backend\memcache ($frontcache, Array (            "servers" = = Array (                Array (                    "host" = "127.0.0.1",                    "port" = "11211",                    "Weight" and "1"        )))) ;       $mydata = $memcache->get (' My-data ');        if ($mydata = = null) {            $mydata = array (1,2,3,4,5);            Save Cache            $memcache->save (' My-data ', $mydata);            echo "--";        }        Var_dump ($mydata);    }

  

Phalcon: Cache fragment, file cache, Memcache cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.