在CLI模式下使用PHP APC缓存使用dumpfiles [英] Using PHP APC cache in CLI mode using dumpfiles

查看:289
本文介绍了在CLI模式下使用PHP APC缓存使用dumpfiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在我们的服务器上使用APC缓存。我们产品的一个最重要的部分是CLI(Cron /计划)进程,其性能至关重要。通常batchjob包括并行运行一些16-32进程大约一个小时(他们每隔几分钟重新启动)。



默认情况下,在CLI中使用APC缓存是由于在各个调用之间不保留操作码高速缓存而浪费时间。但APC还包含 apc_bin_dumpfile() apc_load_dumpfile()函数。


$ b b

我认为这两个函数可能用于使CLI在CLI模式下有效,因为它在batchjob之外编译,存储在一个单独的dumpfile中,并且各个进程加载转储文件。



有没有人有这样的情况下的任何经验,或者你能给出好的理由,为什么它会或不会工作?如果可以合理地获得任何显着的收益,在内存使用或性能?

在CLI中,它是真棒,它可以同样令人沮丧。使用健康的耐心,彻底,远离问题,如果你旋转,记住你正在使用缓存,这就是为什么它似乎它什么都不做,它实际上什么也不做。删除转储文件,只从基本开始,如果这不工作,忘记尝试一个新的机器,新的操作系统,如果它是工作制作一个副本,一块一块扩展功能 - 有一些东西不工作,如果它是工作提交或复制,添加另一块并再次测试,为了sanity检查重新检查以前工作的副本,cliches或不是;如果最初你不成功,请再试一次,你不能继续做同样的事情,期待新的结果。



准备好了吗?这是您一直在等待的:



为cli启用apc



apc.enable-cli = 1




在每个CLI请求中创建,填充和销毁APC缓存都不太理想

   - 以前的回答由未知的海报自从删除。 


你绝对是对的, ?



如果您尝试在CLI下使用APC,并且未启用APC,您会收到警告。



例如:

  PHP警告:apc_bin_loadfile():APC未启用,
apc_bin_loadfile不可用。
PHP警告:apc_bin_dumpfile():APC未启用,
apc_bin_dumpfile不可用。

警告:我建议您不要在php.ini中启用cli ,这不值得的沮丧,你会忘记你做了它,并有许多其他头痛与其他脚本,相信我它的不值得,使用启动脚本。 (见下文)



clc中的apc_loadfile和apc_dumpfile



根据 mightye php ,我们需要禁用apc.stat ,否则您会收到警告



  PHP警告:apc_bin_dumpfile():从apc_bin_dump [file]中排除一些文件。 
必须使用apc.stat = 0的完整路径包含高速缓存文件。



启动程序脚本 - php-apc.sh



我们将使用此脚本启动我们启用apc的脚本(例如 ./ php-apc.sh apc-cli.php ),而不是更改 php.ini

 #/ bin / sh 
php -d apc.enable_cli = 1 -d apc.stat = 0 $ 1

准备好基本功能了吗?当然你是=)



基本APC保留 - apc-cli.php



 <?php 
/ **检查转储文件是否存在,你不想使用file_exists * /
if(false!== $ dump_file = stream_resolve_include_path('apc.dump' )
/ **所以我们在哪里让我们看看我们应该如何/ /
if(false!== apc_bin_loadfile($ dump_file))
/ ** fetch什么存储最后运行只是为了乐趣* /
if(false!== $ value = apc_fetch('my.awesome.apc.store'))
echo$ value from apc\\\
;

/ **存储获取的下一次运行只是为了乐趣* /
apc_store('my.awesome.apc.store','awesome in cli');
/ **什么shlep不允许这一切,我们* /
apc_bin_dumpfile(array(),null,'apc.dump');

注意:为什么不使用file_exists?因为 file_exists == stat 你看到,我们想收获的奖励是 apc.stat = 0 so;在包含路径内工作;使用绝对路径而不是相对路径 - 由 stream_resolve_include_path(); 返回 include_once require_once 使用非 * _一次在调用方法 url_stat; 时,在不使用APC (Muchos重要的senor)的帮助下检查您的统计信息使用情况<致命范围溢出错误!中止通知线程。请参阅 url_stat
消息:由于StreamWrapper导致的错误超出了本讨论范围



烟雾测试



使用启动器执行基本脚本

  ./ php-apc.sh apc-cli.php 


b $ b

一大堆什么都没有发生,这是我们想要的,为什么还要使用缓存?如果它输出了任何东西,那么它没有工作,对不起。



应该有一个转储文件apc.dump看看你能找到它吗?如果你找不到它,那么它没有工作,对不起。



好,我们有转储文件没有错误让我们再次运行。

  ./ php-apc.sh apc-cli.php 

您要查看的内容:

  cles中的awesome from apc 

成功! =)



