缓存技术
1.静态缓存
2.Memcache redis缓存
静态缓存
保存在磁盘上的静态文件,用PHP生成数据放入静态文件中
PHP操作缓存
1.生成缓存
2.获取缓存
3.删除缓存
静态缓存案例:
- <?php
- class cacheFile{
- private $_dir;
-
- const EXT = '.txt';
-
- public function __construct(){
- $this->_dir = dirname(__FILE__).'/files/';
- }
-
- public function cacheData($key,$value='',$path=''){
- $filepath = $this->_dir.$path.$key.self::EXT;
-
- if($value!==''){
- if(is_null($value)){
- return unlink($filepath);
- }
- $dir = dirname($filepath);
- if(!is_dir($dir)){
- mkdir($dir,0777);
- }
- return file_put_contents($filepath,json_encode($value));
- }
-
- if(!is_file($filepath)){
- return FALSE;
- }else{
- return json_decode(file_get_contents($filepath),true);
- }
- }
- }
- <?php
- require_once('./cacheFile.class.php');
- $data = array(
- 'id' => 1,
- 'name' => 'zys',
- 'test' => array(1,2,3,4 => array(123,'sdfdf'))
- );
-
- $cacheFile = new cacheFile();
- if($a = $cacheFile->cacheData('my_cache_txt',null)){
- print_r($a);
- echo 'success';
- }else{
- echo 'false';
- }