500X Faster Caching than Redis/Memcache/APC in PHP & HHVM

function cache_set($key, $val) {
$val = var_export($val, true);
// HHVM fails at __set_state, so just use object cast for now
$val = str_replace('stdClass::__set_state', '(object)', $val);
// Write to temp file first to ensure atomicity
$tmp = "/tmp/$key." . uniqid('', true) . '.tmp';
file_put_contents($tmp, '<?php $val = ' . $val . ';', LOCK_EX);
rename($tmp, "/tmp/$key");
function cache_get($key) {
@include "/tmp/$key";
return isset($val) ? $val : false;
$data = array_fill(0, 1000000, ‘hi’); // your application data here
cache_set('my_key', $data);
apc_store('my_key', $data);
// note: make sure you run this on a separate request from cache_set to ensure PHP's opcache will actually cache the file$t = microtime(true);
$data = cache_get(‘my_key’);
echo microtime(true) - $t;
// 0.00013017654418945
$t = microtime(true);
$data = apc_fetch(‘my_key’);
echo microtime(true) - $t;
// 0.061056137084961



Co-founder @ Guava Health • Founder @ Imgflip • Former Director of Engineering @ Graphiq (acquired by Amazon)

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store