登录  /  注册
php缓存技术详细介绍及php缓存的实现代码
高洛峰
发布: 2016-11-29 10:18:47
原创
663人浏览过

有些信息比方经常不变的,但是还是能变的信息放在缓存中以加快显示速度,这是很有价值的,所谓的缓存,通俗的理解就是一些保存在服务器端的共用信息.它是于服务器同生死的,我们在保存缓存的时候可以指定下次更新的时间的判断,比方要在5分钟更新一次

数据缓存:这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。

用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。

举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。

页面缓存:

每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问的时候页面文件就发挥作用了。(模板引擎和网上常见的一些PHP缓存机制类通常有此功能)

时间触发缓存:

检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。

内容触发缓存:

当插入数据或更新数据时,强制更新PHP缓存机制。

静态缓存:

这里所说的静态缓存是指静态化,直接生成HTML或XML等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。

以上内容是代码级的解决方案,我直接CP别的框架,也懒得改,内容都差不多,很容易就做到,而且会几种方式一起用,但下面的内容是服务器端的缓存方案,非代码级的,要有多方的合作才能做到

内存缓存:

Memcached是高性能的,分布式的内存对象PHP缓存机制系统,用于在动态应用中减少数据库负载,提升访问速度。

php的缓冲器:

有eaccelerator, apc, phpa,xcache,这个这个就不说了吧,搜索一堆一堆的,自己看啦,知道有这玩意就OK

MYSQL缓存:

这也算非代码级的,经典的数据库就是用的这种方式,看下面的运行时间,0.09xxx之类的
我贴段根据蓝色那家伙修改后部分my.ini吧,2G的MYISAM表可以在0.05S左右,据说他前后改了有快一年

基于反向代理的Web缓存:

如Nginx,SQUID,mod_proxy(apache2以上又分为mod_proxy和mod_cache)
NGINX的例子

用google找到一些 php缓存技术方法

发个PHP缓存实现,实现了apc和文件缓存,继承Cache_Abstract即可实现调用第三方的缓存工具。

参考shindig的缓存类和apc。

<?php   
class CacheException extends Exception {}   
/**  
 * <a href="//m.sbmmt.com/category/79.html">缓存</a>抽象类  
 */  
abstract class Cache_Abstract {   
    /**  
     * 读<a href="http://www.php1.cn/category/79.html">缓存</a>变量  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return mixed  
     */  
    abstract public function fetch($key);   
        
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>变量下标  
     * @param string $value <a href="//m.sbmmt.com/category/79.html">缓存</a>变量的值  
     * @return bool  
     */  
    abstract public function store($key, $value);   
        
    /**  
     * 删除<a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return Cache_Abstract  
     */  
    abstract public function delete($key);   
        
    /**  
     * 清(删)除所有<a href="//m.sbmmt.com/category/79.html">缓存</a>  
     *  
     * @return Cache_Abstract  
     */  
    abstract public function clear();   
        
    /**  
     * 锁定<a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return Cache_Abstract  
     */  
    abstract public function lock($key);   
    
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>变量解锁  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return Cache_Abstract  
     */  
    abstract public function unlock($key);   
    
    /**  
     * 取得<a href="//m.sbmmt.com/category/79.html">缓存</a>变量是否被锁定  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return bool  
     */  
    abstract public function isLocked($key);   
    
    /**  
     * 确保不是锁定状态  
     * 最多做$tries次睡眠等待解锁,超时则跳过并解锁  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     */  
    public function checkLock($key) {   
        if (!$this->isLocked($key)) {   
            return $this;   
        }   
            
        $tries = 10;   
        $count = 0;   
        do {   
            usleep(200);   
            $count ++;   
        } while ($count <= $tries && $this->isLocked($key));  // 最多做十次睡眠等待解锁,超时则跳过并解锁   
    
        $this->isLocked($key) && $this->unlock($key);   
            
        return $this;   
    }   
}   
    
    
/**  
 * APC扩展<a href="//m.sbmmt.com/category/79.html">缓存</a>实现  
 *   
 *   
 * @category   Mjie  
 * @package    Cache  
 * @author     流水孟春  
 * @copyright  Copyright (c) 2008- <cmpan(at)qq.com>  
 * @license    New BSD License  
 * @version    $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $  
 */  
class Cache_Apc extends Cache_Abstract {   
        
    protected $_prefix = 'cache.mjie.net';   
        
