A Universal Caching Algorithm for PHP using Memcached
Here is an elegant way to use the same caching logic for all function calls which should have a cache. With the proliferation of 3rd party APIs I was quite happy to find a way to address them all with a single mechanism.
[php]function expensive_third_party_call( $param1, $param2 ) {
// universal caching algorithm header
$result = memcached_retrieve( __FUNCTION__ . serialize(func_get_args()) ) ;
if( $result!==null ) {
return $result ;
}
// this is where the third party call actually happens, if we are hit the cache missed
$to_return = /* some super complex and time consuming logic, throw in a couple of web calls*/ ;
// universal caching algorithm footer
memcached_store( __FUNCTION__ . serialize(func_get_args()), $to_return, CACHE_TIMEOUT ) ;
return $to_return ;
}
////////// helper functions bellow //////////
$m = false ;
function memcached_retrieve( $key ) {
global $m ;
$new_key = md5( $key ) ;
if( $m===false ) {
$m = new Memcached() ;
$m->addServer( ‘localhost’, 11211 ) ;
}
$temp = $m->get( $new_key ) ;
$result_code = $m->getResultCode() ;
if( $result_code==Memcached::RES_SUCCESS ) {
return $temp ;
} else if( $result_code==Memcached::RES_NOTFOUND ) {
return null ;
} else {
echo "error: can’t retrieve memcached key {$key} with result_code {$result_code}" ;
}
return null ;
}
function memcached_store( $key, $data, $timeout ) {
global $m ;
$new_key = md5( $key ) ;
if( $m===false ) {
$m = new Memcached() ;
$m->addServer( ‘localhost’, 11211 ) ;
}
// a little heavy handed but we use null to represent that nothing was found in the cache so we can’t have this be the data
if( $data===null ) {
$data = false ;
}
$m->set( $new_key, $data, $timeout ) ;
$result_code = $m->getResultCode() ;
if( $result_code!==Memcached::RES_SUCCESS ) {
echo "error: can’t store memcached key {$key} with result_code {$result_code}" ;
return false ;
} else {
return true ;
}
return false ;
}
[/php]
Requirements
- apt-get install memcached php-memcached
- make sure to define CACHE_TIMEOUT
Functioning principle
Using PHP’s awareness of the current function it is in along with the parameters which are passed to it, we derive a unique key which is used so store and retrieve from the cache.
The garlic is in
A bit late and not super well done but we don’t want to skip a season. We planted ~400 cloves.
We got leaves from the forest, one the of many things trees give us.

Today’s insulation is tomorrow’s soil 
Cidre
Hard time taking pictures of the process, we’re at 15 gallons of cider, 5 of which are now laced with the inebriating nectar of the gods :). We followed an extremely simple farm recipe, the cider is good but not great. The 2 gallons with honey tasted best, maple syrup left no particular taste but did make the cider a little stronger. Good stuff!
Every batch labeled with a picture for the story of the tree it came from.
“Old man’s arch”, “Maple ghost moon”, Honey third from the left” are all there.

Golden age of the Monarchs
Summer 2016 revealed amazing firefly spectacles. They left the stage this year to the Monarch butterfly.
caterpillar
I never saw butterflies as very good fliers, they seem to land on whatever the wind blows them on. Only after following one a while did I realize it hit a milkweed every single time. Despite of their completely erratic flight pattern, they are in fact quite calculated.
The light at the end of the tunnel
I have been pushing hard for days to get to a point where we can start using the new space. I can see that the rough construction is almost over and that things get easier from then on. In fact, I can see that I will never push as hard as I did these past couple of years. This Summer has been incredible in that the land and the house are really taking shape into the dream we are pursuing. And also because we know the hardest is behind us after more than 2 years of ruthless efforts. And so, while it’s psychologically very hard to muster the resolve to spend days working to exhaustion on ladders in the Sun, I also know this is the last such sacrifice I’ll be doing. Future projects will have a much saner rhythm to them because our livelihood will not depend on them. It is with the knowledge that it only gets easier from this that I plowed through 5 more days of construction.
To finish the sheathing of the second floor I’ve received help from Chris & Lou. Bringing 4’x8′ half inch sheet of wood up ladders is both tricky & tough. Both Chris & Lou are experienced carpenters, I’ve learned a whole lot from them, including how to make this hard task as easy as possible. They showed me many other tricks.
It kind of looks like a bunker without windows.
Insulation & vapor barrier, it’s a very nice feeling to start thinking about the inside.
An addition to the family
Of wood stoves. This little Alpiner will be in charge of the house addition although it will probably not be hooked up to a chimney before next year.
The tractor is once again proving itself invaluable. The front loader fits right in the truck within 0.5″.
My new hobby: taking wood stoves for a ride.
After an hour of trying various techniques, Justin & I give up for the day. The stove is just too heavy to get up the steps.
It’ll sit outside until next time.
Take 2, with a come-along. Same awesome tool I used to move 2 trees a while back.
New inverter
The old inverter I bought second hand for $20 bit the dust. In part due to my lack of cable strain relief which created contact between ground and negative, oops :\ lessons learned. I bought this new guy which works so much better I think the old one was on the way out anyway. I especially love the fact it comes with a remote start/stop. Since we rarely use 110AC and the solar shed is 50′ from the house, it’s perfect to remove phantom loads. It’s not like we remember to ever turn it off, but we can 🙂




















