Symfony2 self-bring file cache (cache) Feature usage

Source: Internet
Author: User
Tags apc

Preface:

If you use the SYMFONY2 framework to do projects, then you may need to cache some data, and when it comes to caching, there are many methods, from database to file to memory, and then to n caching techniques such as PHP's own file_put_contents (file cache), and the most common memcached, APC, Redis (memory cache), and so on.

which cache to choose:

It is true that any kind of caching technology can improve your program a lot of speed, but choose which one to cache according to the actual situation of your project. The recommended use of memcached, APC, Redis and other tools for large clicks is cached because they are structured, read faster, and when the content of another server is often taken from one server (such as reading the API data) for a small change. Choosing a simple file cache can also be compared to the speed of a tool.

Symfony2 File cache:

Without any configuration, simply refer to filesystem and IOException above the controller file you need to manipulate

Use Symfony\component\filesystem\filesystem; Use symfony\component\filesystem\exception\ioexception;


defultcontroll.php

  <?php  namespace ce\webbundle\controller;  use symfony\bundle\ frameworkbundle\controller\controller;  use sensio\bundle\frameworkextrabundle\configuration\ route;  use sensio\bundle\frameworkextrabundle\configuration\template;  use  symfony\component\filesystem\filesystem;  use symfony\component\filesystem\exception\ ioexception;    /**    * @ Interface Cache file method (in the actual project it can be written in model)      * @ $pertime  = 0  for unlimited life     */  private  Function cachefile ($filename,  $date,  $pertime  = 0 )   {           if (Empty ($filename)  | |  empty ($date)) {          return false;           }          //create your own files in the cache directory    $fs  = new filesystem ();           try {        $fs- >mkdir (DirName ($filename));  } catch  (ioexception  $e)  {        return false;  }  //Integrated Data    $time  = time ();    $cache  = array ();   $cache [' contents '] =  $date;   $cache [' Pertime ']   =  $pertime  === 0 ? 0 :  $time  + $ pertime;   $cache [' mtime ']    =  $time;   $res  = serialize ($cache)   //write down your documents    $result  = false;       $f  =  @fopen ($filename,  ' W ');          if  ( $f) &NBSP;{&NBSP;&NBSP;&NBSP;&Nbsp;           @flock ($f, &NBSP;LOCK_EX);               fseek ($f,  0);               ftruncate ($f,  0);                $tmp  =  @fwrite ($f,  $res);               if  (!) ( $tmp  === false))  {                     $result  = true;               }                @fclose ($f);          }        @chmod ($filename, 0777);       return  $result;   }      /**      * @ Clearing cache files      */    private  Function clearfile ($filename)     {    if (!file_exists ($filename)) {    return false;    }else{    //Delete a cache file     return unlink ($filename);    }    }       /**     * @ method of invoking caching      *   @Route ("/file")      *  @Template ()      */     public function fileaction ()     {             $name  =  ' Multidepot ';         //the data that needs to be cached is passed here      $date  = [' id ' =>1, ' name ' = ' small army '];    //set cache time $pertime  = 10; $newTime  = time ();//Set cache path      $filename  =  $this Container->getparameter (' Kernel.cache_dir ')  .  '. $name. TXT ';     //if there is a cache file, take the cache file contents     if (file_exists ($filename)) {     //read the contents of your file $res = unserialize (file_get_contents ($filename));//If the file has expired, re-cache the file if ($res [' Mtime '] -  $newTime  <  $pertime) {$this->clearfile ($filename); $this->cachefile ($ filename,  $date,  $pertime);} It is necessary to determine whether the current data is updated, the update is to fetch data from the interface, re-seed cache//if (true) {//clearfile ($filename);//$this->cachefile (  $filename,   $date,  $pertime  );//}    }else{    //does not exist cache file       $this->cachefile ($filename,  $date);     }

OK, very simple implementation of the Symfony2 file cache, cached files in the D:\wamp\www\abc\branches\dev\app\ Cache\dev folder.

It is important to note that the interfere folder needs to be created manually, and on the server, you need to set the permissions to be writable and readable.



This article is from the "Shows Technology" blog, be sure to keep this source http://wangzhijun.blog.51cto.com/9660708/1676663

Symfony2 self-bring file cache (cache) Feature usage

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.