Skip to content

Instantly share code, notes, and snippets.

@LionsAd
Created July 14, 2016 12:31
Show Gist options
  • Select an option

  • Save LionsAd/a8da4367656c389b2545e2237ac1abe5 to your computer and use it in GitHub Desktop.

Select an option

Save LionsAd/a8da4367656c389b2545e2237ac1abe5 to your computer and use it in GitHub Desktop.
<?php
function yac_store() {
static $yac;
if (!isset($yac)) {
$yac = new Yac('');
}
return $yac;
}
function yac_key($key) {
$key_is_ascii = mb_check_encoding($key, 'ASCII');
if (strlen($key) < 48 && $key_is_ascii) {
return $key;
}
// Return a string that uses as much as possible of the original cache ID
// with the hash appended.
$hash = hash('sha1', $key);
if (!$key_is_ascii) {
return $hash;
}
return substr($key, 0, 48 - strlen($hash)) . $hash;
}
function apcu_add($key, $var = null, $ttl = 0) { return yac_store()->add($key, $var, $ttl); }
function apcu_cache_info($limited = false) { return yac_store()->info(); }
//function apcu_cas($key, $old, $new) { return apc_cas($key, $old, $new); }
function apcu_clear_cache() { return yac_store()->flush(); }
//function apcu_dec($key, $step = 1, &$success = false) { return apc_dec($key, $step, $success); }
function apcu_delete($key) { return yac_store()->delete($key); }
//function apcu_exists($keys) { return apc_exists($keys); }
function apcu_fetch($key, &$success = false) {
if (is_string($key)) {
$skey = yac_key($key);
return yac_store()->get($skey);
}
$key_map = array();
foreach ($key as $k => $v) {
$key_map[yac_key($v)] = $v;
}
$result = yac_store()->get(array_keys($key_map));
if (empty($result)) {
return $result;
}
$return = array();
foreach ($result as $key => $value) {
$return[$key_map[$key]] = $value;
}
return $return;
}
//function apcu_inc($key, $step = 1, &$success = false) { return apc_inc($key, $step, $success); }
//function apcu_sma_info($limited = false) { return apc_sma_info($limited); }
function apcu_store($key, $var = null, $ttl = 0) { $skey=yac_key($key); error_log(__FUNCTION__ . ':' . $key); return yac_store()->set($skey, $var, $ttl); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment