Skip to content

Instantly share code, notes, and snippets.

@RickardAhlstedt
Created September 23, 2019 18:32
Show Gist options
  • Save RickardAhlstedt/6d1050d7c2cd211adb4d124886c81f56 to your computer and use it in GitHub Desktop.
Save RickardAhlstedt/6d1050d7c2cd211adb4d124886c81f56 to your computer and use it in GitHub Desktop.
<?php
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);
$t = microtime(true);
$data = cache_get(‘my_key’);
echo microtime(true) - $t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment