From 6f599b02370185ebb2ba866a6ad78a03f3954293 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 12 Sep 2017 12:39:23 -0400
Subject: [PATCH] Added channel home page caching.

---
 config/vufind/channels.ini                     |  3 +++
 .../VuFind/Controller/ChannelsController.php   | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/config/vufind/channels.ini b/config/vufind/channels.ini
index ec6ffc605fb..f6af7eef645 100644
--- a/config/vufind/channels.ini
+++ b/config/vufind/channels.ini
@@ -6,6 +6,9 @@
 ; this section controls which source will be used:
 default_home_source = "Solr"
 
+; Should we cache channel results on the Channels/Home screen?
+cache_home_channels = true
+
 ; This section controls which providers are used for Solr searches/records.
 ; Providers may be followed by a colon and the name of a configuration section
 ; to use. If no configuration section is provided, the default of
diff --git a/module/VuFind/src/VuFind/Controller/ChannelsController.php b/module/VuFind/src/VuFind/Controller/ChannelsController.php
index 420522eee14..44ac183261d 100644
--- a/module/VuFind/src/VuFind/Controller/ChannelsController.php
+++ b/module/VuFind/src/VuFind/Controller/ChannelsController.php
@@ -85,7 +85,23 @@ class ChannelsController extends AbstractBase
         $providers = $this->getChannelProviderArray($providerIds, $config);
 
         $token = $this->params()->fromQuery('channelToken');
-        $channels = $this->getHomeChannels($providers, $searchClassId, $token);
+        if (isset($config->General->cache_home_channels)
+            && $config->General->cache_home_channels
+        ) {
+            $parts = implode('-', [implode(',', $providerIds), $searchClassId, $token]);
+            $cacheKey = 'homeChannels-' . md5($parts);
+            $cache = $this->serviceLocator->get('VuFind\CacheManager')
+                ->getCache('object');
+        } else {
+            $cacheKey = false;
+        }
+        $channels = $cacheKey ? $cache->getItem($cacheKey) : false;
+        if (!$channels) {
+            $channels = $this->getHomeChannels($providers, $searchClassId, $token);
+            if ($cacheKey) {
+                $cache->setItem($cacheKey, $channels);
+            }
+        }
         return $this->createViewModel(compact('token', 'channels'));
     }
 
-- 
GitLab