[CORE] Fixed small anti-pattern on cache code. Plugins had to throw EndCache events, when this should be done by the library

This commit is contained in:
Miguel Dantas 2019-08-13 01:32:54 +01:00 committed by Diogo Peralta Cordeiro
parent 19d68c9f8e
commit 4f8ede8239

View File

@ -105,46 +105,45 @@ class Cache
* a more-or-less fingerprint of the current running code and adds it to * a more-or-less fingerprint of the current running code and adds it to
* the cache key. In the case of an upgrade of core, or addition or * the cache key. In the case of an upgrade of core, or addition or
* removal of plugins, a new unique fingerprint is generated and used. * removal of plugins, a new unique fingerprint is generated and used.
* *
* There can still be problems with a) differences in versions of the * There can still be problems with a) differences in versions of the
* plugins and b) people running code between official versions. This is * plugins and b) people running code between official versions. This is
* usually a problem only for experienced users like developers, who know * usually a problem only for experienced users like developers, who know
* how to clear their cache. * how to clear their cache.
* *
* For sites that run code between versions (like the status.net cloud), * For sites that run code between versions (like the status.net cloud),
* there's an additional build number configuration setting. * there's an additional build number configuration setting.
* *
* @param string $extra the real part of the key * @param string $extra the real part of the key
* *
* @return string full key * @return string full key
*/ */
static function codeKey($extra) static function codeKey($extra)
{ {
static $prefix = null; static $prefix = null;
if (empty($prefix)) { if (empty($prefix)) {
$names = array(); $names = array();
foreach (GNUsocial::getActivePlugins() as $plugin=>$attrs) { foreach (GNUsocial::getActivePlugins() as $plugin=>$attrs) {
$names[] = $plugin; $names[] = $plugin;
} }
asort($names); asort($names);
// Unique enough. // Unique enough.
$uniq = crc32(implode(',', $names)); $uniq = crc32(implode(',', $names));
$build = common_config('site', 'build'); $build = common_config('site', 'build');
$prefix = GNUSOCIAL_VERSION.':'.$build.':'.$uniq; $prefix = GNUSOCIAL_VERSION.':'.$build.':'.$uniq;
} }
return Cache::key($prefix.':'.$extra); return Cache::key($prefix.':'.$extra);
} }
/** /**
* Make a string suitable for use as a key * Make a string suitable for use as a key
* *
@ -175,12 +174,12 @@ class Cache
$value = false; $value = false;
common_perf_counter('Cache::get', $key); common_perf_counter('Cache::get', $key);
if (Event::handle('StartCacheGet', array(&$key, &$value))) { if (Event::handle('StartCacheGet', [&$key, &$value])) {
if ($this->_inlineCache && array_key_exists($key, $this->_items)) { if ($this->_inlineCache && array_key_exists($key, $this->_items)) {
$value = unserialize($this->_items[$key]); $value = unserialize($this->_items[$key]);
} }
Event::handle('EndCacheGet', array($key, &$value));
} }
Event::handle('EndCacheGet', [$key, &$value]);
return $value; return $value;
} }
@ -200,18 +199,13 @@ class Cache
$success = false; $success = false;
common_perf_counter('Cache::set', $key); common_perf_counter('Cache::set', $key);
if (Event::handle('StartCacheSet', array(&$key, &$value, &$flag, if (Event::handle('StartCacheSet', [&$key, &$value, &$flag, &$expiry, &$success])) {
&$expiry, &$success))) {
if ($this->_inlineCache) { if ($this->_inlineCache) {
$this->_items[$key] = serialize($value); $this->_items[$key] = serialize($value);
} }
$success = true; $success = true;
Event::handle('EndCacheSet', array($key, $value, $flag,
$expiry));
} }
Event::handle('EndCacheSet', [$key, $value, $flag, $expiry]);
return $success; return $success;
} }
@ -229,7 +223,7 @@ class Cache
{ {
$value = false; $value = false;
common_perf_counter('Cache::increment', $key); common_perf_counter('Cache::increment', $key);
if (Event::handle('StartCacheIncrement', array(&$key, &$step, &$value))) { if (Event::handle('StartCacheIncrement', [&$key, &$step, &$value])) {
// Fallback is not guaranteed to be atomic, // Fallback is not guaranteed to be atomic,
// and may original expiry value. // and may original expiry value.
$value = $this->get($key); $value = $this->get($key);
@ -238,8 +232,8 @@ class Cache
$ok = $this->set($key, $value); $ok = $this->set($key, $value);
$got = $this->get($key); $got = $this->get($key);
} }
Event::handle('EndCacheIncrement', array($key, $step, $value));
} }
Event::handle('EndCacheIncrement', [$key, $step, $value]);
return $value; return $value;
} }
@ -255,13 +249,13 @@ class Cache
$success = false; $success = false;
common_perf_counter('Cache::delete', $key); common_perf_counter('Cache::delete', $key);
if (Event::handle('StartCacheDelete', array(&$key, &$success))) { if (Event::handle('StartCacheDelete', [&$key, &$success])) {
if ($this->_inlineCache && array_key_exists($key, $this->_items)) { if ($this->_inlineCache && array_key_exists($key, $this->_items)) {
unset($this->_items[$key]); unset($this->_items[$key]);
} }
$success = true; $success = true;
Event::handle('EndCacheDelete', array($key));
} }
Event::handle('EndCacheDelete', [$key]);
return $success; return $success;
} }
@ -276,10 +270,10 @@ class Cache
{ {
$success = false; $success = false;
if (Event::handle('StartCacheReconnect', array(&$success))) { if (Event::handle('StartCacheReconnect', [&$success])) {
$success = true; $success = true;
Event::handle('EndCacheReconnect', array());
} }
Event::handle('EndCacheReconnect', []);
return $success; return $success;
} }