PHP Development for Android Server use create dynamic cache

Source: Internet
Author: User
Tags create directory delete cache

Why you should have a cache: reduce database server stress.
A. Static cache:
Static files saved on the server disk, with PHP generated data in a static file
PHP Operation Cache:
1. Generating the cache
2. Get the cache

3. Delete Cache


To generate a cache file:


file.php operation of statically cached classes

<?phpclass file {//operation static cached business private $_dir;const EXT = '. txt ';p ublic function __construct () {//Get the current directory of the files, Put the cache file in the current directory files under $this->_dir = DirName (__file__). '/files/';} Key is the cache file filename, value is the cached data public function CacheData ($key, $value = ', $path = ') {$filename = $this->_dir. $path. $key . Self::ext;if ($value!== ") {//Writes value value to cache  //Get directory, determines if directory does not exist, creates directory $dir = DirName ($filename); if (!is_dir ($dir)) { mkdir ($dir, 0777);//Generate a directory, give permission}//the first parameter filename, the second data (string), convert the array vlaue to a string return file_put_contents ($filename, Json_ Encode ($value));}}

testfile.php


<?phprequire_once ('./file.php '); $data =array (' id ' =>1, ' name ' = ' David ', ' type ' =>array (4,5,6)); $file = New File (), if ($file->cachedata (' Davidcache ', $data)) {echo "Success";} Else{echo "Error";}

If the build successfully displays success

This creates a cache file named Davidchche.txt in the directory of the current file.



Advanced version: Also implement cache read and delete


file.php

<?phpclass file {//operation static cached business private $_dir;const EXT = '. txt ';p ublic function __construct () {//Get the current directory of the files, Put the cache file in the current directory files under $this->_dir = DirName (__file__). '/files/';} Key is the cache file filename, value is the cached data public function CacheData ($key, $value = ', $path = ') {$filename = $this->_dir. $path. $key . Self::ext;if ($value!== ") {//Writes value value to cache              if (Is_null ($value)) {///if value value wears null then delete this cache file return @unlink ($ filename);} Get the directory, determine if directory does not exist, create directory $dir = dirname ($filename), if (!is_dir ($dir)) {mkdir ($dir, 0777);//Generate directory, give permission}//first parameter filename, The second data (string) converts the array Vlaue to a string return file_put_contents ($filename, Json_encode ($value));} if (!is_file ($filename))//Read cache file {return FALSE;} else{            return Json_decode (file_get_contents ($filename), True);}}}

Depending on the file class, if value is null = ' ', the cached data is read,
If value is null, the cached data is deleted,
If value is not empty and is not NULL, then the cache data is created

testfile.php


<?phprequire_once ('./file.php '); $data =array (' id ' =>1, ' name ' = ' David ', ' type ' =>array (4,5,6)); $file = New file ();//Delete cache file if ($file->cachedata (' Davidcache ', null)) {//if ($file->cachedata (' Davidcache ')) {//var_ Dump ($file->cachedata (' Davidcache '));//exit;echo "Success";} Else{echo "error";}    /* This is the IF ($file->cachedata (' Davidcache ')) {var_dump ($file->cachedata (' Davidcache ')) used when reading the cache, Exit;echo " Success ";} Else{echo "Error";} *//* Below is the IF ($file->cachedata (' Davidcache ', $data)) {echo "success") that created the cache call;} Else{echo "Error";} *///according to the file class, if value is null = ' ', read the cached data,//if value is NULL, delete the cached data,//if value is not empty, nor null, then create the cached data


PHP Development for Android Server use create dynamic cache

Related Article

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.