/** * Test is a cache has a key. * * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the * test and any subsequent action (get, set, delete etc). * Instead it is recommended to write your code in such a way they it performs the following steps: * <ol> * <li>Attempt to retrieve the information.</li> * <li>Generate the information.</li> * <li>Attempt to set the information</li> * </ol> * * Its also worth mentioning that not all stores support key tests. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method. * Just one more reason you should not use these methods unless you have a very good reason to do so. * * @param string|int $key * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or * data source then the code will try load the key value from the next item in the chain. * @return bool True if the cache has the requested key, false otherwise. */ public function has($key, $tryloadifpossible = false) { $parsedkey = $this->parse_key($key); if ($this->is_in_persist_cache($parsedkey)) { return true; } if ($this->has_a_ttl() && !$this->store_supports_native_ttl() || !$this->store_supports_key_awareness()) { if ($this->store_supports_key_awareness() && !$this->store->has($parsedkey)) { return false; } $data = $this->store->get($parsedkey); if (!$this->store_supports_native_ttl()) { $has = $data instanceof cache_ttl_wrapper && !$data->has_expired(); } else { $has = $data !== false; } } else { $has = $this->store->has($parsedkey); } if (!$has && $tryloadifpossible) { if ($this->loader !== false) { $result = $this->loader->get($parsedkey); } else { if ($this->datasource !== null) { $result = $this->datasource->load_for_cache($key); } } $has = $result !== null; if ($has) { $this->set($key, $result); } } return $has; }
/** * Test is a cache has a key. * * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the * test and any subsequent action (get, set, delete etc). * Instead it is recommended to write your code in such a way they it performs the following steps: * <ol> * <li>Attempt to retrieve the information.</li> * <li>Generate the information.</li> * <li>Attempt to set the information</li> * </ol> * * Its also worth mentioning that not all stores support key tests. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method. * Just one more reason you should not use these methods unless you have a very good reason to do so. * * @param string|int $key * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or * data source then the code will try load the key value from the next item in the chain. * @return bool True if the cache has the requested key, false otherwise. */ public function has($key, $tryloadifpossible = false) { $parsedkey = $this->parse_key($key); if ($this->is_in_persist_cache($parsedkey)) { // Hoorah, that was easy. It exists in the static acceleration array so we definitely have it. return true; } if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) { // The data has a TTL and the store doesn't support it natively. // We must fetch the data and expect a ttl wrapper. $data = $this->store->get($parsedkey); $has = $data instanceof cache_ttl_wrapper && !$data->has_expired(); } else { if (!$this->store_supports_key_awareness()) { // The store doesn't support key awareness, get the data and check it manually... puke. // Either no TTL is set of the store supports its handling natively. $data = $this->store->get($parsedkey); $has = $data !== false; } else { // The store supports key awareness, this is easy! // Either no TTL is set of the store supports its handling natively. $has = $this->store->has($parsedkey); } } if (!$has && $tryloadifpossible) { if ($this->loader !== false) { $result = $this->loader->get($parsedkey); } else { if ($this->datasource !== null) { $result = $this->datasource->load_for_cache($key); } } $has = $result !== null; if ($has) { $this->set($key, $result); } } return $has; }