The application scope is a sweet spot when writing for the web, where caching can make or break (really break) an app. It’s also a huge convenience when working with variables that are largely static during the life of the application. Some languages, like Coldfusion, provide this functionality out of the box–but PHP is lacking this feature.
Enter Chris Dary, programmer extraordinaire here at Arc90, with AppCache, a really nice PHP5 implementation of the application scope that leverages memcached for caching. You can get it, and check out some examples, over in the Arc90 lab.
arc90 lab said:
AppCache for PHP5
Speed up your apps and have a convenient application-level scope with AppCache by Chris Dary, the seventh tool from the arc90 lab….
Chris Dary said:
Thanks to ‘caffinated’ from ##php on irc.freenode.net for pointing out a minor race condition in cached keys.
If process A and B get their keys, A deletes a key, A updates, and then B updates, this can lead to incorrect data.
This should be fixed in the next version of the code, however for the moment AppCache should still be very usable and this will only occur under very specific conditions.
-Chris Dary
Enzo said:
Trying out Appcache. One quick question that I can’t get works. Sorry for my English. I have memcache installed and works fine. Both SimpleExample and ComplexExamples work fine too.
I’m trying to access the $app from global $app. In SimpleExample.php we set $app->sampleValue.=”Foo”;
Now in SimpleExample2.php I want to print that value.
global $app;
print_r($app->sampleValue);
That doesn’t work. Nothing is print. register_globals is on for Php5. What I’m missing here?
-Enzo
Chris D said:
Hey there Enzo,
If I had to take a guess, it’d be because you’re probably not initializing $app within SimpleExample2.php
Either include a common file that will include this line:
$app = new Application(‘Sample Application Name’);
or stick the above line in SimpleExample2.php so that it can connect to memcache successfully.
Enzo said:
You are fast! I just see that all I need to do in the SimpleExample2.php page is:
require ‘include/Application.php’; (I moved to my include dir)
$app = new Application(‘Sample Application Name’);
and then I can access $app->sampleValue; from my page 2.
I don’t need use keyword global at all in this case. So by giving same app string name in page 1 and page 2 here:
$app = new Application(‘Sample Application Name’);
this means I will access same object in memcache correct?
This is very nice. I just started to play with memcache and was thinking of writing layer like this. Good job.
Chris D said:
Glad I could help Enzo. :)
In fact, it sounds like you can make it even easier than you’ve got it.
If you put your $app initialization line into a configuration file, and then ‘require’ it at the beginning of every PHP file that needs to access it, you only have your initialization in one place, which is cleaner code.
Enzo said:
Yes, thanks. I will require in every file as you said. I was just testing out.
Firestarter+Squid+Lighttpd+AppCache+Memcache+APC+PHP5+MYSQL5+CENTOS5 = we see how far this scales soon !
Turbine said:
I am confuse with AppCache
when I write Asp or Jsp
the application scope var life cycle is end when the server shutdown
but with AppCache
I shutdown my appache server, app var life cycle is not end.
ok, I think maybe the problem is because the Memcache process is live when my appache server is shutdown
I kill the Memcache process and restart it, and app var life cycle is not end.
so I guess that if I want to end the application scope var life cycle,
I need restart Memcache process and my appache server togetther.
Is it right?
If it is true , how can I avoid to forget shutdow together?
Marcus B said:
Hey Chris,
I really like this wrapper around memcached. I’m glad I found it, because I was about to write pretty much the same thing!
I found a minor ‘buglette’ in the code though:
in prepare_cache() :
“$keymod = true;” Should say “$keys_modified = true;”
Hope that helps,
Cheers,
Marcus.