From add90c92bd4a03ebda34122941ab1e3f64468294 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 | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 3835f01..d200052 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -559,20 +559,24 @@ 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);
+  $files = &drupal_static(__FUNCTION__, array());
+
+  // 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[$cid] = $cache->data;
+    }
+    else {
+      $files[$cid] = file_scan_directory($dir, $mask, $options);
+      cache_set($cid, $files[$cid]);
+    }
   }
-  return $files[$hash];
+
+  return $files[$cid];
 }
 
 /**
-- 
2.14.1

