首页 > 编程技术 > php

PHP Opcode缓存加速组件:APC详解介绍

发布时间:2016-11-25 15:30

Opcode缓存可以给php加速了,我们这里来看一篇关于PHP Opcode缓存加速组件:APC详解介绍吧,希望文章能够给各位带来帮助.

PHP在性能上相对于其他编译型语言来说算不上突出,但是使用了Opcode缓存后性能提升还是很明显的。常见的缓存加速组件主要有 eAccelerator,XCache,APC本文主要介绍APC的安装使用。

APC,全称是Alternative PHP Cache,官方翻译叫"可选PHP缓存"。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。

系统缓存

它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存3600s(一小时)。但是这样仍会浪费大量CPU时间。因此可以在php.ini中设置system缓存为永不过期(apc.ttl=0)。不过如果这样设置,改运php代码后需要重启WEB服务器。目前使用较多的是指此类缓存。

用户数据缓存

缓存由用户在编写PHP代码时用apc_store和apc_fetch函数操作读取、写入的。如果数据量不大的话,可以一试。如果数据量大,使用类似memcache此类的更加专著的内存缓存方案会更好。

APC模块的安装

最简单的方法是直接使用pecl,在命令行下输入:/usr/local/php/bin/pecl install apc
然后按照提示一步步完成即可,示例如下:

[root@iZ23bm1tc0pZ ~]# /usr/local/php/bin/pecl install apc
downloading APC-3.1.13.tgz ...
Starting to download APC-3.1.13.tgz (171,591 bytes)
.....................................done: 171,591 bytes
55 source files, building
running: phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
Enable internal debugging in APC [no] : no
Enable per request file info about files used from the APC cache [no] : no
Enable spin locks (EXPERIMENTAL) [no] : no
Enable memory protection (EXPERIMENTAL) [no] : no
Enable pthread mutexes (default) [no] : no
Enable pthread read/write locks (EXPERIMENTAL) [yes] : yes

然后重启服务器即可:

lnmp nginx restart

先看一下没有使用apc情况下的压测结果:

