Search code examples
phpcachingapc

PHP - APC Cache - User specific data vs data accessible by all users


I have read a couple of tutorials on several web sites as well as a few questions here on StackOverFlow about the subject and I still couldn't find a clear answer to my question.

I am wondering how APC Cache manages/saves the user-specific data (variables that will be used in the php code, that are user-specific. In other words, data that should not be seen by other users.) and how does it save the publicly available data that all users can see?

I am just trying to understand how it works. I know that APC "saves" or keeps in memory required and included files... but what if those included files have user-specific variables in the code? If let's say in /account/user_profile.php I use several variables like $firstname $lastname $address, etc. will those variables be kept in memory? If, for example, John X is logged in at the time the cache is being updated or saved, then APC will always remember John as $firstname and X as $lastname? If another user goes to the same page, I want him to see its user profile details, not John's.

I know this might have been discussed already, but I need a clear answer, please.

Thank you!


Solution

  • You have the false understanding of the APC cache. It's a bytecode-cache, which means it will store the bytecode of a PHP script. This will save the PHP-interpreter the next time to create the bytecode again from the script because it's already there.

    Note: Since PHP Version 5.5 it comes with it's own OPCode Cache Core Extension, named Opcache. Usage of APC for Opcode-Caching is inofficially deprecated. Consult your Sysadmin or Opdesk for detailed information and options, the general principles outlined in this answer still apply regardless of the extensions name.

    In normal PHP execution your scripts code will be taken and compiled into byte-code. This byte-code will then be executed by the php processor. It's a common pattern for JIT compilers.

    So w/o a bytecode-cache, the bytecode needs to be compiled on each request. With a bytecode-cache, this step needs to be done only once across all requests. Next time the bytecode is already in the cache and can be executed straight-forward.

    This is totally unrelated to variable contents, it's just for the code.