PHP Cache Technology (reduces database server stress)

Source: Internet
Author: User
Tags create directory set set delete cache

    1. Static cache (static files stored on disk, with PHP generated data into static files)

A) PHP operation cache

I. Generating a cache

II. Get the cache

Iii. Deleting a cache

Determine if the directory exists: Is_dir ()

DirName (__file__): You can get the current directory of the file

mkdir (): Create directory

Unlink (): Delete file

<?PHP//Static Cachingclass File{ Constext= ". txt"; Private $_dir;  Public function__construct () {//Default Path       $this->_dir=dirname(__file__).‘ /files/'; }     /** * $key cache filename * $value Cache value * $path cache file Directory **/      Public functionCacheData ($key,$value= ",$path= ' '){        $filename=$this->_dir.$path.$key. Self::EXT; if($value!== ') {//write value values to cache                   if(Is_null($value)){//Delete a cache file              return@unlink($filename); }           $dir=dirname($filename); if(!Is_dir($dir)){             mkdir($dir, 0777);//Create a cache catalog file            }            return file_put_contents($filename, Json_encode ($value)); }            //Get Cache    if(!Is_file($filename)){      return false; }Else{     returnJson_encode (file_get_contents($filename),true); } }}$data=Array(' id ' =>1, ' name ' = ' ann ');//instantiating the file class    $file=New File();//Generate Cache   if($file->cachedata (' Index_cache ',$data))    {     Echo' Success '; }Else{      Echo' Error '; }    //Get Cache/*if ($file->cachedata ("Index_cache")) {Var_dump ($file->cachedata ("Index_cache"));      }else{echo ' ERROR '; } */       //Delete Cache /*if ($file->cachedata (' Index_cache ', null)) {echo ' success ';    } else{echo ' Errors '; }*/?>

2.Memcache Redis Cache

Setting up the cache

Failed to set cache after 15 seconds

To get the cached value:

PHP operation Memcache (see PHP Manual for more)

1. Installing the memcache Extension

2.L Connection Service-connect (' Memcache_host ', 11211);

3.set Set Cache

4. Get Cache

Timed tasks (Linux systems)

Timed Task commands

1. Scheduled Task Service provides crontab command to set the service

2.CRONTAB-E//Edit a user's cron service

3. Crontab-l//list details of a user cron service

4.crontab-r//Delete a user's cron service

PHP Cache Technology (reduces database server stress)

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.