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