<commit_before><?php
/**
 * Garp_Cache_Manager
 * Provides various ways of purging the many caches Garp uses.
 *
 * @author       <NAME> | grrr.nl
 * @version      1.1
 * @package      Garp_Cache
 */
class Garp_Cache_Manager {
	/**
 	 * Read query cache
 	 * @param Garp_Model_Db $model
 	 * @param String $key
 	 * @return Mixed
 	 */
	public static function readQueryCache(Garp_Model_Db $model, $key) {
		$cache = new Garp_Cache_Store_Versioned($model->getName().'_version');
		return $cache->read($key);
	}

	/**
 	 * Write query cache
 	 * @param Garp_Model_Db $model
 	 * @param String $key
 	 * @param Mixed $results
 	 * @return Void
 	 */
	public static function writeQueryCache(Garp_Model_Db $model, $key, $results) {
		$cache = new Garp_Cache_Store_Versioned($model->getName().'_version');
		$cache->write($key, $results);
	}

	/**
	 * Purge all cache system wide
	 * @param Array|Garp_Model_Db $tags
	 * @param Boolean $createClusterJob Whether this purge should create a job to clear the other nodes in this server cluster, if applicable.
	 * @return Void
	 */
	public static function purge($tags = array(), $createClusterJob = true) {
		if ($tags instanceof Garp_Model_Db) {
			$tags = self::getTagsFromModel($tags);
		}
		self::purgeStaticCache($tags);
		self::purgeMemcachedCache($tags);
		self::purgePluginLoaderCache();

		$ini = Zend_Registry::get('config');
		if ($createClusterJob && $ini->app->clusteredHosting) {
			Garp_Cache_Store_Cluster::createJob($tags);
		}
	}

	/**
 	 * Clear the Memcached cache that stores queries and ini files and whatnot.
 	 * @param Array|Garp_Model_Db $modelNames
 	 * @return Void
 	 */
	public static function purgeMemcachedCache($modelNames = array()) {
		if ($modelNames instanceof Garp_Model_Db) {
			$modelNames = self::getTagsFromModel($modelNames);
		}

		if (empty($modelNames)) {
			if (Zend_Registry::isRegistered('CacheFrontend')) {
				$cacheFront = Zend_Registry::get('CacheFrontend'); 
				$cacheFront->clean(Zend_Cache::CLEANING_MODE_ALL);
			}
		} else {
			foreach ($modelNames as $modelName) {
				$model = new $modelName();
				self::_incrementMemcacheVersion($model);
				if ($model->getObserver('Translatable')) {
					// Make sure cache is cleared for all languages.
					$locales = Garp_I18n::getLocales();
					foreach ($locales as $locale) {
						$modelFactory = new Garp_I18n_ModelFactory($locale);
						$i18nModel = $modelFactory->getModel($model);
						self::_incrementMemcacheVersion($i18nModel);
					}
				}
			}
		}
	}

