user967451
user967451

Reputation:

PHP: Is just installing APC enough to enable caching or do I need to make changes to my code as well?

A very noob question here.

I just installed APC and when I go to the monitoring page (apc.php) and click on the "System Cache Entries" tab I can see a lot of pages on that list after browsing my app that's hosted on the server. To test I restarted apache and all the cache entries were gone but as soon as I started browsing more pages on my app again they started appearing on that list.

I didn't make any changes to my code, so is this all I have to do to enable optcode caching? Or are changes to my code also necessary?

I ask because my app is using codeigniter and there is a page in the codeigniter docs on caching docs that describes code changes:

http://codeigniter.com/user_guide/libraries/caching.html

Upvotes: 0

Views: 4762

Answers (2)

Bailey Parker
Bailey Parker

Reputation: 15905

APC stores opcode caches as they are parsed. As you have already discovered the caches only persist as long as apache remains open. But when an opcode cache is missing for a page that is requested, APC will store it for as long as Apache remains running. However, opcode caches are only half the battle. While it is true you will receive a speed increase from caching opcode, a lot of time in PHP is lost to file input/output and socket communications (ie. database queries). As long as you can be sure that your script is the only resource which will be modifying the database or a file, you can safely caching database query results or file contents so that each request doesn't need to touch the filesystem or database layer. The logic for this uses some APC functions:

if(apc_exists('some_database_value')) {
    $value = apc_fetch('some_database_value');
} else {
    //Query db for value, store in $value
    apc_store('some_database_value', $value);
}

The only disadvantage to this solution is if you need to modify any cached resource outside of the PHP script, you will need to clear the APC cache from the CLI.

Upvotes: 4

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

No, APC requires no code changes to accellerate the actual running of the code; for a bit more information, see for example this answer

With APC, you first get an opcode cache -- for that part, you having nothing to modify in your code : just install the extension, and enable it.

The opcode cache will generally speed up things : it prevents the PHP scripts from being compiled again and again, by keeping the opcodes -- the result of the compilation of the PHP files -- in memory.

Upvotes: 1

Related Questions