Skip to content

Instantly share code, notes, and snippets.

@aholmes
Created December 23, 2013 19:49
Show Gist options
  • Save aholmes/8103409 to your computer and use it in GitHub Desktop.
Save aholmes/8103409 to your computer and use it in GitHub Desktop.
Overload apc_fetch and apc_add as a quick replacement for APC methods on systems that don't support it (like PHP 5.5 on OSX). These methods are not an exact reimplementation of the defined documentation on http://php.net. TTL is ignored, for one.
<?php
// apc crashed on my dev box, so overload the methods if they don't exist
if (!function_exists('apc_fetch')) {
function apc_fetch($key, &$success) {
if (!($filename = apc_get_tmpfile($key, $f))) {
return false;
}
if (!($d = fread($f, filesize($filename)))) {
$success = false;
return false;
}
fclose($f);
if (!($unserialized_d = unserialize($d))) {
$success = false;
return false;
}
return $unserialized_d;
}
}
if (!function_exists('apc_add')) {
function apc_add($key, $var, $ttl = 0) {
if (!($filename = apc_get_tmpfile($key, $f))) {
return false;
}
if (!($serialized_d = serialize($var))) {
return false;
}
if (!ftruncate($f, filesize($filename))) {
return false;
}
if (!fwrite($f, $serialized_d)) {
return false;
}
fclose($f);
return true;
}
}
function apc_get_tmpfile($name, &$f) {
$tmpdir = sys_get_temp_dir();
if (!($f = fopen($tmpdir . '/' . $name, 'c+'))) {
return false;
}
return $tmpdir . '/' . $name;
}
@dougreese
Copy link

Added support for apc_store and apc_delete here and a simple test script: https://gist.github.com/ReeseSys/a8eba3de5418a44418ff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment