Index: branches/5.2.x/core/kernel/managers/cache_manager.php =================================================================== diff -u -N -r15137 -r15226 --- branches/5.2.x/core/kernel/managers/cache_manager.php (.../cache_manager.php) (revision 15137) +++ branches/5.2.x/core/kernel/managers/cache_manager.php (.../cache_manager.php) (revision 15226) @@ -1,6 +1,6 @@ Application->isCachingType(CACHING_TYPE_MEMORY) ) { - $this->Application->rebuildCache('master:configs_parsed', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime); - } - else { - $this->Application->rebuildDBCache('configs_parsed', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime); - } - return false; } @@ -402,7 +395,7 @@ $this->Application->rebuildCache('master:configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime); } else { - $this->Application->rebuildDBCache('configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime); + $this->rebuildDBCache('configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime); } } @@ -418,7 +411,7 @@ $this->Application->rebuildCache('master:sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime); } else { - $this->Application->rebuildDBCache('sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime); + $this->rebuildDBCache('sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime); } } @@ -502,16 +495,6 @@ } /** - * Prints caching statistics - * - * @access public - */ - public function printStatistics() - { - $this->cacheHandler->printStatistics(); - } - - /** * Returns cached $key value from cache named $cache_name * * @param int $key key name from cache @@ -526,7 +509,7 @@ } /** - * Adds new value to cache $cache_name and identified by key $key + * Stores new $value in cache with $key name * * @param int $key key name to add to cache * @param mixed $value value of cached record @@ -540,17 +523,31 @@ } /** + * Stores new $value in cache with $key name (only if not there already) + * + * @param int $key key name to add to cache + * @param mixed $value value of cached record + * @param int $expiration when value expires (0 - doesn't expire) + * @return bool + * @access public + */ + public function addCache($key, $value, $expiration = 0) + { + return $this->cacheHandler->addCache($key, $value, $expiration); + } + + /** * Sets rebuilding mode for given cache * * @param string $name * @param int $mode * @param int $max_rebuilding_time - * @return void + * @return bool * @access public */ public function rebuildCache($name, $mode = null, $max_rebuilding_time = 0) { - $this->cacheHandler->rebuildCache($name, $mode, $max_rebuilding_time); + return $this->cacheHandler->rebuildCache($name, $mode, $max_rebuilding_time); } /** @@ -587,6 +584,7 @@ public function getDBCache($name, $max_rebuild_seconds = 0) { // no serials in cache key OR cache is outdated + $rebuilding = false; $wait_seconds = $max_rebuild_seconds; while (true) { @@ -596,25 +594,33 @@ // cache rebuild requested -> rebuild now $this->deleteDBCache($name . '_rebuild'); - return false; + if ( $this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M1]') ) { + return false; + } } $cache = $cached_data[$name]; $rebuilding = $cached_data[$name . '_rebuilding']; if ( ($cache === false) && (!$rebuilding || $wait_seconds == 0) ) { // cache missing and nobody rebuilding it -> rebuild; enough waiting for cache to be ready + $this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M2' . ($rebuilding ? 'R' : '!R') . ',WS=' . $wait_seconds . ']'); + return false; } elseif ( $cache !== false ) { // cache present -> return it + $this->cacheHandler->storeStatistics($name, $rebuilding ? 'h' : 'H'); + return $cache; } $wait_seconds -= kCache::WAIT_STEP; sleep(kCache::WAIT_STEP); } + $this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M3' . ($rebuilding ? 'R' : '!R') . ',WS=' . $wait_seconds . ']'); + return false; } @@ -673,6 +679,8 @@ */ public function setDBCache($name, $value, $expiration = false) { + $this->cacheHandler->storeStatistics($name, 'WU'); + $this->deleteDBCache($name . '_rebuilding'); $this->_setDBCache($name, $value, $expiration); } @@ -683,11 +691,13 @@ * @param string $name * @param mixed $value * @param int|bool $expiration + * @param string $insert_type + * @return bool * @access protected */ - protected function _setDBCache($name, $value, $expiration = false) + protected function _setDBCache($name, $value, $expiration = false, $insert_type = 'REPLACE') { - if ((int)$expiration <= 0) { + if ( (int)$expiration <= 0 ) { $expiration = -1; } @@ -699,28 +709,58 @@ ); $this->Conn->nextQueryCachable = true; - $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'SystemCache', 'REPLACE'); + + return $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'SystemCache', $insert_type); } /** + * Sets value to database cache + * + * @param string $name + * @param mixed $value + * @param int|bool $expiration + * @return bool + * @access protected + */ + protected function _addDBCache($name, $value, $expiration = false) + { + return $this->_setDBCache($name, $value, $expiration, 'INSERT'); + } + + /** * Sets rebuilding mode for given cache * * @param string $name * @param int $mode * @param int $max_rebuilding_time - * @return void + * @param string $miss_type + * @return bool * @access public */ - public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0) + public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0, $miss_type = 'M') { if ( !isset($mode) || $mode == kCache::REBUILD_NOW ) { - $this->_setDBCache($name . '_rebuilding', 1, $max_rebuilding_time); + $this->cacheHandler->storeStatistics($name, $miss_type); + + if ( !$max_rebuilding_time ) { + return true; + } + + if ( !$this->_addDBCache($name . '_rebuilding', 1, $max_rebuilding_time) ) { + $this->cacheHandler->storeStatistics($name, 'l'); + + return false; + } + $this->deleteDBCache($name . '_rebuild'); + $this->cacheHandler->storeStatistics($name, 'L'); } elseif ( $mode == kCache::REBUILD_LATER ) { $this->_setDBCache($name . '_rebuild', 1, 0); $this->deleteDBCache($name . '_rebuilding'); } + + return true; } /**