Which one is faster php File based caching or Opcache - php

I just recently got an updated about Opcache in php and i am little familiar with file based caching in Codeigniter.
But i thought as of now File based caching is faster other caching techniques, since there won't be any database access and it directly connect to the generated html file to load. So it should be fast than other techniques.
So i have searched in Google and some websites compared the speed of caching by benchmarking it where they mentioned File caching is slow on retrieve when compared to other caching technique memcache and Opcache php and I am confused with the report.
I know every caching technique having their own pros and cons. Suggest me on the situation so my page won't be need of real time data and currently i am using file based caching. So Is it ok to go Opcache or Memache?

Opcache and Memcached store data in memory. In the vast majority of cases, retrieving data from memory is faster than retrieving data from the file system. The drawback? Running Memcached and using an opcache will obviously use up some of your server's memory.

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.
File based caching that you are talking about is about caching your variable to a file and get it on later. (often use when the time for getting data is very slow)
Therefore, you can still use file based caching to store your variable and use Opcache for caching your script. However, to cache your data to memory will be much more faster. In that case, try Memcached/Redis or anything you can find.

Related

Understanding OPCache behavior

Even tho the website is behind CloudFlare CDN, we decided to use OPCache to reduce the load on server as there are a maximum (peak) of ~400 active users per second (most of the time it's 50-100 u/s).
But most pages have some different data for each user, for example user's dashboard overview, most of the data is same but it has some different numbers for each user that needs to be up to date.
My Questions are:
Is it wise to use OPCache for such a website?
How will it handle pages with unique queries? will it take more RAM (caching multiple pages for each user) than running without OPCache?
Will it affect performance on pages such as Signup/Login etc?
I read that all PHP applications should use OPCache, is that correct?
P.S. The website is running on PHP 7.3.4
Yes it will gain performance improvement
If your RAM usage increased and you're concerned with it, you can fine tune memory consumption via opcache.memory_consumption
Affect meaning they will be faster
Yes, there really is no reason to not use OPCache other than edge cases (e.g., when your app's source code changes at a very fast rate or testing).
TLDR: Production code should always have OPCached enabled.
OPcache only caches the internal opcode representation of a PHP script, not its output. The queries performed or the content displayed by the page has no impact on how OPcache will behave.
This caching will improve the performance of all PHP web pages. As such, it should always be enabled on production sites.
Each PHP script is being compiled at runtime,
It takes time to transform the human readable code into a code that can be understood by the machine.
OpCache is a bytecode cache engine , it will compile the script to bytecode script only once - so you will save time - then the precompiled script is being stored in memory, which should lead to performance boosts in your PHP applications.
What i think you are missing is that the opcache DOES NOT cache the result of the script , but it is just compile the script .
Note this method is not good if the php script it self is changing for each user
or if it is loaded from -let's say for example- a database
As general view for this approach
Php caching whatever it was APC cache or opcache or else is amazing strategy this should increase php performance by 50% in general
As it act as follows
When php script excuted there's this involve three major steps
1-parse of the script
2-compile of the script
3-output
APC cache or else act as intermediary as it saves php script in the compiled form so php will start in the output stage directly and this will not affect queries but it will increase its speed as query for MySQL or else not just the statement of SELECT or whatever but it involves execution of extensions like PDO so it will become faster
You can classify extension as follows
APC or Xcache for php 5
In new php version use opcache
All of them have the same principle
Some developers like APC other like opcache
As example X-cart the popular shopping platform use xcache .

Is OPcache a plus if HTTP caching is enabled?

I have been reading about general concepts in PHP performance and what could be done to improve it. When developing websites and it's the time to go live I use HTTP caching and maybe a CDN (depends on traffic). Now that made me wonder if OPcache would be useful since I already use HTTP caching - page caching.
Definition of OPcache:
OPcache improves PHP performance by storing precompiled script
bytecode in shared memory, thereby removing the need for PHP to load
and parse scripts on each request.
Is OPcache a plus if a http caching is enabled? I think i miss the point somewhere.
HTTP caching is way different then PHP Opcache.
HTTP caching will cache static results on the client side. This could be for example images, scripts, and pages.
Opcaching will cache precompiled PHP scripts. This will make execution of scripts which are unable to be stored on the client. Think of a my account page for example.
So in short yes. Opcaching will make your PHP execution faster. Since it stores precompiled PHP files. HTTP caching will only help you with static results/pages.
Since you're working on speeding up your PHP performance it might also be good to take a look at http://php.net/manual/en/book.apc.php

Is there a way to define constants that persist in memory when PHP loads?

Is there a way to define constants that boot into memory when the PHP process starts on the server? If so, can it be done with arrays, classes, and functions as well?
Before people start listing the different ways of declaring things that will be available across pages and scopes: I'm not asking this from a coding convenience, but rather a performance perspective.
It seems like a waste to keep loading things that never change, from pages or databases etc, on every script execution. Being a server-side process, I would think PHP has some way to read things into memory once and have them always available.
What you are looking for is an opcode cache. Opcode caches work by storing the compiled contents of PHP files in shared memory, then using that data to short-circuit the standard code parsing process when the file is included/required in the future.
The canonical opcode cache at this point is the PHP opcache extension, but a number of other opcode caches exist, including APC and XCache.
Opcode caches do not cache data from databases. There are other extensions which you can use to assist in this process, though, such as APCu which stores them in shared memory, or memcached which stores them in an external process. None of this is automatic, though, as caching data from a database requires some application knowledge to know what is useful to cache, and to handle cache invalidation when you update the database.
In general php is a single treaded environment and it does not actually run constantly (unlike java). Php starts working only when receives a command to do so. Then it parses the code into opcode and eventually executes it. And that technically happens on every request (or CLI command).
However, there are several ways how you can cache the opcode with not much efforts with APC: http://www.php.net/manual/en/book.apc.php

php Apc caching or File caching for semi-static website?

i'm new in PHP and want to try caching(for the first time), so i make website and it has :
dynamic home page
dynamic portfolio page
dynamic contact page
static about page
static admin page
so i read the tutorial about caching and i try to make my own caching system:
using file cache based on the what page is requested, when the page is requested the cache system will check if there's cache in cache directory if there's no cache file yet then write all the output(html) from the php script(in this case output from output buffer) and if there's cache file that corresponds with the specific id(based on URI) then just include_once() the html file.
Then i read in CodeIgniter(i make this website using CI) says there's APC for caching, then i read again about APC, what i read about APC is that it caches the DB results, but now i'm confused which should i use
what i get so far:
file caching probably would slower if there's alot of request (i dont know if this is true or not but i read it somewhere from search engine)
APC is fast
but i'm still confused which i should use , i'm on shared hosting
The levels of caching most relevant in a PHP application:
File / Script caching - The operating system will actually do this to a large extent. When a file is opened it's added to an OS-level cache. It stays there until the file is touched or the OS needs to free memory for other processes. A homegrown PHP solution isn't a good replacement for this.
Opcode caching - In order to function, PHP needs to parse and compile a script into opcodes. A mechanism like APC will cache the opcodes of every PHP script executed by Apache, provided that the cache doesn't overflow. A homegrown PHP solution build on top of APC can partially do this, but APC already does it ... so don't bother.
Query caching - If your script accesses a lot of data that doesn't change very frequently, or wherein some latency between updates and the visibility of those updates is acceptable, caching the results from complex queries is beneficial. A homegrown PHP solution built on APC is acceptable and beneficial at this level. But a database level solution is also appropriate here, and often more appropriate.
Output caching - If your page is largely deterministic and/or the same sort of latency applicable to query caching is acceptable, you can cache the entire output of the script using output buffering and APC. A homegrown PHP solution built on APC is acceptable here, but generally not necessary. If the page is static, you're probably not saving yourself any re-computation. And if it's dynamic, it's usually preferable to just re-render the page anyway.
In a dedicated or virtual-dedicated environment you'd need install APC (or something similar) yourself. But, in a shared hosting environment, it's very likely that APC is installed. And if it weren't you couldn't install it yourself anyway.
And, due to my own uncertainty, I'd recommend not performing any query or output caching with APC in a shared environment -- I'm not sure whether APC segregates caches by virtual host. Even if it does, I wouldn't assume that my site is truly a separate virtual host.

PHP APC, educate me

I'm currently implementing memcached into my service but what keeps cropping up is the suggestion that I should also implement APC for caching of the actual code.
I have looked through the few tutorials there are, and the PHP documentation as well, but my main question is, how do I implement it on a large scale? PHP documentation talks about storing variables, but it isn't that detailed.
Forgive me for being uneducated in this area but I would like to know where in real sites this is implemented. Do I literally cache everything or only the parts that are used often, such as functions?
Thanks!
As you know PHP is an interpreted language, so everytime a request arrives to the server it need to open all required and included files, parse them and execute them. What APC offers is to skip the require/include and parsing steps (The files still have to be required, but are stored in memory so access is much much faster), so the scripts just have to be executed. On our website, we use a combination of APC and memcached. APC to speed up the above mentioned steps, and memcached to enable fast and distributed storing and accessing of both global variables (precomputed expensive function calls etc that can be shared by multiple clients for a certain amount of time) as well as session variables. This enables us to have multiple front end servers without losing any client state such as login status etc.
When it comes to what you should cache... well, that really depends on your application. If you have a need for multiple frontends somewhere down the line, I would try to go with memcached for such caching and storing, and use APC as an opcode cache.
APC is both an opcode cache and a general data cache. The latter works pretty much like memcached, whereas the opcode cache works by caching the parsed php-files, so that they won't have to be parsed on each request. That can generally speed up execution time up quite a bit.
You don't have to implement the opcode caching features of APC, you just enable them as a php module.
APC cache size and other configuration information is here.

Categories