Home php教程 php手册 采用PEAR来缓冲PHP程序一

采用PEAR来缓冲PHP程序一

Jun 13, 2016 am 10:28 AM
pear php one world dynamic yes Popular of program buffer use page

PHP 世界中缓冲是一个热门的话题,因为 PHP 产生的动态页面,每次用户请求都需要重新计算,不论请求的结果是否一样,同时,PHP 每次都会编译一次脚本。这样的超负荷运转对一个流量很高的网站来说肯定难以忍受。幸运的是, Web 的结果可以缓冲,而不需要重新运行和编译脚本,商品化的产品像 ZendCache 或者开源的 Alternate PHP Cache都提供了把 PHP 脚本编译为字节代码并缓冲的办法。 PEAR 的缓冲包提供了缓冲动态内容,数据库查询和 PHP 函数调用的框架。 就像 Perl 有 CPAN, TeX 有 CTAN,PHP 也有自己的中心资源库,存放类,库和模块。这个库称为 PEAR(PHP Extension and Add-On Repository)。 本文假设你已经安装了 PEAR 环境,如果没有的话,可以去 PHP 网站下载。 PEAR 的缓冲包包含一个总体的缓冲类和几个特别的子类。缓冲类使用容器类来存贮和管理缓冲数据。 下面是 PEAR 缓冲当前所包含的容器,以及各自的参数: file -- file 容器在文件系统存储了缓冲的数据,是最快的容器。 cache_dir -- 这是容器存储文件的目录。 filename_prefix -- 缓冲文件的前缀,例如:"cache_"。 shm -- shm 容器把缓冲数据放入共享内存,基准测试显示,目前的实现下,这个容器的速度要比文件容器慢。 shm_key -- 共享内存使用的键值。 shm_perm -- 使用共享内存数据段的权限。 shm_size -- 分配共享内存的大小。 sem_key -- 信号灯的键值。 sem_perm -- 信号灯的权限。 db -- PEAR 的数据库抽象层。 dsn -- 数据库连接的 DSN 。可以参考 PEAR 的 DB 文档。 cache_table -- 表的名字。 phplib -- phplib 容器使用数据库抽象层存储缓冲。 db_class db_file db_path local_file local_path ext/dbx -- PHP 的数据库抽象层扩展,如果像把缓冲存入数据库,可以采用这个容器。 module host db username password cache_table persistent 使用 PEAR Cache 所得到的性能提升取决于你所选择的缓冲容器,例如,把数据库的结果再次存入数据库缓冲中就显得毫无意义。 PEAR Cache 的函数缓冲模块能把任何函数或者方法的结果缓冲,不论是 PHP 的内置函数还是用户自定义函数,他缺省采用文件容器,把缓冲数据放入到一个叫做 function_cache 的目录。 Cache_Function 类的构造器可以有三个可选的参数: $container :缓冲容器的名字。 $container_options :缓冲容器的数组参数。 $expires:缓冲对象过期的时间(秒数)。 普通的函数调用采用 Cache_Function 类的 call() 方法时,就能触发缓冲。调用 call() 很容易,的一个参数是函数的名字,然后是函数的参数,第二个参数是要调用函数中的第一个,依此类推,我们来看例子: 例 1: 缓冲函数和方法的调用 // 调用 PEAR Cache 的函数缓冲。


"; } } class bar { function foobar($object) { echo ’$’.$object.’->foobar(’.$object.’) ’; } } $bar = new bar; function foobar() { echo ’foobar()’; } // 取得 Cache_Function 对象 $cache = new Cache_Function(); // 对 foo 类的静态函数 bar() 作缓冲(foo::bar())。 $cache->call(’foo::bar’, ’test’); // $bar->foobar() $cache->call(’bar->foobar’, ’bar’); $cache->call(’foobar’); ?> 下面我们采用 Cache_Output 来把输出作缓冲: 例子 2: 缓冲脚本的输出 // 加载 PEAR Cache 的输出缓冲 ’.’) ); // 计算要缓冲页面的标记,我们假定页面的缓冲取决于 // URL, HTTP GET 和 POST 变量以及 cookies。 $cache_id = $cache->generateID(array(’url’ => $REQUEST_URI, ’post’ => $HTTP_POST_VARS, ’cookies’ => $HTTP_COOKIE_VARS) ); // 查询缓冲 if ($content = $cache->start($cache_id)) { // 缓冲命中 echo $content; die(); } // 缓冲丢失 // -- 在这里插入内容产生代码 -- // 把页面存入缓冲 echo $cache->end(); ?> 利用 Cache_Output 类,很容易把一个动态的数据库驱动的网站应用转化为静态,从而极大的提升站点的性能。 越来越多的站点在采用 GZIP 压缩 HTML 内容,这样减少了服务器的带宽消耗,对于使用 Modem 上网的用户来说也能受益不少。 Cache_OutputCompression 扩展了 Cache_Output 类的功能,他把 GZIP 压缩的 HTML 内容进行缓冲,从而节省了 CPU 压缩的时间。 待续...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles