Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

caching

apc

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!

like image 252
Matt Avatar asked Feb 24 '23 14:02

Matt


2 Answers

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.

like image 171
hakre Avatar answered Mar 02 '23 00:03

hakre


P.S: I have never used APC(switched to different language before that I really understand the importance of APC), but I think I understand the concept. Please correct me if I am telling a lie somewhere.

> 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?

You can use apc_store to store data inside memory, which can be retrieved using apc-fetch. It does not store all your variables inside your program.

bool apc_store ( string $key , mixed $var [, int $ttl = 0 ] )

Note: Unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests (until the value is removed from the cache).

Parameters

key

Store the variable using this name. keys are cache-unique, so storing a second value with the same key will overwrite the original value.

From reading the docs I assume that you have to provide unique keys just like you would in memcached/redis. To store data private you would just get the session_id(unique per session) and use that as your prefix. You use the key to store your data. This Redis tutorial from Simon Willison also has a section describing how to use keys which I will quote below:

Redis is at heart a key-value store, so it makes sense to start by talking about keys. Keys should not contain whitespace - versions of Redis prior to 1.2 had trouble with this, and even now it's not guaranteed that any edge-case bugs have been ironed out. A common convention is to use obj-type:id:field, though now that Redis supports hashes as values this convention is likely to become less important.

like image 36
Alfred Avatar answered Mar 01 '23 22:03

Alfred