From 09801f11fea503d9424b966e3dd4e096a227eb88 Mon Sep 17 00:00:00 2001
From: Thomas Skovgaard Gielfeldt <thomas@gielfeldt.com>
Date: Sat, 3 Dec 2011 16:56:33 +0100
Subject: [PATCH] Issue #1349694: Add default options to cache_graceful().

---
 cache_graceful.module |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/cache_graceful.module b/cache_graceful.module
index 3b64283..037289f 100755
--- a/cache_graceful.module
+++ b/cache_graceful.module
@@ -178,6 +178,11 @@ function cache_graceful_options($callback = NULL, $options = NULL) {
  * @see cache_get()
  */
 function cache_graceful($key, $args, $table = 'cache', $expire = NULL, $prefetch = NULL, $options = array()) {
+  $options += array(
+    'wait_for_cache' => variable_get('cache_graceful_wait_for_cache', CACHE_GRACEFUL_WAIT_FOR_CACHE),
+    'ensure_result' => TRUE,
+  );
+
   if (empty($expire) && (string)$expire !== '0') {
     $expire = variable_get('cache_graceful_expire', CACHE_GRACEFUL_EXPIRE);
   }
@@ -255,9 +260,10 @@ function cache_graceful($key, $args, $table = 'cache', $expire = NULL, $prefetch
  * @return mixed
  */
 function cache_graceful_execute($key, $args, $table = 'cache', $expire = NULL, $prefetch = NULL, $options = array()) {
-  $wait_for_cache = array_key_exists('wait_for_cache', $options) ? $options['wait_for_cache'] : variable_get('cache_graceful_wait_for_cache', CACHE_GRACEFUL_WAIT_FOR_CACHE);
-  $ensure_result = array_key_exists('ensure_result', $options) ? $options['ensure_result'] : TRUE;
-  $cache = array_key_exists('cache', $options) ? $options['cache'] : NULL;
+  $options += array(
+    'wait_for_cache' => variable_get('cache_graceful_wait_for_cache', CACHE_GRACEFUL_WAIT_FOR_CACHE),
+    'ensure_result' => TRUE,
+  );

   $callback = array_shift($args);
   if (!is_callable($callback)) {
@@ -275,10 +281,10 @@ function cache_graceful_execute($key, $args, $table = 'cache', $expire = NULL, $
     // watchdog('cache_graceful', 'Unlocked', array(), WATCHDOG_DEBUG);
     return $result;
   }
-  elseif ($wait_for_cache) {
+  elseif ($options['wait_for_cache']) {
     // watchdog('cache_graceful', 'Could not obtain lock, waiting for cache', array(), WATCHDOG_DEBUG);
     // Could not get lock, but we should wait for the cache to be updated.
-    $timeout = microtime(TRUE) + $wait_for_cache;
+    $timeout = microtime(TRUE) + $options['wait_for_cache'];
     while (microtime(TRUE) <= $timeout) {
       if (lock_may_be_available($key)) {
         if ($cache = cache_get($key, $table)) {
@@ -301,7 +307,7 @@ function cache_graceful_execute($key, $args, $table = 'cache', $expire = NULL, $
     // watchdog('cache_graceful', 'Could not obtain lock ...', array(), WATCHDOG_DEBUG);
   }

-  if ($ensure_result) {
+  if ($options['ensure_result']) {
     // watchdog('cache_graceful', 'Cache could not be obtained, rendering ...', array(), WATCHDOG_DEBUG);
     cache_graceful_options(_background_process_callback_name($callback), $options);
     $result = call_user_func_array($callback, $args);
--
1.7.0.4
