infrastructure/ops#842 Fix Getting Started and Blog dashlets caching.
authorMathieu Lutfy <mathieu@bidon.ca>
Fri, 31 Aug 2018 01:56:45 +0000 (21:56 -0400)
committerMathieu Lutfy <mathieu@bidon.ca>
Fri, 31 Aug 2018 13:00:39 +0000 (09:00 -0400)
CRM/Dashlet/Page/Blog.php
CRM/Dashlet/Page/GettingStarted.php

index 23cac0abf75e22fa22ed34297bc6a2e852830511..3416d186601f4d61ca4d5d4279611e0e96e22d3f 100644 (file)
@@ -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;
   }
 
index cb77b45cff34a5145574a19f26538404e58826d4..18e3e89d3c833e94f9114d27481d63f89d980666 100644 (file)
@@ -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;
   }