-
-
Save dougreese/a8eba3de5418a44418ff to your computer and use it in GitHub Desktop.
APC replacement functions for code using apc_* user based caching functions for servers running PHP 5.5 (where APC is no longer supported) or where APC is otherwise not installed. Stores data in file based cache files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
APC replacement functions | |
Replaces APC user cache functionality with file based caching. | |
Notes: | |
- cache files stored in result of sys_get_temp_dir() | |
- ttl not supported, cache files must be cleaned up manually | |
*/ | |
if (!function_exists('apc_fetch')) { | |
function apc_fetch($key, &$success = true) { | |
if (!($filename = apc_get_tmpfile($key, $f))) { | |
return false; | |
} | |
$size = filesize($filename); | |
if ($size == 0) { | |
return false; | |
} | |
if (!($d = fread($f, $size))) { | |
$success = false; | |
return false; | |
} | |
fclose($f); | |
if (!($unserialized_d = unserialize($d))) { | |
$success = false; | |
return false; | |
} | |
return $unserialized_d; | |
} | |
} | |
if (!function_exists('apc_store')) { | |
function apc_store($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); | |
clearstatcache($true, $filename); | |
return true; | |
} | |
} | |
if (!function_exists('apc_add')) { | |
function apc_add($key, $var, $ttl = 0) { | |
if (!($filename = apc_get_tmpfile($key, $f))) { | |
return false; | |
} | |
$size = filesize($filename); | |
if ($size != 0) { | |
// cache data already exists, don't overwrite | |
return false; | |
} | |
return apc_store($key, $var, $ttl); | |
} | |
} | |
if (!function_exists('apc_delete')) { | |
function apc_delete($key) { | |
if (!($filename = apc_get_tmpfile($key, $f))) { | |
return false; | |
} | |
$success = unlink($filename); | |
clearstatcache($true, $filename); | |
return $success; | |
} | |
} | |
function apc_get_tmpfile($name, &$f) { | |
$tmpdir = sys_get_temp_dir(); | |
if (!($f = fopen($tmpdir . '/' . $name, 'c+'))) { | |
return false; | |
} | |
return $tmpdir . '/' . $name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre> | |
<?php | |
include 'apc_overloads.php'; | |
function runtest($key, $value, $ttl) { | |
$dumpval = (bool)(isset($_GET['dump']) ? $_GET['dump'] : false); | |
echo "-- Testing type: " . gettype($value) . " --\n"; | |
$result = apc_store($key, $value, $ttl); | |
echo "apc_store result: " . ($result ? 'pass' : 'fail') . "\n"; | |
if ($dumpval) var_dump($key, $value); | |
$test = apc_fetch($key); | |
echo "apc_fetch result: " . ($test == $value ? 'pass' : 'fail') . "\n"; | |
if ($dumpval) var_dump($key, $value); | |
$result = apc_add($key, $value, $ttl); | |
echo "apc_add result - data exists: " . ($result ? 'fail' : 'pass') . "\n"; | |
if ($dumpval) var_dump($key, $value); | |
$result = apc_delete($key); | |
echo "apc_delete result: " . ($result ? 'pass' : 'fail') . "\n"; | |
if ($dumpval) var_dump($key); | |
$result = apc_add($key, $value); | |
echo "apc_add result - data previously deleted: " . ($result ? 'pass' : 'fail') . "\n"; | |
if ($dumpval) var_dump($key, $value); | |
$result = apc_delete($key); | |
echo "apc_delete result: " . ($result ? 'pass' : 'fail - cache data not cleaned up') . "\n"; | |
if ($dumpval) var_dump($key); | |
echo "\n"; | |
} | |
$key = 'test-data'; | |
$value = 123456789; | |
$ttl = 1234; | |
runtest($key, $value, $ttl); | |
$key = 'test-data'; | |
$value = '*** test string ***'; | |
$ttl = 1234; | |
runtest($key, $value, $ttl); | |
$key = 'test-data'; | |
$value = array( | |
'test-int' => 123456789, | |
'test-string' => '*** test data ***', | |
); | |
$ttl = 1234; | |
runtest($key, $value, $ttl); | |
?> | |
</pre> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment