PHP ob_start vs opcode APC, explain differences and real world usage?

半腔热情 提交于 2019-12-23 08:46:56

问题


Premise: I'm not trying to reinvent the wheel, I'm just trying to understand.

Output caching can be implemented easily:

//GetFromMyCache returns the page if it finds the file otherwise returns FALSE
if( ($page = GetFromMyCache($page_id)) !== FALSE )
{
   echo $page; //sending out page from cache
   exit();
}

//since we reach this point in code, it means page was not in cache
ob_start(); //let's start caching

//we process the page getting data from DB

//saving processed page in cache and flushing it out
echo CachePageAndFlush(ob_get_contents());

explained well in another article, and also in another answer.

But then comes APC (that will be included in PHP6 by default).

  1. Is APC a module that once installed on the server, existing PHP code will run faster without modification?

    Is APC automatic?

  2. Then, why are there functions like apc_add?

    How do we cache entire pages using APC?

  3. When APC is installed, do I still need to do any caching on my part?

  4. If APC is going to save hosting providers money, why do they not install it? (I mean they should be racing to install it, but I don't see that happening.)

    Does installing APC have disadvantages for these hosting providers?


回答1:


APC is an opcode cache:

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

This is not the same as a template cache (what you are demonstrating), and it has little impact on output buffering. It is not the same thing.

Opcode caching means cache the PHP code after it has been interpreted. This could be any code fragment (not necessarily something that outputs HTML). For example, you could stick classes and the template engine itself in an opcode cache. This would dramatically speed up your code, as the PHP interpreter doesn't need to "interpret" your code again, it can simply load the "interpreted" version from the cache.

Please do not confuse output buffering with a cache. There are many levels of caching, for example, two of the most common that you may be familiar with.

Caching the session

A very basic version of this is a cookie that stores some settings. You only execute the code that "calculates" the settings once (when a user logs in), and for the rest of the session, you use the "cached" settings from the cookie.

Caching the rendered template

This is done when a page that needs to be generated once, but doesn't change very often. For example a "daily specials" page, which is a template. You only generate this once, and then serve the "rendered" page from cache.

None of these use APC




回答2:


Is APC makes the PHP to run faster on its own?

Yes. In a way. The benefit hugely differs though.

When using APC do I still need to cache rendered HTML?

Bytecode is NOT like resulting HTML. It is the same program as a regular PHP script. Even with APC enabled, PHP have to process data and render HTML.

I hope you understand the difference now.

APC cache provides both byte-code cache and memory-based storage to store user data.
So, you can also use it to store some user-defined data.
And store whole rendered pages as well (I don't understand your confusion here - what is that 'page' data type you are talking about? Isn't the ob result being just a regular string?).
However, caching of the resulting HTML is not that easy as you imagine.

Premature optimization is the root of all evil.
Start optimizing your site only when you have a reason.

why are Web Hosters waiting to install APC?

There are several reasons. But one is enough - bytecode cache won't make any profit for the usual PHP-based ugly homepage ecommerce site.




回答3:


APC caches bytecodes. PHP turns source code you write into these when a file gets requested or included, and then gets rid of them. With APC the bytecode stays around.

ob_start turns on an output buffer. It can be used to cache one effect of the program code, which is the text it prints.

Use APC if you want your program to run faster and consume less CPU power. It has no effect on database throughput.

Cache ob_start output if you only want to run the program every now and then and just statically serve its last output. This saves database throughput, at the price of information freshness and personalization.

APC is good when each page request conveys new information, or information specific to the user.

Cache ob_start output if you are running some heavyweight calculations or data access and it's okay that everyone gets the same not-quite-fresh output.



来源:https://stackoverflow.com/questions/9977442/php-ob-start-vs-opcode-apc-explain-differences-and-real-world-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!