Home php教程 php手册 PHP简单的缓存文件类详解

PHP简单的缓存文件类详解

Jun 13, 2016 am 10:40 AM
php develop Scalability powerful document of Simple kind cache Detailed explanation

        PHP由于它的强大和可伸缩性,近几年来得到长足的发展,PHP相比传统的ASP网站,在速度上有绝对的优势,想mssql转6万条数据PHP如需要40秒,ASP不下2分钟.但是,由于网站的数据越来越多,我们渴求能更快速的调用数据,不必要每次都从数据库掉,我们可以从其他的地方,比方一个文件,或者某个内存地址,这就是PHP的缓存技术,也就是Cache技术。

  分析深入
  一般来说,缓存的目的是把数据放在一个地方让访问的更快点,毫无疑问,内存是最快的,但是,几百M的数据能往内存放么?这不现实,当然,有的时候临时放如服务器缓存,如ob_start()这个缓存页面开启的话在发送文件头之前页面内容都被缓存在内存中,知道等页面输出自动清楚或者等待 ob_get_contents的返回,或者被ob_end_clean显示的清除,这在静态页面的生成中能很好的利用,在模板中能得到很好的体现。

  另外,在ASP中有一对象application,可以保存公用的参数,这也算点缓存,但在PHP,我至今没看到开发者产出这种对象,的确,没必要.ASP.NET的页面缓存技术就用的是viewstate,而cache就是文件关联,(不一定准确),文件被修改,更新缓存,文件没被修改而且不超时(注释1),就读取缓存,返回结果,就是这个思路,看看这个源码:
class cache{    
        private $cache_dir;    
        private $expireTime=180;//缓存的时间是 60 秒    
        function __construct($cache_dirname){    
                if(!@is_dir($cache_dirname)){    
                        if(!@mkdir($cache_dirname,0777)){    
                                $this->warn(缓存文件不存在而且不能创建,需要手动创建.);    
                                return false;    
                        }    
                }    
                $this->cache_dir = $cache_dirname;    
        }   
        
        function __destruct(){    
                echo Cache class bye.;    
        }    
        
        function get_url() {    
                if (!isset($_SERVER[REQUEST_URI])) {    
                        $url = $_SERVER[REQUEST_URI];    
                }else{    
                        $url = $_SERVER[SCRIPT_NAME];    
                        $url .= (!emptyempty($_SERVER[QUERY_STRING])) ? ? . $_SERVER[QUERY_STRING] : ;    
                }    
                return $url;    
        }
        
        function warn($errorstring){  
                echo "发生错误:

".$errorstring."
Copy after login
";  
        }  
        
        function cache_page($pageurl,$pagedata){  
                if(!$fso=fopen($pageurl,w)){  
                        $this->warns(无法打开缓存文件.);//trigger_error  
                        return false;  
                }  
                if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定  
                        $this->warns(无法锁定缓存文件.);//trigger_error  
                        return false;  
                }  
                if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式  
                        $this->warns(无法写入缓存文件.);//trigger_error  
                        return false;  
                }  
                flock($fso,LOCK_UN);//释放锁定  
                fclose($fso);  
                return true;  
        }  
        
        function display_cache($cacheFile){  
                if(!file_exists($cacheFile)){  
                        $this->warn(无法读取缓存文件.);//trigger_error  
                        return false;  
                }  
                echo 读取缓存文件:.$cacheFile;  
                //return unserialize(file_get_contents($cacheFile));  
                $fso = fopen($cacheFile, r);  
                $data = fread($fso, filesize($cacheFile));  
                fclose($fso);  
                return $data;  
        }  
        
      

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles