<commit_before><?php

abstract class CM_Cache_Abstract extends CM_Class_Abstract {
	protected $_runtimeCache = array();

	/**
	 * @return CM_Cache_Abstract
	 */
	public static final function getInstance() {
		if (!isset(static::$_instance)) {
			static::$_instance = new static();
		}
		return static::$_instance;
	}

	public static final function set($key, $value, $lifeTime = null) {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		$cache->_runtimeCache[$key] = $value;
		CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-set', $key);
		$cache->_set($key, $value, $lifeTime);
	}

	public static final function get($key) {
		if (!static::_enabled()) {
			return false;
		}
		$cache = static::getInstance();
		if (array_key_exists($key, $cache->_runtimeCache)) {
			return $cache->_runtimeCache[$key];
		}
		CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-get', $key);
		if (($value = $cache->_get($key)) !== false) {
			$cache->_runtimeCache[$key] = $value;
		}
		return $value;
	}

	public static final function delete($key) {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		unset($cache->_runtimeCache[$key]);
		$cache->_delete($key);
	}

	public static final function flush() {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		$cache->_runtimeCache = array();
		$cache->_flush();
	}

	public static final function setTagged($tag, $key, $value, $lifetime = null) {
		static::_callInstance('setTagged', func_get_args());
	}

	public static final function getTagged($tag, $key) {
		return static::_callInstance('getTagged', func_get_args());
	}

	public static final function deleteTag($tag) {
		static::_callInstance('deleteTag', func_get_args());
	}

	public static final function key($namespace, $key) {
		if (!is_scalar($key)) {
			$key = md5(serialize($key));
		}
		return $namespace . '_' . $key;
	}

	public static final function __callStatic($name, $arguments) {
		static::_callInstance($name, $arguments, true);
	}

	protected static final function _callInstance($functionName, $arguments, $log = false) {
		if (!static::_enabled()) {
			return false;
		}
		$cache = static::getInstance();
		if ($log) {
			CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-' . $functionName, implode(', ', $arguments));
		}
		return call_user_func_array(array($cache, '_' . $functionName), $arguments);
	}

	/**
	 * @return boolean
	 */
	protected static function _enabled() {
		return static::_getConfig()->enabled;
	}

	/**
	 * @return string<commit_msg>t8: Refactoring Cache::key() function<commit_after><?php

abstract class CM_Cache_Abstract extends CM_Class_Abstract {
	protected $_runtimeCache = array();

	/**
	 * @return CM_Cache_Abstract
	 */
	public static final function getInstance() {
		if (!isset(static::$_instance)) {
			static::$_instance = new static();
		}
		return static::$_instance;
	}

	public static final function set($key, $value, $lifeTime = null) {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		$cache->_runtimeCache[$key] = $value;
		CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-set', $key);
		$cache->_set($key, $value, $lifeTime);
	}

	public static final function get($key) {
		if (!static::_enabled()) {
			return false;
		}
		$cache = static::getInstance();
		if (array_key_exists($key, $cache->_runtimeCache)) {
			return $cache->_runtimeCache[$key];
		}
		CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-get', $key);
		if (($value = $cache->_get($key)) !== false) {
			$cache->_runtimeCache[$key] = $value;
		}
		return $value;
	}

	public static final function delete($key) {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		unset($cache->_runtimeCache[$key]);
		$cache->_delete($key);
	}

	public static final function flush() {
		if (!static::_enabled()) {
			return;
		}
		$cache = static::getInstance();
		$cache->_runtimeCache = array();
		$cache->_flush();
	}

	public static final function setTagged($tag, $key, $value, $lifetime = null) {
		static::_callInstance('setTagged', func_get_args());
	}

	public static final function getTagged($tag, $key) {
		return static::_callInstance('getTagged', func_get_args());
	}

	public static final function deleteTag($tag) {
		static::_callInstance('deleteTag', func_get_args());
	}

	/**
	 * @param mixed $keyPart ...
	 * @return string
	 */
	public static final function key($keyPart) {
		$parts = func_get_args();
		foreach ($parts as &$part) {
			if (!is_scalar($part)) {
				$part = md5(serialize($part));
			}
		}
		return implode('_', $parts);
	}

	public static final function __callStatic($name, $arguments) {
		static::_callInstance($name, $arguments, true);
	}

	protected static final function _callInstance($functionName, $arguments, $log = false) {
		if (!static::_enabled()) {
			return false;
		}
		$cache = static::getInstance();
		if ($log) {
			CM_Debug::get()->incStats(strtolower($cache->_getName()) . '-' . $functionName, implode(', ', $arguments));
		}
		return call_user_func_array(array($cache, '_' . $functionName), $arguments);
	}

	/**
	 * @return boolean
	 */
	protected static function _enabled() {
		return static::_getConfig()->enabled;
	}

	/**
	 * @return string
{'fraction_non_alphanumeric': 0.1833616298811545, 'fraction_numerical': 0.0005659309564233164, 'mean_word_length': 4.581052631578947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242166', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1700, 'n_words': 370}