服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

php实现文件数据缓存实现代码

时间:2016-05-08 10:37来源: 作者: 点击:
缓存技术是每次访问页面的时候,都会先检测相应的缓存是否存在,如果不存在,就连接数据库,得到数据,完成模板变量的赋值,显示页面,同时生成缓存文件,这样下次访问的时候缓存文件就

缓存技术是每次访问页面的时候,都会先检测相应的缓存是否存在,如果不存在,就连接数据库,得到数据,完成模板变量的赋值,显示页面,同时生成缓存文件,这样下次访问的时候缓存文件就发挥作用了.

而不会再执行if块的数据查询语句了,当然,在实际使用中会有很多东西要考虑,比如,有效期的设置,缓存组的设置等等.

php实现文件数据缓存实现代码如下:

  1. <?php   
  2. class cacheexception extends exception {}   
  3. /**  
  4.  * 缓存抽象类  
  5.  */   
  6. abstract class cache_abstract {   
  7.     /**  
  8.      * 读缓存变量  
  9.      *  
  10.      * @param string $key 缓存下标  
  11.      * @return mixed  
  12.      */   
  13.     abstract public function fetch($key);   
  14.        
  15.     /**  
  16.      * 缓存变量  
  17.      *  
  18.      * @param string $key 缓存变量下标  
  19.      * @param string $value 缓存变量的值  
  20.      * @return bool  
  21.      */   
  22.     abstract public function store($key$value);   
  23.        
  24.     /**  
  25.      * 删除缓存变量  
  26.      *  
  27.      * @param string $key 缓存下标  
  28.      * @return cache_abstract  
  29.      */   
  30.     abstract public function delete($key);   
  31.        
  32.     /**  
  33.      * 清(删)除所有缓存  
  34.      *  
  35.      * @return cache_abstract  
  36.      */   
  37.     abstract public function clear();   
  38.        
  39.     /**  
  40.      * 锁定缓存变量  
  41.      *  
  42.      * @param string $key 缓存下标  
  43.      * @return cache_abstract  
  44.      */   
  45.     abstract public function lock($key);   
  46.    
  47.     /**  
  48.      * 缓存变量解锁  
  49.      *  
  50.      * @param string $key 缓存下标  
  51.      * @return cache_abstract  
  52.      */   
  53.     abstract public function unlock($key);   
  54.    
  55.     /**  
  56.      * 取得缓存变量是否被锁定  
  57.      *  
  58.      * @param string $key 缓存下标  
  59.      * @return bool  
  60.      */   
  61.     abstract public function islocked($key);   
  62.    
  63.     /**  
  64.      * 确保不是锁定状态  
  65.      * 最多做$tries次睡眠等待解锁,超时则跳过并解锁  
  66.      *  
  67.      * @param string $key 缓存下标  
  68.      */   
  69.     public function checklock($key) {   
  70.         if (!$this->islocked($key)) {   
  71.             return $this;   
  72.         }   
  73.            
  74.         $tries = 10;   
  75.         $count = 0;   
  76.         do {   
  77.             usleep(200);   
  78.             $count ++;   
  79.         } while ($count <= $tries && $this->islocked($key));  // 最多做十次睡眠等待解锁,超时则跳过并解锁   
  80.    
  81.         $this->islocked($key) && $this->unlock($key);   
  82.            
  83.         return $this;   
  84.     }   
  85. }   
  86.    
  87.    
  88. /**  
  89.  * apc扩展缓存实现  
  90.  *   
  91.  *   
  92.  * @category   mjie  
  93.  * @package    cache  
  94.  * @author     流水孟春  
  95.  * @copyright  copyright (c) 2008- <cmpan(at)qq.com>  
  96.  * @license    new bsd license  
  97.  * @version    $id: cache/apc.php 版本号 2010-04-18 23:02 cmpan $  
  98.  */   
  99. class cache_apc extends cache_abstract {   
  100.        
  101.     protected $_prefix = 'cache.mjie.net';   
  102.        
  103.     public function __construct() {   
  104.         if (!function_exists('apc_cache_info')) {   
  105.             throw new cacheexception('apc extension didn't installed');   
  106.         }   
  107.     }   
  108.        
  109.     /**  
  110.      * 保存缓存变量  
  111.      *  
  112.      * @param string $key  
  113.      * @param mixed $value  
  114.      * @return bool  
  115.      */   
  116.     public function store($key$value) {   
  117.         return apc_store($this->_storagekey($key), $value);   
  118.     }   
  119.        
  120.     /**  
  121.      * 读取缓存  
  122.      *  
  123.      * @param string $key  
  124.      * @return mixed  
  125.      */   
  126.     public function fetch($key) {   
  127.         return apc_fetch($this->_storagekey($key));   
  128.     }   
  129.        
  130.     /**  
  131.      * 清除缓存  
  132.      *  
  133.      * @return cache_apc  
  134.      */   
  135.     public function clear() {   
  136.         apc_clear_cache();   
  137.         return $this;   
  138.     }   
  139.        
  140.     /**  
  141.      * 删除缓存单元  
  142.      *  
  143.      * @return cache_apc  
  144.      */   
  145.     public function delete($key) {   
  146.         apc_delete($this->_storagekey($key));   
  147.         return $this;   
  148.     }   
  149.        
  150.     /**  
  151.      * 缓存单元是否被锁定  
  152.      *  
  153.      * @param string $key  
  154.      * @return bool  
  155.      */   
  156.     public function islocked($key) {   
  157.         if ((apc_fetch($this->_storagekey($key) . '.lock')) === false) {   
  158.             return false;   
  159.         }   
  160.            
  161.         return true;   
  162.     }   
  163.        
  164.     /**  
  165.      * 锁定缓存单元  
  166.      *  
  167.      * @param string $key  
  168.      * @return cache_apc  
  169.      */   
  170.     public function lock($key) {   
  171.         apc_store($this->_storagekey($key) . '.lock''', 5);   
  172.         return $this;   
  173.     }   
  174.        
  175.     /**  
  176.      * 缓存单元解锁  
  177.      *  
  178.      * @param string $key  
  179.      * @return cache_apc  
  180.      */   
  181.     public function unlock($key) {   
  182.         apc_delete($this->_storagekey($key) . '.lock');   
  183.         return $this;   
  184.     }   
  185.        
  186.     /**  
  187.      * 完整缓存名  
  188.      *  
  189.      * @param string $key  
  190.      * @return string  
  191.      */   
  192.     private function _storagekey($key) {   
  193.         return $this->_prefix . '_' . $key;   
  194.     }   
  195. }   
  196.    
  197. /**  
  198.  * 文件缓存实现  
  199.  *   
  200.  *   
  201.  * @category   mjie  
  202.  * @package    cache  
  203.  * @author     流水孟春  
  204.  * @copyright  copyright (c) 2008- <cmpan(at)qq.com>  
  205.  * @license    new bsd license  
  206.  * @version    $id: cache/file.php 版本号 2010-04-18 16:46 cmpan $  
  207.  */   
  208. class cache_file extends cache_abstract {   
  209.     public $usesubdir     = false;   
  210.        
  211.     protected $_cachesdir = 'cache';   
  212.        
  213.     public function __construct() {   
  214.         if (defined('data_dir')) {   
  215.             $this->_setcachedir(data_dir . '/cache');   
  216.         }   
  217.     }   
  218.        
  219.     /**  
  220.      * 获取缓存文件  
  221.      *  
  222.      * @param string $key  
  223.      * @return string  
  224.      */   
  225.     protected function _getcachefile($key) {   
  226.         $subdir = $this->usesubdir ? substr($key, 0, 2) . '/' : '';   
  227.         return $this->_cachesdir . '/' . $subdir . $key . '.php';   
  228.     }   
  229.    
  230.     /**  
  231.      * 读取缓存变量  
  232.      * 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头  
  233.      *   
  234.      * @param string $key 缓存下标  
  235.      * @return mixed  
  236.      */   
  237.     public function fetch($key) {   
  238.         $cachefile = self::_getcachefile($key);   
  239.         if (file_exists($cachefile) && is_readable($cachefile)) {   
  240.             // include 方式   
  241.             //return include $cachefile;   
  242.             // 系列化方式   
  243.    
  244.             return unserialize(@file_get_contents($cachefile, false, null, 13));   
  245.         }   
  246.    
  247.         return false;   
  248.     }   
  249.    
  250.     /**  
  251.      * 缓存变量  
  252.      * 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头  
  253.      *  
  254.      * @param string $key 缓存变量下标  
  255.      * @param string $value 缓存变量的值  
  256.      * @return bool  
  257.      */   
  258.     public function store($key$value) {   
  259.         $cachefile = self::_getcachefile($key);   
  260.         $cachedir  = dirname($cachefile);   
  261.    
  262.         if(!is_dir($cachedir)) {   
  263.             if(!@mkdir($cachedir, 0755, true)) {   
  264.                 throw new cacheexception("could not make cache directory");  
  265.             }   
  266.         }   
  267.     // 用include方式   
  268.         //return @file_put_contents($cachefile, '<?php return ' . var_export($value, true). ';');   
  269.    
  270.         return @file_put_contents($cachefile'<?php exit;?>' . serialize($value));   
  271.     }   
  272.    
  273.     /**  
  274.      * 删除缓存变量  
  275.      *  
  276.      * @param string $key 缓存下标  
  277.      * @return cache_file  
  278.      */   
  279.     public function delete($key) {   
  280.         if(emptyempty($key)) {   
  281.             throw new cacheexception("missing argument 1 for cache_file::delete()");   
  282.         }   
  283.            
  284.         $cachefile = self::_getcachefile($key);   
  285.         if(!@unlink($cachefile)) {   
  286.             throw new cacheexception("cache file could not be deleted");   
  287.         }   
  288.    
  289.         return $this;   
  290.     }   
  291.    
  292.     /**  
  293.      * 缓存单元是否已经锁定  
  294.      *  
  295.      * @param string $key  
  296.      * @return bool  
  297.      */   
  298.     public function islocked($key) {   
  299.         $cachefile = self::_getcachefile($key);   
  300.         clearstatcache();   
  301.         return file_exists($cachefile . '.lock');   
  302.     }   
  303.    
  304.     /**  
  305.      * 锁定  
  306.      *  
  307.      * @param string $key  
  308.      * @return cache_file  
  309.      */   
  310.     public function lock($key) {   
  311.         $cachefile = self::_getcachefile($key);   
  312.         $cachedir  = dirname($cachefile);   
  313.         if(!is_dir($cachedir)) {   
  314.             if(!@mkdir($cachedir, 0755, true)) {   
  315.                 if(!is_dir($cachedir)) {   
  316.                     throw new cacheexception("could not make cache directory");   
  317.                 }   
  318.             }   
  319.         }   
  320.    
  321.         // 设定缓存锁文件的访问和修改时间   
  322.         @touch($cachefile . '.lock');   
  323.         return $this;   
  324.     }   
  325.      
  326.     /**  
  327.      * 解锁  
  328.      *  
  329.      * @param string $key  
  330.      * @return cache_file  
  331.      */   
  332.     public function unlock($key) {   
  333.     //开源代码最模板zuimoban.com 
  334.         $cachefile = self::_getcachefile($key);   
  335.         @unlink($cachefile . '.lock');   
  336.         return 
  337. ?> 

下面来看一款关于smarty缓存的文件实例代码,再来看看smarty提供的页面缓存功能:

  1. <?php 
  2.  
  3. require('smarty.class.php'); 
  4.  
  5. $smarty = new smarty; 
  6.  
  7. $smarty->caching = true; 
  8.  
  9. if(!$smarty->is_cached('index.tpl')) { 
  10.  
  11.  // no cache available, do variable assignments here. 
  12.  
  13.  $contents = get_database_contents(); 
  14.  
  15.  $smarty->assign($contents); 
  16.  
  17.  
  18. $smarty->display('index.tpl'); 
  19.  
  20. ?> 

php缓存技术工作时,当程序查询数据的时候,会把相应的结果序列化后保存到文件中,以后同样的查询语句就可以不用直接查询数据库,而是从缓存文件中获得,这一改进使得程序运行速度得以太幅度提升.

 
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------