private function purgeLocation($locationId)
{
$fs = $this->getFilesystem();
$locationCacheDir = $this->getLocationCacheDir($locationId);
if ($fs->exists($locationCacheDir)) {
// 1. Copy cache files to stale cache dir
// 2. Place a lock file indicating to use the stale cache
// 3. Remove real cache dir
// 4. Remove lock file
// 5. Remove stale cache dir
// Note that there is no need to remove the meta-file
$staleCacheDir = str_replace(static::LOCATION_CACHE_DIR, static::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
$fs->mkdir($staleCacheDir);
$fs->mirror($locationCacheDir, $staleCacheDir);
$lockFile = $this->getLocationCacheLockName($locationId);
file_put_contents($lockFile, getmypid());
try {
// array of removal is in reverse order on purpose since remove() starts from the end.
$fs->remove(array($staleCacheDir, $lockFile, $locationCacheDir));
return true;
} catch (IOException $e) {
// Log the error in the standard error log and at least try to remove the lock file
error_log($e->getMessage());
@unlink($lockFile);
return false;
}
}
return false;
}