[root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   15.517 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    64.45 [#/sec] (mean)
Time per request:       1551.671 [ms] (mean)
Time per request:       15.517 [ms] (mean, across all concurrent requests)
Transfer rate:          915.34 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   4.8      0      17
Processing:    46 1481 277.0   1560    1638
Waiting:       42 1481 277.1   1560    1638
Total:         58 1482 272.8   1560    1638
Percentage of the requests served within a certain time (ms)
  50%   1560
  66%   1576
  75%   1582
  80%   1587
  90%   1602
  95%   1612
  98%   1622
  99%   1629
 100%   1638 (longest request)
可见最大吞吐率只有64.45reqs/s

然后我们开启apc,测试结果如下:

[root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   7.122 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    140.41 [#/sec] (mean)
Time per request:       712.189 [ms] (mean)
Time per request:       7.122 [ms] (mean, across all concurrent requests)
Transfer rate:          1994.29 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.4      0      10
Processing:    23  677 125.3    705     775
Waiting:       22  677 125.4    705     775
Total:         30  678 123.1    705     775
Percentage of the requests served within a certain time (ms)
  50%    705
  66%    719
  75%    726
  80%    730
  90%    742
  95%    750
  98%    760
  99%    765
 100%    775 (longest request)

 可见吞吐率提高了一倍多,达到140.41reqs/s。
 然后,我们在开启动态内容缓存(楼主的博客用的是Smarty缓存),测试结果如下:

 [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   2.263 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    441.98 [#/sec] (mean)
Time per request:       226.255 [ms] (mean)
Time per request:       2.263 [ms] (mean, across all concurrent requests)
Transfer rate:          6277.49 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.1      0      12
Processing:    18  215  38.1    222     255
Waiting:       18  215  38.3    222     255
Total:         26  216  35.6    223     255
Percentage of the requests served within a certain time (ms)
  50%    223
  66%    230
  75%    232
  80%    234
  90%    237
  95%    239
  98%    240
  99%    243
 100%    255 (longest request)

这一次吞吐率居然达到441.98reqs/s,提高了三倍多,相比最初的64.45reqs/s提高了近7倍,可见使用apc的opcode缓存配合Smarty缓存,对网站性能的优化效果还是相当明显的。

ThinkPHP跨数据配置如何配置参数呢,我们这里来为各位介绍一下关于ThinkPHP跨数据操作配置吧,希望文章能够对各位带来帮助.

在程序的开发中,难免会遇到跨数据库操作(包括同种和不同种类的数据库)的情况,使用Thinkphp跨数据库操作会容易的多

以下是简单的配置.

1 onfig.php文件

'LOAD_EXT_CONFIG' =>'db,db_config',
//加载扩展配置文件,多个文件用逗号分隔
//其中db.php中放置的是mysql的配置
//db_config放置的是另一个mysql数据库的配置

2 其中db.php内容如下

return array(
  'DB_TYPE'=>'mysql',
  'DB_HOST'=>'localhost',
  'DB_NAME'=>'your_mysql_dbname',
  'DB_USER'=>'your_mysql_user_name',
  'DB_PWD'=>'123456',
  'DB_PORT'=>'3306',
  'DB_PREFIX'=>'',
);
db_config.php内容如下
return array(
  'DB_CONFIG'=>array(
    'DB_TYPE' => 'mysql',
    'DB_HOST' => '127.0.0.1',
    'DB_PORT' => '3306',
    'DB_NAME' => 'oa',
    'DB_USER' => 'root',
    'DB_PWD'  => '',
    'DB_PREFIX' => 'tp_'
  )
);

3 在Lib/Model下新建一个专门的模型

class UserModel extends Model {
  protected $connection = 'DB_CONFIG';
}

4 可以在Action中调各个模型了

$user = D('User');
//继续进行其他操作...
$user->select(); //查找所有的用户

今天我们来看一篇关于Windows下命令行下执行Php程序的例子,希望这篇文章能够给各位带来帮助,具体的细节步骤如下文介绍.

在windows的命令行下执行程序

打开 命令提示符 cmd.exe

 

 

切换到PHP安装目录,如图:E:

进入相应的目录,如图:cd \np\php

执行PHP文件:php.exe E:\www\test.php

Hook在php中叫做钩子了,今天我们就一起来看看关于Php中钩子(Hook)的应用例子,希望这篇文章能够给各位带来帮助.

我们先来回顾下原本的开发流程;
产品汪搞出了一堆需求;
当用户注册成功后需要发送短信、发送邮件等等;
然后聪明机智勇敢的程序猿们就一扑而上;
把这些需求转换成代码扔在 用户注册成功 和 跳转到首页 之间;

没有什么能够阻挡;充满创造力的猿们;

<?php
 class Test{
 public function index(){
 // 用户注册成功
 /*
此处是一堆发送短信的代码
*/
 /*
此处是一堆发送邮件的代码
*/
 /*
此处是一堆其他功能的代码
*/
 // 前往网站首页
 }
}
$test=new Test();
$test->index();
如果每个功能都由不同的猿完成的话;
首先面临的就是代码会很杂乱;配合起来会比较麻烦;
那封装成函数吧;一方面会规范整洁写;另外方便重复调用;

没有什么能够阻挡;充满创造力的猿们;

<?php
class Test{
 public function index(){
 // 用户注册成功
 // 发送短信
sendSms($phone);
 // 发送邮件
sendSms($email);
 // 其他操作...
 // 前往网站首页
 }
}
/**
* 发送短信通知
* @param integer $phone 手机号
*/
function sendSMS($phone){
 // 此处是发送短信的代码
}
/**
* 发送邮件通知
* @param string $email 邮箱地址
*/
function sendEmail($email){
 // 此处是发送邮件的代码
}
这时候运营喵表示;
如果能在后台点点按钮就能设置是发邮件还是发短信;那想必是极好的;

没有什么能够阻挡;充满创造力的猿们;

<?php
class Test{
 public function index(){
 // 用户注册成功
 if ('如果设置了发送短信') {
 // 发送短信
sendSms($phone);
 }
 if ('如果设置了发送邮件') {
 // 发送邮件
sendSms($email);
 }
 // 其他操作...
 // 前往网站首页
 }
}
/**
* 发送短信通知
* @param integer $phone 手机号
*/
function sendSMS($phone){
 // 此处是发送短信的代码
}
/**
* 发送邮件通知
* @param string $email 邮箱地址
*/
function sendEmail($email){
 // 此处是发送邮件的代码
}

在一个封闭企业环境下这样搞是没有问题的;
然鹅;我们还有一位开放无私的猿领导要把程序开源出去造福其他猿类;
希望有更多的猿类来参与这个项目;共同开发功能;
如果大家都去改动这套程序;把自己的代码扔在 用户注册成功 和 跳转到首页 之间;
这显然是不靠谱的;想想都混乱的一塌糊涂;

那可不可以大家把自己写的代码放到某个目录下;
然后系统自动的根据配置项把这些代码加载到 用户注册成功 和 跳转到首页 之间呢?
好先定义如下目录

├─plugin // 插件目录
│ ├─plugin1 // 插件1
│ │ ├─config.php // 插件1的配置项
│ │ ├─index.php // 插件1的程序处理内容
│ ├─plugin2
│ │ ├─config.php
│ │ ├─index.php
│ ├─plugin3
│ │ ├─config.php
│ │ ├─index.php
│ ├─...
├─index.php // 业务逻辑

业务逻辑的代码

<?php
class Test{
 public function index(){
 // 用户注册成功
 // 获取全部插件
$pluginList=scandir('./plugin/');
 // 循环插件 // 排除. ..
 foreach ($pluginList as $k => $v) {
 if ($v=='.' || $v=='..') {
unset($pluginList[$k]);
 }
 }
echo "简易后台管理<hr>";
 // 插件管理
 foreach ($pluginList as $k => $v) {
 // 获取配置项
$config=include './plugin/'.$v.'/config.php';
$word=$config['status']==1 ? '点击关闭' : '点击开启';
echo $config['title'].'<a href="./index.php?change='.$v.'">'.$word.'</a><br />';
 }
echo '<hr>';
 // 输出插件内容
 foreach ($pluginList as $k => $v) {
 // 获取配置项
$config=include './plugin/'.$v.'/config.php';
 if ($config['status']==1) {
include './plugin/'.$v.'/index.php';
 // 运行插件
 Hook::run($v);
 }
 }
 // 前往网站首页
 }
}
// 插件类
class Hook{
 // 注册添加插件
 public static function add($name,$func){
$GLOBALS['hookList'][$name][]=$func;
 }
 // 执行插件
 public static function run($name,$params=null){
 foreach ($GLOBALS['hookList'][$name] as $k => $v) {
call_user_func($v,$params);
 }
 }
}
// 更改插件状态
if (isset($_GET['change'])) {
 // 获取到配置项
$config=include './plugin/plugin'.substr($_GET['change'],-1).'/config.php';
 // 如果是开启 那就关闭 如果是关闭 则开启
$config['status']=$config['status']==1 ? 0: 1;
 // 将更改后的配置项写入到文件中
$str="<?php \r\n return ".var_export($config,true).';';
file_put_contents('./plugin/'.$_GET['change'].'/config.php', $str);
header('Location:./');
}
$test=new Test();
$test->index();

插件配置项代码:

<?php
 return array (
 'status' => 1, // 定义状态 1表示开启 0表示关闭
 'title' => '发送短信', // 插件的名称
);

插件的内容:

<?php
Hook::add('plugin1',function(){
echo '发送短信的内容<br />';
});

标签:[!--infotagslink--]

您可能感兴趣的文章: