So this is whats bothering me. I just installed APC cache and Im testing it.
When using APC Admin interface, in apc.php file, I can see all the info about APC etc.
When I go to System Cache Entries I can see that every script i invoke gets written there.
So does this means that APC Cache works out of the box? I can just install APC cache and it already speeds up my application by caching scripts? And if I want I can then cache variables to make it even faster?
Hope you get the question, its probably simple to someone with more experience with APC.
Its I know i can add some variables to cache, and then get them out and that will speed up my app. But is it true, that APC will speed up the app and cache scripts all by him self?
And is there any good documentation where I could learn more about APC?
Yeah, APC "just works". Anyone running PHP in production without APC/(other opcodecache) is missing out on the easiest performance improvement they can readily achieve.
A few caveats though.
If you are in development, you can still run APC, however, you probably want to enable stat calls. This means that APC will check the last modified of your files.
apc.stat = [1|0]
So if you don't have stat calls enabled, and you change a file and APC has already cached it, then it won't observe your changes, and you will continue using the cached opcode.
As you have mentioned, APC isn't just for opcode caching, it is also useful for user space caching. You have your system cache and your user cache.
You can store things against your user cache by just performing something like:
apc_store("fooKey", "barValue");
Related
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.
I'm struggling to find any logic in the way APC clears up its old entries.
Especially with user entries, I notice the fragmentation quickly increasing to unexpected levels.
Although apc.ttl and apc.user_ttl are set at 3600, I notice a LOT of old entries in the APC cache which are not getting cleared. I have repeatedly increased the size of the memory for APC, but it only makes it last a little longer before reaching 100% fragmentation.
So, why could this be happening? Actually I would consider this a bug in APC, it's just not normal behaviour. I would expect some APC process to clear out old entries from time to time.
Also, could it lead to the conclusion that it would be better to use another caching system for PHP, and only use APC as an opcode cache (where the lack of cleaning is less of a problem)?
If you want to ensure old values are removed. You need to explicitly remove it.
I use a combination of APC and memcached in my applications. APC provides opcode caching as well as caches local data that is likely to remain unchanged (Like config files). All other objects are cached in memcached using some sort of a read through logic. (Gets cleared if data is changed)
When I do a new release, I restart APC (via apache or php if its a separate process). That will clear the APC cache and the config files will get reloaded shortly afterwards. I will restart memcached if the objects have changed significantly enough to cause issues if its loading old cache data.
I installed APC on my VPS and it works great with W3 Cache wordpress plugin. My problem is that there is one database in MySQL which is pinged by client end every few seconds to see if there are new updates. These db contains certain time sensitive information and hence it can't be part of cached data.
How can I disable APC for this database/files? or Can I set a very short expiry of certain type of data?
Any help is highly appreciated.
APC does two things. It provides a transparent cache of PHP bytecode, and it can cache data at the request of the application.
There is no reason at all to attempt to disable the bytecode cache, but that's not what you seem to be talking about here. The bytecode cache just caches bytecode, not data.
If the application you are using asks APC to cache certain data, and it does not contain an option to disable this caching if APC is installed and available, you are going to need to modify that application. Look for calls to apc_store and apc_fetch and alter the code as required.
As mentioned in the comments, your real problem is probably with the Wordpress caching plugin that you've chosen, not with APC. APC just stores data. If it can not disable itself for selected pages, you may need to find a solution that can, or find another way to get to the data you need that bypasses it.
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
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.
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.
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.