	/**
 	 * This clears Zend's Static caching.
 	 * @param Array|Garp_Model_Db $modelNames Clear the cache of a specific bunch of models.
 	 * @param String $cacheDir Directory containing the cache files
 	 * @return Void
 	 */
	public static function purgeStaticCache($modelNames = array(), $cacheDir = false) {
		if ($modelNames instanceof Garp_Model_Db) {
			$modelNames = self::getTagsFromModel($modelNames);
		}
	
		$cacheDir = $cacheDir ?: self::_getStaticCacheDir();
		if (!$cacheDir) {
			return;
		}
		$cacheDir = str_replace(' ', '\ ', $cacheDir);
		$cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

		// Destroy all if no model names are given
		if (empty($modelNames)) {
			$allPath = $cacheDir.'*';<commit_msg>Catch model already localized exception<commit_after><?php
/**
 * Garp_Cache_Manager
 * Provides various ways of purging the many caches Garp uses.
 *
 * @author       Harmen Janssen | grrr.nl
 * @version      1.1
 * @package      Garp_Cache
 */
class Garp_Cache_Manager {
	/**
 	 * Read query cache
 	 * @param Garp_Model_Db $model
 	 * @param String $key
 	 * @return Mixed
 	 */
	public static function readQueryCache(Garp_Model_Db $model, $key) {
		$cache = new Garp_Cache_Store_Versioned($model->getName().'_version');
		return $cache->read($key);
	}

	/**
 	 * Write query cache
 	 * @param Garp_Model_Db $model
 	 * @param String $key
 	 * @param Mixed $results
 	 * @return Void
 	 */
	public static function writeQueryCache(Garp_Model_Db $model, $key, $results) {
		$cache = new Garp_Cache_Store_Versioned($model->getName().'_version');
		$cache->write($key, $results);
	}

	/**
	 * Purge all cache system wide
	 * @param Array|Garp_Model_Db $tags
	 * @param Boolean $createClusterJob Whether this purge should create a job to clear the other nodes in this server cluster, if applicable.
	 * @return Void
	 */
	public static function purge($tags = array(), $createClusterJob = true) {
		if ($tags instanceof Garp_Model_Db) {
			$tags = self::getTagsFromModel($tags);
		}
		self::purgeStaticCache($tags);
		self::purgeMemcachedCache($tags);
		self::purgePluginLoaderCache();

		$ini = Zend_Registry::get('config');
		if ($createClusterJob && $ini->app->clusteredHosting) {
			Garp_Cache_Store_Cluster::createJob($tags);
		}
	}

	/**
 	 * Clear the Memcached cache that stores queries and ini files and whatnot.
 	 * @param Array|Garp_Model_Db $modelNames
 	 * @return Void
 	 */
	public static function purgeMemcachedCache($modelNames = array()) {
		if ($modelNames instanceof Garp_Model_Db) {
			$modelNames = self::getTagsFromModel($modelNames);
		}

		if (empty($modelNames)) {
			if (Zend_Registry::isRegistered('CacheFrontend')) {
				$cacheFront = Zend_Registry::get('CacheFrontend'); 
				$cacheFront->clean(Zend_Cache::CLEANING_MODE_ALL);
			}
		} else {
			foreach ($modelNames as $modelName) {
				$model = new $modelName();
				self::_incrementMemcacheVersion($model);
				if ($model->getObserver('Translatable')) {
					// Make sure cache is cleared for all languages.
					$locales = Garp_I18n::getLocales();
					foreach ($locales as $locale) {
						try {
							$modelFactory = new Garp_I18n_ModelFactory($locale);
							$i18nModel = $modelFactory->getModel($model);
							self::_incrementMemcacheVersion($i18nModel);
						} catch (Garp_I18n_ModelFactory_Exception_ModelAlreadyLocalized $e) {
							// all good in the hood  ｡^‿^｡
						}
					}
				}
			}
		}
	}

	/**
 	 * This clears Zend's Static caching.
 	 * @param Array|Garp_Model_Db $modelNames Clear the cache of a specific bunch of models.
 	 * @param String $cacheDir Directory containing the cache files
 	 * @return Void
 	 */
	public static function purgeStaticCache($modelNames = array(), $cacheDir = false) {
		if ($modelNames instanceof Garp_Model_Db) {
			$modelNames = self::getTagsFromModel($modelNames);
		}
	
		$cacheDir = $cacheDir ?: self::_getStaticCacheDir();
		if (!$cacheDir) {
			return;
		}
		$cacheDir = str_replace(' ', '\ ', $cacheDir);
		$cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

		// Destroy all if no model names are given
		if (empty($modelNames)) {
			$allPath = $cacheDir.'*';
{'fraction_non_alphanumeric': 0.130936329588015, 'fraction_numerical': 0.003295880149812734, 'mean_word_length': 3.9451851851851854, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 22, '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': '2621708', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2141, 'n_words': 518}