Yoast: RT @idivenow: I just donated to @Yoast for his awesome plugins, you should too: http://t.co/7GTWe1rE <-- THX!!
![]()
PHP-APC: Speed up your web applications!
As regular readers of this blog might know I have written quite some tools using the different API's of search engines, and always found them quite useful. When I was implementing my sitewide search function, one of the things that bothered me that it was a bit slow. I knew that I had seen some caching implementations on the Yahoo PHP developer center, but I hadn't bothered up till then to look at them a bit better.
Now I did, and I found the cacheAPC example to be very, very easy. It relies on the Alternative PHP Cache, an opcode cache PECL extension for PHP. I wrote two functions, which I then put in to all my pieces of code which I've published that use a lot of calls to the different API's. The first is curlopen, the function I use to open connections:
function curlopen($request) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$results = curl_exec ($ch);
curl_close($ch);
return $results;
}The second one is the actual cache function, look at how easy it is:
function request_cache($url, $ttl) {
if(!$doc = apc_fetch($url)) {
$doc = curlopen($url);
if ($doc === false) return false;
apc_store($url,$doc, $ttl);
}
return $doc;
}It basically does three things:
- It looks if the requested resource is already in the cache, and if it is, it fetches that;
- If it's not, it opens it through
curlopenand stores it in the cache; - It returns the requested data;
As you can see the request_cache function takes two parameters: the request url and the TTL, which, in seconds, determines how long that resource should be cached. Now if you request a PageRank for a URL, it's fairly safe to set this to 24 hours, and you can see how much requests this saves!




by Hong Kong SEO on 8 April, 2007 at 17:58
But is this application SEO friendly?
by Joost de Valk on 8 April, 2007 at 19:33
It doesn't change any output, so yes, it is. It works server side before anything is written to the client.
by steve M on 12 September, 2007 at 07:25
Good article in a simple usefull understandable style,What if we increase the TTL,will it be more cache boost.I would like know does it support SEO?
http://www.websites-design.com.au/
by steve M on 12 September, 2007 at 07:39
Thanks, Despite the title of my original post, I think I agree with your post. Of course.What if we increase TTL timing, will it boost cache.i would like to know does this application support SEO.
http://www.websites-design.com.au/
by rakesh on 5 October, 2007 at 09:09
what is the purpose of curlopen
by Joost de Valk on 5 October, 2007 at 09:10
It's a function I wrote that get's the content from a URL. Basically the CURL version of file_get_contents.
by rakesh on 5 October, 2007 at 09:18
is this curl works on ftp only
by Joost de Valk on 5 October, 2007 at 09:18
No CURL works on everything.
by rakesh on 5 October, 2007 at 09:25
why can we get the url from curl, actually we just want to
check the data available in that cache only
by Joost de Valk on 5 October, 2007 at 09:28
Well this function uses curlopen to request the data from the URL when it's not currently in the cache.
by rakesh on 5 October, 2007 at 12:46
if i use In my application can i change anything in that 2 parts
means in curl and memcache
by rakesh on 5 October, 2007 at 12:48
sorry not memcache in APc cache
by rakesh on 5 October, 2007 at 21:03
can you please send one complete application using
apc cache please it's very urgent.
thank you.
by Joost de Valk on 5 October, 2007 at 21:26
No. You can hire me for Eur 75 per hour.
by Ondra on 13 July, 2008 at 18:31
Thanks for quick introduction to APC, it was exactly what I was looking for. PS: serene discussion above :)