From a03490f5f16191bd7fc31bf8e2abddd7733ee041 Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Thu, 30 Aug 2018 21:56:45 -0400 Subject: [PATCH] infrastructure/ops#842 Fix Getting Started and Blog dashlets caching. --- CRM/Dashlet/Page/Blog.php | 27 ++++++++++--------------- CRM/Dashlet/Page/GettingStarted.php | 31 ++++++++++------------------- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/CRM/Dashlet/Page/Blog.php b/CRM/Dashlet/Page/Blog.php index 23cac0abf7..3416d18660 100644 --- a/CRM/Dashlet/Page/Blog.php +++ b/CRM/Dashlet/Page/Blog.php @@ -73,27 +73,21 @@ class CRM_Dashlet_Page_Blog extends CRM_Core_Page { * @return array */ protected function getData() { - // Fetch data from cache - $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache - WHERE group_name = 'dashboard' AND path = 'newsfeed'"); - if ($cache->fetch()) { - $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS); - // Refresh data after CACHE_DAYS - if (strtotime($cache->created_date) < $expire) { - $new_data = $this->getFeeds(); - // If fetching the new rss feed was successful, return it - // Otherwise use the old cached data - it's better than nothing - if ($new_data) { - return $new_data; - } + $value = Civi::cache('community_messages')->get('dashboard_newsfeed'); + + if (!$value) { + $value = $this->getFeeds(); + + if ($value) { + Civi::cache('community_messages')->set('dashboard_newsfeed', $value, (60 * 60 * 24 * self::CACHE_DAYS)); } - return unserialize($cache->data); } - return $this->getFeeds(); + + return $value; } /** - * Fetch all feeds & cache results. + * Fetch all feeds. * * @return array */ @@ -104,7 +98,6 @@ class CRM_Dashlet_Page_Blog extends CRM_Core_Page { return array(); } $feeds = $this->formatItems($newsFeed); - CRM_Core_BAO_Cache::setItem($feeds, 'dashboard', 'newsfeed'); return $feeds; } diff --git a/CRM/Dashlet/Page/GettingStarted.php b/CRM/Dashlet/Page/GettingStarted.php index cb77b45cff..18e3e89d3c 100644 --- a/CRM/Dashlet/Page/GettingStarted.php +++ b/CRM/Dashlet/Page/GettingStarted.php @@ -87,27 +87,21 @@ class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page { * @return array */ private function _gettingStarted() { - // Fetch data from cache - $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache - WHERE group_name = 'dashboard' AND path = 'gettingStarted'"); - if ($cache->fetch()) { - $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS); - // Refresh data after CACHE_DAYS - if (strtotime($cache->created_date) < $expire) { - $new_data = $this->_getHtml($this->gettingStartedUrl()); - // If fetching the new html was successful, return it - // Otherwise use the old cached data - it's better than nothing - if ($new_data) { - return $new_data; - } + $value = Civi::cache('community_messages')->get('dashboard_gettingStarted'); + + if (!$value) { + $value = $this->_getHtml($this->gettingStartedUrl()); + + if ($value) { + Civi::cache('community_messages')->set('dashboard_gettingStarted', $value, (60 * 60 * 24 * self::CACHE_DAYS)); } - return unserialize($cache->data); } - return $this->_getHtml($this->gettingStartedUrl()); + + return $value; } /** - * Get html and cache results. + * Get html. * * @param $url * @@ -115,18 +109,15 @@ class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page { * array of gettingStarted items; or NULL if not available */ public function _getHtml($url) { - $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); list ($status, $html) = $httpClient->get($url); + if ($status !== CRM_Utils_HttpClient::STATUS_OK) { return NULL; } $tokensList = CRM_Utils_Token::getTokens($html); $this->replaceLinkToken($tokensList, $html); - if ($html) { - CRM_Core_BAO_Cache::setItem($html, 'dashboard', 'gettingStarted'); - } return $html; } -- 2.25.1