Home  >  Article  >  Backend Development  >  cache_lite trial_PHP tutorial

cache_lite trial_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:46770browse

Let’s study pear’s cache_lite today.

Downloaded the latest cache_lite from http://pear.php.net. cache_lite is a lightweight cache library class in the pear library class. It is indeed lightweight, with a total of 4 files of code. lite.php cache/File.php cache/unction.php cache/output.php. And the scalability is very good. Add it to your own library class and mainly modify the raiseError function of lite.php.

After adding your own library class, start testing. First basic caching:

<span style="COLOR: #000000"><code><span style="COLOR: #000000"><br><span style="COLOR: #0000bb"><? <BR></SPAN><SPAN style="COLOR: #007700">require_once(</SPAN><SPAN style="COLOR: #dd0000">'../libs/cache/Lite.php'</SPAN><SPAN style="COLOR: #007700">); <br><br></SPAN><SPAN style="COLOR: #0000bb">$id</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #dd0000">'1'</SPAN><SPAN style="COLOR: #007700">; <BR></SPAN><SPAN style="COLOR: #0000bb">$options </SPAN><SPAN style="COLOR: #007700">= array( <BR> </SPAN><SPAN style="COLOR: #dd0000">'cacheDir' </SPAN><SPAN style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">'../cache/test/'</span><span style="COLOR: #007700">, <br>    </span><span style="COLOR: #dd0000">'lifeTime' </span><span style="COLOR: #007700">=> </span><span style="COLOR: #0000bb">60 <br></span><span style="COLOR: #007700">); <br></span><span style="COLOR: #0000bb">$cache</span><span style="COLOR: #007700">=new </span><span style="COLOR: #0000bb">Cache_Lite</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$options</span><span style="COLOR: #007700">); <br>if(</span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb">$cache</span><span style="COLOR: #007700">-></span><span style="COLOR: #0000bb">get</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$id</span><span style="COLOR: #007700">)){ <br>   echo </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">; <br>}else{ <br>   </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">=</span><span style="COLOR: #0000bb">time</span><span style="COLOR: #007700">(); <br>   </span><span style="COLOR: #0000bb">$cache</span><span style="COLOR: #007700">-></span><span style="COLOR: #0000bb">save</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">); <br>   echo </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">; <br>} <br></span><span style="COLOR: #0000bb">?> <br></span></span>require_once('../libs/cache/Lite.php');

$id='1';
$options = array(
'cacheDir' => 'cacheDir' SPAN>'../cache/test/',
'lifeTime' => 60 );

$cache=new Cache_Lite($options);
if($data=$cache->get($id)){ $data; }else{ $data= time(); $cache->save($data); echo $data; } ?> In order to make the test effect easier to detect, I set the cache time to 1 minute. After running, you find a file cache_c21f969b5f03d33d43e04f8f136e7682_c4ca4238a0b923820dcc509a6f75849b in the ../cache/test/ directory. This is the cache file generated by cache_lite. After opening it you will find that the data is encrypted. Of course, this is for safety reasons. After refreshing the page, you will find that the page has not changed. This is because the cache is working. It is very simple. ^_^. After 1 minute, refresh, haha, changed. This achieves the basic effect of caching.
cache_lite can also implement function caching and other functions. I will not introduce the details. You can visit http://www.phpobject.net/blog/books/cache_lite.html
http://www.bkjia.com/PHPjc/317940.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317940.htmlTechArticleStudy pear’s cache_lite today. Downloaded the latest cache_lite from http://pear.php.net. cache_lite is a lightweight cache library class in the pear library class. It is indeed lightweight, with a total of 4 files of code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn