dir = $dir; } /** * @param string * @param mixed * @return mixed stored value */ public function save($key, $value) { file_put_contents( $this->filePath($key), serialize($value), LOCK_EX ); return $value; } /** * @param string * @return mixed|NULL */ public function load($key) { $path = $this->filePath($key); if (is_file($path) && ($fd = fopen($path, 'rb')) && flock($fd, LOCK_SH)) { $cached = stream_get_contents($fd); flock($fd, LOCK_UN); fclose($fd); $success = TRUE; set_error_handler(function() use (& $success) { return $success = FALSE; }, E_NOTICE); $cached = unserialize($cached); restore_error_handler(); if ($success) { return $cached; } } } /** * @param string * @return string */ private function filePath($key) { return $this->dir . DIRECTORY_SEPARATOR . sha1($key) . '.php'; } }