commit 08b1f5ae4e1a15c2beb2bc786b720174492bf9cf
Author: fago <nuppla@zites.net>
Date:   Tue Jun 4 19:53:01 2013 +0200

    Fixed caching

diff --git a/includes/rules.core.inc b/includes/rules.core.inc
index 53904f2..3acbbbd 100644
--- a/includes/rules.core.inc
+++ b/includes/rules.core.inc
@@ -1051,7 +1051,7 @@ abstract class RulesPlugin extends RulesExtendable {
         elseif ($this->plugin == 'reaction rule') {
           // Clear event sets cached for evaluation.
           cache_clear_all('event_', 'cache_rules', TRUE);
-          variable_del('rules_empty_sets');
+          variable_del('rules_event_whitelist');
         }
         drupal_static_reset('rules_get_cache');
         drupal_static_reset('rules_config_update_dirty_flag');
diff --git a/includes/rules.plugins.inc b/includes/rules.plugins.inc
index cac9c60..29778ec 100644
--- a/includes/rules.plugins.inc
+++ b/includes/rules.plugins.inc
@@ -801,10 +801,9 @@ class RulesEventSet extends RulesRuleSet {
       drupal_alter('rules_event_set', $event_name, $set);
       rules_set_cache('event_' . $event_name, $set);
     }
-    // Cache a list of empty sets so we can use it to speed up later calls.
-    // See rules_get_event_set().
-    $empty_events = array_keys(array_diff_key($events, $sets));
-    variable_set('rules_empty_sets', array_flip($empty_events));
+    // Cache a whitelist of configured events so we can use it to speed up later
+    // calls. See rules_invoke_event().
+    variable_set('rules_event_whitelist', array_flip(array_keys($sets)));
   }
 
   protected function stateVariables($element = NULL) {
diff --git a/rules.install b/rules.install
index f49c78e..c27dd68 100644
--- a/rules.install
+++ b/rules.install
@@ -18,7 +18,7 @@ function rules_install() {
  * Implements hook_uninstall().
  */
 function rules_uninstall() {
-  variable_del('rules_empty_sets');
+  variable_del('rules_event_whitelist');
   variable_del('rules_debug');
 }
 
@@ -440,3 +440,10 @@ function rules_update_7209() {
     'description' => 'Whether to use a permission to control access for using components.',
   ));
 }
+
+/**
+ * Deletes the unused rules_empty_sets variable.
+ */
+function rules_update_7210() {
+  variable_del('rules_empty_sets');
+}
diff --git a/rules.module b/rules.module
index f8ef50f..9c78f56 100644
--- a/rules.module
+++ b/rules.module
@@ -344,7 +335,6 @@ function rules_set_cache($cid, $data) {
  * Implements hook_flush_caches().
  */
 function rules_flush_caches() {
-  variable_del('rules_empty_sets');
   return array('cache_rules');
 }
 
@@ -353,7 +343,7 @@ function rules_flush_caches() {
  */
 function rules_clear_cache() {
   cache_clear_all('*', 'cache_rules', TRUE);
-  variable_del('rules_empty_sets');
+  variable_del('rules_event_whitelist');
   drupal_static_reset('rules_get_cache');
   drupal_static_reset('rules_fetch_data');
   drupal_static_reset('rules_config_update_dirty_flag');
@@ -895,9 +885,11 @@ function rules_invoke_event() {
   $args = func_get_args();
   $event_name = $args[0];
   unset($args[0]);
-  // For invoking the rules event we directly acccess the global $conf. This is
-  // fast without having to introduce another static cache.
-  if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
+  // We maintain a whitelist of configured events to reduces the number of cache
+  // reads. We access it directly via the global $conf as this is fast without
+  // having to introduce another static cache. Then, if the whitelist is unset,
+  // we ignore it so cache rebuilding is triggered.
+  if (!defined('MAINTENANCE_MODE') && (!isset($conf['rules_event_whitelist']) || isset($conf['rules_event_whitelist'][$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
     $event->executeByArgs($args);
   }
 }
@@ -922,9 +914,11 @@ function rules_invoke_event() {
 function rules_invoke_event_by_args($event_name, $args = array()) {
   global $conf;
 
-  // For invoking the rules event we directly acccess the global $conf. This is
-  // fast without having to introduce another static cache.
-  if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
+  // We maintain a whitelist of configured events to reduces the number of cache
+  // reads. We access it directly via the global $conf as this is fast without
+  // having to introduce another static cache. Then, if the whitelist is unset,
+  // we ignore it so cache rebuilding is triggered.
+  if (!defined('MAINTENANCE_MODE') && (!isset($conf['rules_event_whitelist']) || isset($conf['rules_event_whitelist'][$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
     $event->executeByArgs($args);
   }
 }
