PHP Development for Android Server use create dynamic 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
_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
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
_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
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