2

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?

Narf
  • 14,600
  • 3
  • 37
  • 66
Raja
  • 3,477
  • 12
  • 47
  • 89
  • PHP is an interpreted language, not a compiled language, in other words, when PHP tries to use one of your files, it first reads it and transforms (aka compiles) it into something akin to machine code that it can then run. This happens every time for every request. What `opcache` does is store this machine code in cache so that PHP can immediately use that instead of reading and parsing your PHP files. For complex sites, the speed boost from this can be exceptional. – Kevin Nagurski May 21 '15 at 14:43
  • If your rendered pages don't need real time/dynamic data then file/page caching is what you need from the server perspective. Set an expires header so the client will cache it from the client perspective. – AbraCadaver May 21 '15 at 14:59
  • @Kevin - wrong in one major regard - PHP is __not__ an interpreted language; it is a compiled language.... but it's JIT (Just in Time) compiled, which may be what's confusing you – Mark Baker May 21 '15 at 15:13
  • @MarkBaker Nope, not wrong http://stackoverflow.com/questions/1514676/is-php-compiled-or-interpreted PHP itself is compiled (written in C), but the code that you write is interpreted – Kevin Nagurski May 21 '15 at 15:16
  • @MarkBaker heck, their own [GitHub Repo](https://github.com/php/php-src) refers to it as an interpreter! – Kevin Nagurski May 21 '15 at 15:23
  • 1
    OK, so you're saying that a JIT compiler is the same as an interpreter.... that simply isn't the case. When a PHP file is loaded, then the entirety of that file is ___compiled___ to bytecode (and optionally cached in OpCache) similar to Java.... it's not interpreted a line at a time in the way traditional interpreted languages (like BASIC) are.... and that makes a big difference – Mark Baker May 21 '15 at 15:40

2 Answers2

3

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.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • So you are saying that in Opcache the Database results also stored in the memory? and no more time database query execution? – Raja May 21 '15 at 15:10
  • 1
    @Yadheendran - OpCache stores purely script bytecode, not data of any kind, so database results will never be stored in OpCache – Mark Baker May 21 '15 at 15:14
0

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.

Dat Tran
  • 1,576
  • 1
  • 12
  • 16