/** * This function will compress data for storage in `tbl_cache`. * It is left to the user to define a unique hash for this data so that it can be * retrieved in the future. Optionally, a `$ttl` parameter can * be passed for this data. If this is omitted, it data is considered to be valid * forever. This function utilizes the Mutex class to act as a crude locking * mechanism. * * @see toolkit.Mutex * @throws DatabaseException * @param string $hash * The hash of the Cached object, as defined by the user * @param string $data * The data to be cached, this will be compressed prior to saving. * @param integer $ttl * A integer representing how long the data should be valid for in minutes. * By default this is null, meaning the data is valid forever * @param string $namespace * The namespace allows data to be grouped and saved so it can be * retrieved later. * @return boolean * If an error occurs, this function will return false otherwise true */ public function write($hash, $data, $ttl = null, $namespace = null) { if (!Mutex::acquire($hash, 2, TMP)) { return false; } $creation = time(); $expiry = null; $ttl = intval($ttl); if ($ttl > 0) { $expiry = $creation + $ttl * 60; } if (!($data = Cacheable::compressData($data))) { return false; } $this->delete($hash, $namespace); $this->Database->insert(array('hash' => $hash, 'creation' => $creation, 'expiry' => $expiry, 'data' => $data, 'namespace' => $namespace), 'tbl_cache'); Mutex::release($hash, TMP); return true; }