APC is most widely known for allowing the end user to be able to track the progress of file transfers. APC, which stands for “Alternative PHP Cache” does much more than that. Complex and large scale websites can benefit from it’s ability to cache data and as well as its optimization of PHP code.
If you are looking for a tutorial on using APC for tracking file upload progress then visit my tutorial for File Upload Progress
APC does not have a large host of commands, this does not ease the complexity. We will take a look at a few simple functions and explain their use, in addition I will try to explain the practical reasons to use them. There are a few warnings to adhere to when trying utilize alternative PHP cache, these will also be discussed.
APC Functions – The Basics
In order to write to and read from the cache we will use two separate functions, the first “apc_store” will allow us to store a variable. The command “apc_fetch” will retrieve it. These functions are able to store arrays as well, however multidimensional arrays are a different story. A basic example on how to store a single variable would be as follows:
apc_store("test", $cachevar);
//To retreive our cached variable we will call it by its name "test"
var_dump(apc_fetch("test"));
The variable above will remain in the cache until we either clear it or restart our server. We can also set a time limit on the cached variable so that it will be removed after x amount of time. This is called the “ttl”(time to live) and is used: apc_store(“test”, $cachevar, 30); .
To delete the variable stored in the cache you would simply call the apc_delete function and specify the key:
As we mentioned above it is possible to store arrays, anything more than a single level deep will not work. A way to overcome this is by first serializing your data. PHP uses serialize as well as unserialize to allow a work around for arrays.
A Practical Use
Instead of using sessions or a database to collect user information you could store their data within the cache for a given time period, say 10 minutes. You could collect the users IP address or a password from the user which will allow you to set the key for your apc_store function. The user will then have x amount of time to complete registration based on the “ttl” and they can exit and return the site, even close their browser, and their registration info will still be available.
APC… The Need For Speed
There has been a lot of debate over APC and if installing it actually speeds up the execution of PHP. By default APC will compile and store each PHP file as it is executed for the first time. The compiled version is stored in bytecode and placed in shared memory.
To me this proves one major point, if the code is pre-compiled then there is no need to compile every time the code is hit by a user. This means more available time for the CPU to handle other tasks thus reducing server load.
APC will allow you to compile files manually which is great for larger sites running on multiple servers. Before the server is brought back online it is possible to compile everything, which will reduce the initial load. Even smaller sites can compile additions to increase performance when expanding. To compile a file we would use the command apc_compile and simply feed it a filename:
apc_compile_file($file);
//To compile a complete directory we would use apc_compile_dir
$recursively = true;
apc_compile_dir($dir, $recursively);
A great example on recursively compiling an entire site can be found in the PHP manual for APC.
Speed
For PHP intensive sites you should notice a large decrease in execution time after installing APC. For maximum speed enabling APC under FastCGI can produce great results. There does seem to be a conflict when trying to run this on older versions of PHP. Running 5.2+ you should not run into any problems.
Cache Information and Commands
In order to tell what resources are available or the remaining size of your cache you will need to run a command or two. APC comes with a couple functions which allows you to extract the current state of the cache.
The first command is: apc_cache_info(), which will return cached files as well as the number of hits and misses. Below will show how to view cached files.
echo("<b>File Number:</b> $file<br/>");
foreach ($cache[‘cache_list’][$file] as $data=>$info){
echo("<b>$data:</b> $info<br/>");
}
}
//Below is an example of what the output would look like for each file:
//File Number: 1
//filename: filename
//device: 344467735
//inode: 11834
//type: file
//num_hits: 3
//mtime: 1249415260
//creation_time: 1252725219
//deletion_time: 0
//access_time: 1252725234
//ref_count: 0
//mem_size: 3877
The second command we can use is apc_sma_info() which will tell you about the shared memory allocation:
Cleaning Up
There are several ways to clear the cache. The easiest which happens automatically is when your server is restarted. The second is using the apc_clear_cache() function. Using APC’s built in function can be very time consuming especially for large sites. Rebooting in many cases will clear the cache much faster then using this command.
Final Thoughts
While APC provides some great tools for speeding up PHP it still has some room for improvement. Using this tool will provide an increase in server performance and it’s functionality can even be implemented into the user experience. I am waiting to see what else others can do with this tools, if you find any innovative uses please leave us a comment!
![[del.icio.us]](http://scriptperfect.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://scriptperfect.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://scriptperfect.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://scriptperfect.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://scriptperfect.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://scriptperfect.com/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://scriptperfect.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://scriptperfect.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://scriptperfect.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://scriptperfect.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://scriptperfect.com/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://scriptperfect.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://scriptperfect.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://scriptperfect.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://scriptperfect.com/wp-content/plugins/bookmarkify/email.png)
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Website Trackback Link…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Links…
[...] that’s the end of this short article. Here you’ll discover some sites that we presume you will enjoy, simply click the links through [...]…
Trackbacks…
[...] we wish to remember all kinds of other sites on the net, whether or not they may not be linked to us, by simply linking to them. Below are some webpages worth checking out [...]…
Sources…
[...]check below, are a couple of totally unrelated blogs to ours, however, they are undoubtedly trustworthy sources that we employed[...]……
miss…
[...]There are many heartburn home remedies and it is not that serious issue. Anyone can suffer from heartburn due to various reasons like change in food style and lifestyle[...]…
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
far east plaza in singapore…
[...]epidemiologists hunting the virus now know definitively that [...]…
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Check This Out…
[...]Here are some of the sites we recommend for our visitors[...]…
window cleaning…
[...]the time to read or visit the material or sites we’ve linked to below the[...]…
Related.. Trackback…
[...]the time to read or visit the content or sites we have linked to below the[...]…
as the only option for lodging. although…
camping with the family can certainly be fun, most travelers would not like that as their only option, especially for business travel. and it certainly wouldn’t work for a family vacation to disney world. just as time changed tourist camps into…
Gems form the internet…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……
Read was interesting, stay in touch……
[...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……
far east plaza directory…
[...]It was as if Hendra virus awoke[...]…
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……
Trackback for this article…
[...] Left you a trackback for your visitors to get additional information [...]…
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
queens movers…
[...]please check out the sites we comply with, including this one particular, because it represents our picks from the web[...]…
storage nyc…
[...]always a significant fan of linking to bloggers that I adore but really don’t get a great deal of link adore from[...]…
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
modern politics with machiavelli…
[...]food production and animal husbandry of waterfowl and pigs[...]…
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Coolest news on earth, you have to see this!…
[...] Here’s a number of relevant information [...]…
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
more…
[...]With heartburn home remedies we can cure this entire problem. Heartburn remedies are easy .heartburn can occur for anyone and it is a very simple trouble[...]…
Extra Reading…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Dreary Day…
It was a dreary day here today, so I just took to piddeling around on the internet and found…
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
cipa…
Bardzo wielka cipka z sztucznym kutasem w pochwie….
Great information…
This is certainly exceptional. A particular checked out involving blog posts therefore we are confounded. We are most certainly curious about this type of accessories. Person appreciate going up on your knowledge, and number your time inside this. Plea…
Can’t Get Any Better Than This…
[...]Right on the spot! I am in search of this. Keep it up[...]……
fotoshooting…
[...]we like to honor lots of other web internet sites around the internet, even when they aren?t linked to us, by linking to them. Below are some webpages really worth checking out[...]…
[...]the time to read or visit the content or sites we have linked to below the[...]……
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Great website…
[...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……
Wow!…
A very spectacular post….
Hello readers,…
Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up…
sopa bill rejected…
[...]all options set before him just before he handed down his death sentence to [...]…