captcha_points_cache-7.x-1.3.patch 1.77 KB
diff --git a/captcha.inc b/captcha.inc
index 934d72f..8b42fc0 100644
--- a/captcha.inc
+++ b/captcha.inc
@@ -64,6 +64,7 @@ function captcha_set_form_id_setting($form_id, $captcha_type) {
       'warning'
     );
   }
+  cache_clear_all('captcha_points:' . $form_id,'cache_captcha');
 }
 
 /**
@@ -82,16 +83,19 @@ function captcha_set_form_id_setting($form_id, $captcha_type) {
  *   or in the form 'captcha/Math'.
  */
 function captcha_get_form_id_setting($form_id, $symbolic = FALSE) {
-  // Fetch setting from database.
-  if (module_exists('ctools')) {
-    ctools_include('export');
-    $object = ctools_export_load_object('captcha_points', 'names', array($form_id));
-    $captcha_point = array_pop($object);
-  }
-  else {
-    $result = db_query("SELECT module, captcha_type FROM {captcha_points} WHERE form_id = :form_id",
-      array(':form_id' =>  $form_id));
-    $captcha_point = $result->fetchObject();
+
+  $cache_key = 'captcha_points:' . $form_id;
+
+  $cache = cache_get($cache_key,'cache_captcha');
+  $captcha_point = false;
+  if($cache && !empty($cache->data)){
+    $captcha_point = $cache->data;
+  }else{
+    $captcha_points = captcha_get_captcha_points();
+    if(isset($captcha_points[$form_id])){
+      $captcha_point = $captcha_points[$form_id];
+      cache_set($cache_key,$captcha_point,'cache_captcha',CACHE_PERMANENT);
+    }
   }
 
   // If no setting is available in database for the given form,
@@ -137,7 +141,7 @@ function captcha_get_captcha_points() {
     $captcha_points = array();
     $result = db_select('captcha_points', 'cp')->fields('cp')->orderBy('form_id')->execute();
     foreach ($result as $captcha_point) {
-      $captcha_points[] = $captcha_point;
+      $captcha_points[$captcha_point->form_id] = $captcha_point;
     }
   }
   return $captcha_points;