prefix = $name; } public function store($key, $value) { $cacheDir = CacheStorageFile::getCacheDir($key); $cacheFile = CacheStorageFile::getCacheFile($key); if (! is_dir($cacheDir)) { if (! @mkdir($cacheDir, 0755, true)) { throw new CacheException("Could not create cache directory"); } } return file_put_contents($cacheFile, $value); } public function fetch($key) { $cacheFile = CacheStorageFile::getCacheFile($key); if (File::exists($cacheFile) && File::readable($cacheFile)) { return @file_get_contents($cacheFile); } return false; } public function delete($key) { $cacheFile = CacheStorageFile::getCacheFile($key); if (! @unlink($cacheFile)) { throw new CacheException("Cache file could not be deleted"); } } public function isLocked($key) { // our lock file convention is simple: /the/file/path.lock clearstatcache(); return file_exists($key . '.lock'); } public function lock($key) { $cacheDir = CacheStorageFile::getCacheDir($key); $cacheFile = CacheStorageFile::getCacheFile($key); if (! is_dir($cacheDir)) { if (! @mkdir($cacheDir, 0755, true)) { // make sure the failure isn't because of a concurency issue if (! is_dir($cacheDir)) { throw new CacheException("Could not create cache directory"); } } } @touch($cacheFile . '.lock'); } public function unlock($key) { // suppress all warnings, if some other process removed it that's ok too $cacheFile = CacheStorageFile::getCacheFile($key); @unlink($cacheFile . '.lock'); } private function getCacheDir($key) { // use the first 2 characters of the hash as a directory prefix // this should prevent slowdowns due to huge directory listings // and thus give some basic amount of scalability return Config::get('cache_root') . '/' . $this->prefix . '/' . substr($key, 0, 2); } private function getCacheFile($key) { return $this->getCacheDir($key) . '/' . $key; } }