. ** ** it_cache.class - Caching functions */ class it_cache { static $_fetch_func; static $_store_func; static function _defaults($p) { $p += array( 'ttl' => 2, 'hostsfile' => '/opt/ultra/etc/memcached.hosts', 'safety' => 1, ); return $p; } /** * Get value for specific key from cache. Can be mixed value but no objects. WARNING: only distributed works from shell * @param $key Key to get value for * @param $p['distributed'] Use distributed memcache (scalars may become strings) [false] * @return Value for given key or null */ static function get($key, $p = array()) { $p = it_cache::_defaults($p); if (isset($GLOBALS['it_cache_local'][$key])) { # Use local copy $result = $GLOBALS['it_cache_local'][$key]; $success = true; } else if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) { $result = @$memcache->get($key); $success = !$memcache->getResultCode(); } if (!$success) $result = ($func = self::$_fetch_func) ? $func($key, $success) : null; return $success === false ? null : $result; } /** * Put value for specific key into cache. Can be mixed value but no objects. WARNING: only distributed works from shell * @param $key Key to put value with * @param $value Value to put (mixed but no objects allowed) * @param $p['distributed'] Use distributed memcache on live machines (scalars may become strings) [false] * @param $p['ttl'] Time to live for this key/value-pair in seconds [2] * @param $p['safety'] set to 0 to suppress an it::error in case of failure [1] * @return Returns $value */ static function put($key, $value, $p = array()) { $p = it_cache::_defaults($p); if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) $success = $memsuccess = @$memcache->set($key, $value, $p['ttl']); if (!$success) $success = ($func = self::$_store_func) ? $func($key, $value, $p['ttl']) : null; if ($memsuccess === false && $p['safety'] == 1) it::error(array_filter([ 'title' => ($p['distributed'] ? "memcache (" . ($memcache ? ($memcache->getResultMessage() . " on " . $memcache->getServerByKey($key)['host']) : "n/a") . ")" : self::$_store_func) . " in it_cache::put failed", 'body' => "key='$key'", 'id' => $p['distributed'] ? "it_cache_fail_" . $memcache->getServerByKey($key)['host'] : "it_cache_fail", 'timewindow' => "1200-1300", 'blockmailid' => $memcache ? "memcache_on_" . $memcache->getServerByKey($key)['host'] : null, 'blockmail' => $memcache? 12*3600 : null, ])); $GLOBALS['it_cache_local'][$key] = $value; # Also store local copy return $value; } static function _get_memcache($p) { $memcache_id = "it_cache_memcache_" . $p['hostsfile']; if (!isset($GLOBALS[$memcache_id]) && class_exists("Memcached", false)) { $memcache = new Memcached; foreach (array_filter(it::replace(array('[#\s].*' => ""), it::file($p['hostsfile']))) as $host) $reachable += intval(@$memcache->addServer($host, 11211)); $GLOBALS[$memcache_id] = $reachable ? $memcache : false; } return $GLOBALS[$memcache_id]; } } it_cache::$_fetch_func = function_exists("apcu_fetch") ? "apcu_fetch" : (function_exists("apc_fetch") ? "apc_fetch" : null); it_cache::$_store_func = function_exists("apcu_store") ? "apcu_store" : (function_exists("apc_store") ? "apc_store" : null);