    public function __construct() {   
        if (!function_exists('apc_cache_info')) {   
            throw new CacheException('apc extension didn't installed');   
        }   
    }   
        
    /**  
     * 保存<a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     *  
     * @param string $key  
     * @param mixed $value  
     * @return bool  
     */  
    public function store($key, $value) {   
        return apc_store($this->_storageKey($key), $value);   
    }   
        
    /**  
     * 读取<a href="//m.sbmmt.com/category/79.html">缓存</a>  
     *  
     * @param string $key  
     * @return mixed  
     */  
    public function fetch($key) {   
        return apc_fetch($this->_storageKey($key));   
    }   
        
    /**  
     * 清除<a href="//m.sbmmt.com/category/79.html">缓存</a>  
     *  
     * @return Cache_Apc  
     */  
    public function clear() {   
        apc_clear_cache();   
        return $this;   
    }   
        
    /**  
     * 删除<a href="//m.sbmmt.com/category/79.html">缓存</a>单元  
     *  
     * @return Cache_Apc  
     */  
    public function delete($key) {   
        apc_delete($this->_storageKey($key));   
        return $this;   
    }   
        
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>单元是否被锁定  
     *  
     * @param string $key  
     * @return bool  
     */  
    public function isLocked($key) {   
        if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {   
            return false;   
        }   
            
        return true;   
    }   
        
    /**  
     * 锁定<a href="//m.sbmmt.com/category/79.html">缓存</a>单元  
     *  
     * @param string $key  
     * @return Cache_Apc  
     */  
    public function lock($key) {   
        apc_store($this->_storageKey($key) . '.lock', '', 5);   
        return $this;   
    }   
        
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>单元解锁  
     *  
     * @param string $key  
     * @return Cache_Apc  
     */  
    public function unlock($key) {   
        apc_delete($this->_storageKey($key) . '.lock');   
        return $this;   
    }   
        
    /**  
     * 完整<a href="//m.sbmmt.com/category/79.html">缓存</a>名  
     *  
     * @param string $key  
     * @return string  
     */  
    private function _storageKey($key) {   
        return $this->_prefix . '_' . $key;   
    }   
}   
    
/**  
 * 文件<a href="//m.sbmmt.com/category/79.html">缓存</a>实现  
 *   
 *   
 * @category   Mjie  
 * @package    Cache  
 * @author     流水孟春  
 * @copyright  Copyright (c) 2008- <cmpan(at)qq.com>  
 * @license    New BSD License  
 * @version    $Id: Cache/File.php 版本号 2010-04-18 16:46 cmpan $  
 */  
class Cache_File extends Cache_Abstract {   
    public $useSubdir     = false;   
        
    protected $_cachesDir = 'cache';   
        
    public function __construct() {   
        if (defined('DATA_DIR')) {   
            $this->_setCacheDir(DATA_DIR . '/cache');   
        }   
    }   
        
    /**  
     * 获取<a href="//m.sbmmt.com/category/79.html">缓存</a>文件  
     *  
     * @param string $key  
     * @return string  
     */  
    protected function _getCacheFile($key) {   
        $subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';   
        return $this->_cachesDir . '/' . $subdir . $key . '.php';   
    }   
    
    /**  
     * 读取<a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     * 为防止信息泄露,<a href="//m.sbmmt.com/category/79.html">缓存</a>文件格式为php文件,并以"<?php exit;?>"开头  
     *   
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return mixed  
     */  
    public function fetch($key) {   
        $cacheFile = self::_getCacheFile($key);   
        if (file_exists($cacheFile) && is_readable($cacheFile)) {   
            // include 方式   
            //return include $cacheFile;   
            // 系列化方式   
    
            return unserialize(@file_get_contents($cacheFile, false, NULL, 13));   
        }   
    
        return false;   
    }   
    
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     * 为防止信息泄露,<a href="//m.sbmmt.com/category/79.html">缓存</a>文件格式为php文件,并以"<?php exit;?>"开头  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>变量下标  
     * @param string $value <a href="//m.sbmmt.com/category/79.html">缓存</a>变量的值  
     * @return bool  
     */  
    public function store($key, $value) {   
        $cacheFile = self::_getCacheFile($key);   
        $cacheDir  = dirname($cacheFile);   
    
        if(!is_dir($cacheDir)) {   
            if(!@mkdir($cacheDir, 0755, true)) {   
                throw new CacheException("Could not make cache directory");   
            }   
        }   
    // 用include方式   
        //return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');   
    
        return @file_put_contents($cacheFile, '<?php exit;?>' . serialize($value));   
    }   
    
    /**  
     * 删除<a href="//m.sbmmt.com/category/79.html">缓存</a>变量  
     *  
     * @param string $key <a href="//m.sbmmt.com/category/79.html">缓存</a>下标  
     * @return Cache_File  
     */  
    public function delete($key) {   
        if(emptyempty($key)) {   
            throw new CacheException("Missing argument 1 for Cache_File::delete()");   
        }   
            
        $cacheFile = self::_getCacheFile($key);   
        if(!@unlink($cacheFile)) {   
            throw new CacheException("Cache file could not be deleted");   
        }   
    
        return $this;   
    }   
    
    /**  
     * <a href="//m.sbmmt.com/category/79.html">缓存</a>单元是否已经锁定  
     *  
     * @param string $key  
     * @return bool  
     */  
    public function isLocked($key) {   
        $cacheFile = self::_getCacheFile($key);   
        clearstatcache();   
        return file_exists($cacheFile . '.lock');   
    }   
    
    /**  
     * 锁定  
     *  
     * @param string $key  
     * @return Cache_File  
     */  
    public function lock($key) {   
        $cacheFile = self::_getCacheFile($key);   
        $cacheDir  = dirname($cacheFile);   
        if(!is_dir($cacheDir)) {   
            if(!@mkdir($cacheDir, 0755, true)) {   
                if(!is_dir($cacheDir)) {   
                    throw new CacheException("Could not make cache directory");   
                }   
            }   
        }   
    
        // 设定<a href="//m.sbmmt.com/category/79.html">缓存</a>锁文件的访问和修改时间   
        @touch($cacheFile . '.lock');   
        return $this;   
    }   
      
    /**  
     * 解锁  
     *  
     * @param string $key  
     * @return Cache_File  
     */  
    public function unlock($key) {   
        $cacheFile = self::_getCacheFile($key);   
        @unlink($cacheFile . '.lock');   
        return
登录后复制


相关标签:
php
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学