PHP中有一些作为一个工作的APC实现满足。

nJoy!


I've recently started using APC cache on our servers. One of the most important parts of our product is a CLI (Cron/scheduled) process, whose performance is critical. Typically the batchjob consists of running some 16-32 processes in parallel for about an hour (they "restart" every few minutes).

By default, using APC cache in CLI is a waste of time due to the opcode cache not being retained between individual calls. But APC also contains apc_bin_dumpfile() and apc_load_dumpfile() functions.

I was thinking these two function might be used to make APC efficient in CLI mode by having it all compiled sometime outside the batchjob, stored in a single dumpfile and having the individual processes load the dumpfile.

Does anybody have any experience with such a scenario or can you give good reasons why it will or will not work? If any significant gains could reasonably be had, either in memory use or performance? What pitfalls are lurking in the shadows?

解决方案

Disclaimer: As awesome as APC is when it works in CLI, and it is awesome, it can equally be as frustrating. Use with a healthy load of patience, be thorough, step away from the problem if you're spinning, keep in mind you are working with cache that is why it seems like its doing nothing, it is actually doing nothing. Delete dump file, start with just the basics, if that doesn't work forget it try a new machine, new OS, if it is working make a copy, piece by piece expand functionality - there are loads of things that won't work, if it is working commit or make a copy, add another piece and test again, for sanity-check recheck the copies that were working before, cliches or not; if at first you don't succeed try try again, you can't keep doing the same thing expecting new results.

Ready? This is what you've been waiting for:

Enable apc for cli

apc.enable-cli=1

it is not ideal to create, populate and destroy the APC cache on every CLI request

   - previous answer by unknown poster since removed.

You're absolutely right that sucks, lets fix it shall we?

If you try and use APC under CLI and it is not enabled you will get warnings.

something like:

PHP Warning:  apc_bin_loadfile(): APC is not enabled, 
              apc_bin_loadfile not available.
PHP Warning:  apc_bin_dumpfile(): APC is not enabled, 
              apc_bin_dumpfile not available.

Warning: I suggest you don't enable cli in php.ini, it is not worth the frustration, you are going to forget you did it and have numerous other headaches with other scripts, trust me its not worth it, use a launcher script instead. (see below)

apc_loadfile and apc_dumpfile in cli

As per the comment by mightye php we need to disable apc.stat or you will get a warnings

something like:

PHP Warning:  apc_bin_dumpfile(): Excluding some files from apc_bin_dump[file].  
              Cached files must be included using full path with apc.stat=0. 

launcher script - php-apc.sh

We will use this script to launch our apc enabled scripts (ex. ./php-apc.sh apc-cli.php) instead of changing the properties in php.ini directly.

#/bin/sh
php -d apc.enable_cli=1 -d apc.stat=0 $1

Ready for the basic functionality? Sure you are =)

basic APC persisted - apc-cli.php

<?php
/** check if dump file exists, you don't want to use file_exists */
if (false !== $dump_file = stream_resolve_include_path('apc.dump'))
    /** so where were we lets have a look see shall we */
    if (false !== apc_bin_loadfile($dump_file))
        /** fetch what was stored last run just for fun */
        if (false !== $value = apc_fetch('my.awesome.apc.store'))
            echo "$value from apc\n";

/** store what gets fetched the next run just for fun */
apc_store('my.awesome.apc.store', 'awesome in cli');
/** what a shlep lets not do that all over again shall we */
apc_bin_dumpfile(array(),null,'apc.dump');

Notice: Why not use file_exists? Because file_exists == stat you see and we want to reap the reward that is apc.stat=0 so; work within the include path; use absolute and not relative paths - as returned by stream_resolve_include_path(); avoid include_once, require_once use the non *_once counterparts; check your stat usage, when not using APC(Muchos important senor), with the help of a StreamWrapper echo for calls to method url_stat; Oops: Fatal scope over-run error! aborting notice thread. see url_stat message: Error caused by StreamWrapper outside the scope of this discussion.

The smoke test

Using the launcher execute the basic script

./php-apc.sh apc-cli.php

A whole bunch of nothing happened that's what we want right, why else do you want to use cache? If it did output anything then it didn't work, sorry.

There should be a dump file called apc.dump see if you can find it? If you can't find it then it didn't work, sorry.

Good we have the dump file there were no errors lets run it again.

./php-apc.sh apc-cli.php

What you want to see:

awesome in cli from apc

Success! =)

There are few in PHP as satisfying as a working APC implementation.
nJoy!

这篇关于在CLI模式下使用PHP APC缓存使用dumpfiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