From 10cac9513825500d176496403c3e8804cd8a38c4 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 30 Aug 2019 19:45:28 -0400 Subject: [PATCH] Clear static DAO cache when writing to DAO --- CRM/Core/DAO.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 8693a2e4ea..83dac3f433 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -563,6 +563,7 @@ class CRM_Core_DAO extends DB_DataObject { $event = new \Civi\Core\DAO\Event\PostUpdate($this); \Civi::service('dispatcher')->dispatch("civi.dao.postUpdate", $event); } + $this->clearDbColumnValueCache(); } else { $this->insert(); @@ -616,6 +617,8 @@ class CRM_Core_DAO extends DB_DataObject { \Civi::service('dispatcher')->dispatch("civi.dao.postDelete", $event); $this->free(); + $this->clearDbColumnValueCache(); + return $result; } @@ -1192,12 +1195,16 @@ FROM civicrm_domain CRM_Core_Error::fatal(); } - $cacheKey = "{$daoName}:{$searchValue}:{$returnColumn}:{$searchColumn}"; - if (self::$_dbColumnValueCache === NULL) { - self::$_dbColumnValueCache = []; + self::$_dbColumnValueCache = self::$_dbColumnValueCache ?? []; + + while (strpos($daoName, '_BAO_') !== FALSE) { + $daoName = get_parent_class($daoName); } - if (!array_key_exists($cacheKey, self::$_dbColumnValueCache) || $force) { + if ($force || + empty(self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) || + !array_key_exists($returnColumn, self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) + ) { $object = new $daoName(); $object->$searchColumn = $searchValue; $object->selectAdd(); @@ -1207,11 +1214,10 @@ FROM civicrm_domain if ($object->find(TRUE)) { $result = $object->$returnColumn; } - $object->free(); - self::$_dbColumnValueCache[$cacheKey] = $result; + self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn] = $result; } - return self::$_dbColumnValueCache[$cacheKey]; + return self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn]; } /** @@ -2950,4 +2956,20 @@ SELECT contact_id return $fields; } + /** + * Remove item from static cache during update/delete operations + */ + private function clearDbColumnValueCache() { + $daoName = get_class($this); + while (strpos($daoName, '_BAO_') !== FALSE) { + $daoName = get_parent_class($daoName); + } + if (isset($this->id)) { + unset(self::$_dbColumnValueCache[$daoName]['id'][$this->id]); + } + if (isset($this->name)) { + unset(self::$_dbColumnValueCache[$daoName]['name'][$this->name]); + } + } + } -- 2.25.1