From 935be946821aa86bd22516aaed0962ce5ae72b40 Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Fri, 1 Dec 2017 15:10:06 -0600
Subject: [PATCH] Issue #2927945 by markcarver: _bootstrap_file_scan_directory
 unnecessarily calls the DB every time

---
 includes/common.inc | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 3835f01..1df30a4 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -559,20 +559,25 @@ function _bootstrap_get_base_themes($theme_key = NULL, $include_theme_key = FALS
  * @see file_scan_directory()
  */
 function _bootstrap_file_scan_directory($dir, $mask, array $options = array()) {
-  // Retrieve cached data.
-  $cid = 'theme_registry:bootstrap:files';
-  $files = array();
-  if ($cache = cache_get($cid)) {
-    $files = $cache->data;
-  }
-  // Generate a unique hash for all parameters passed as a change in any of
-  // them would return different results.
-  $hash = drupal_hash_base64(serialize(func_get_args()));
-  if (!isset($files[$hash])) {
-    $files[$hash] = file_scan_directory($dir, $mask, $options);
-    cache_set($cid, $files);
+  static $files = [];
+
+  // Generate a unique cache identifier for all parameters passed as a change
+  // in any of them would return different results.
+  $cid = 'theme_registry:bootstrap:files:' . drupal_hash_base64(serialize(func_get_args()));
+
+  // Load from DB cache or scan filesystem if files are not statically cached.
+  if (!isset($files[$cid])) {
+    if (($cache = cache_get($cid)) && isset($cache->data)) {
+      $files = $cache->data;
+    }
+    else {
+      $files = file_scan_directory($dir, $mask, $options);
+      cache_set($cid, $files);
+    }
+    $files[$cid] = $files;
   }
-  return $files[$hash];
+
+  return $files[$cid];
 }
 
 /**
-- 
2.14.1

