Statusdat: Set default cachepath to '/tmp'; disable caching when path is not writable

This commit is contained in:
Eric Lippmann 2013-10-28 12:46:19 +01:00
parent c9db103384
commit 2387380399

View File

@ -29,6 +29,7 @@
namespace Icinga\Protocol\Statusdat; namespace Icinga\Protocol\Statusdat;
use Icinga\Application\Logger;
use Icinga\Data\DatasourceInterface; use Icinga\Data\DatasourceInterface;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception; use Icinga\Exception;
@ -48,9 +49,8 @@ class Reader implements IReader, DatasourceInterface
/** /**
* The folder for the statusdat cache * The folder for the statusdat cache
*
*/ */
const STATUSDAT_DEFAULT_CACHE_PATH = "/cache"; const STATUSDAT_DEFAULT_CACHE_PATH = '/tmp';
/** /**
* The last state from the cache * The last state from the cache
@ -145,22 +145,26 @@ class Reader implements IReader, DatasourceInterface
*/ */
private function initializeCaches() private function initializeCaches()
{ {
$defaultCachePath = "/tmp" . self::STATUSDAT_DEFAULT_CACHE_PATH; $defaultCachePath = self::STATUSDAT_DEFAULT_CACHE_PATH;
$cachePath = $this->config->get('cache_path', $defaultCachePath);
$cachePath = $this->config->get('cache_path', $defaultCachePath); $maxCacheLifetime = intval($this->config->get('cache_path', self::DEFAULT_CACHE_LIFETIME));
$maxCacheLifetime = intval($this->config->get('cache_path', self::DEFAULT_CACHE_LIFETIME)); $cachingEnabled = true;
if (!is_writeable($cachePath)) { if (!is_writeable($cachePath)) {
throw new ConfigurationError( Logger::warn(
"Cache path $cachePath is not writable, check your configuration" 'Can\'t cache Status.dat backend; make sure cachepath %s is writable by the web user. '
. 'Caching is now disabled',
$cachePath
); );
$cachePath = null;
} }
$backendOptions = array( $backendOptions = array(
'cache_dir' => $cachePath 'cache_dir' => $cachePath
); );
// the object cache might exist for months and is still valid // the object cache might exist for months and is still valid
$this->objectCache = $this->initCache($this->config->object_file, $backendOptions, null); $this->objectCache = $this->initCache($this->config->object_file, $backendOptions, null, $cachingEnabled);
$this->statusCache = $this->initCache($this->config->status_file, $backendOptions, $maxCacheLifetime); $this->statusCache = $this->initCache(
$this->config->status_file, $backendOptions, $maxCacheLifetime, $cachingEnabled
);
} }
@ -173,14 +177,14 @@ class Reader implements IReader, DatasourceInterface
* *
* @return \Zend_Cache_Core|\Zend_Cache_Frontend * @return \Zend_Cache_Core|\Zend_Cache_Frontend
*/ */
private function initCache($file, $backend, $lifetime) private function initCache($file, $backendConfig, $lifetime)
{ {
$frontendOptions = array( $frontendOptions = array(
'lifetime' => $lifetime, 'lifetime' => $lifetime,
'automatic_serialization' => true, 'automatic_serialization' => true,
'master_files' => array($file) 'master_files' => array($file)
); );
return \Zend_Cache::factory('Core', 'File', $frontendOptions, $backend); return \Zend_Cache::factory('Core', 'File', $frontendOptions, $backendConfig);
} }
/** /**