diff --git a/classes/rule.php b/classes/rule.php index d4f4466..b62469b 100644 --- a/classes/rule.php +++ b/classes/rule.php @@ -189,5 +189,6 @@ protected function after_create() { */ protected function after_update($result) { cache_helper::purge_by_event('ruleschanged'); + cache::make('tool_dynamic_cohorts', 'matchinguserscount')->delete($this->get('id')); } } diff --git a/classes/rule_manager.php b/classes/rule_manager.php index 470d487..49e751f 100644 --- a/classes/rule_manager.php +++ b/classes/rule_manager.php @@ -225,9 +225,11 @@ public static function delete_rule(rule $rule): void { public static function get_matching_users(rule $rule, ?int $userid = null): array { global $DB; - $conditions = $rule->get_condition_records(); + $matchinguserscache = cache::make('tool_dynamic_cohorts', 'matchinguserscount'); + $conditions = $rule->get_condition_records(); if (empty($conditions)) { + $matchinguserscache->set($rule->get('id'), 0); return []; } @@ -238,15 +240,21 @@ public static function get_matching_users(rule $rule, ?int $userid = null): arra } catch (\Exception $exception ) { self::trigger_matching_failed_event($rule, $exception->getMessage()); $rule->mark_broken(); + $matchinguserscache->set($rule->get('id'), 0); return []; } try { - return $DB->get_records_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params()); + $users = $DB->get_records_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params()); + if (empty($userid)) { + $matchinguserscache->set($rule->get('id'), count($users)); + } + return $users; } catch (\Exception $exception) { self::trigger_matching_failed_event($rule, $exception->getMessage()); $rule->mark_broken(); + $matchinguserscache->set($rule->get('id'), 0); return []; } @@ -256,37 +264,48 @@ public static function get_matching_users(rule $rule, ?int $userid = null): arra * Get count of matching users for a given rule. * * @param \tool_dynamic_cohorts\rule $rule - * @param int|null $userid * * @return int */ - public static function get_matching_users_count(rule $rule, ?int $userid = null): int { + public static function get_matching_users_count(rule $rule): int { global $DB; + $matchinguserscache = cache::make('tool_dynamic_cohorts', 'matchinguserscount'); + + $count = $matchinguserscache->get($rule->get('id')); + if ($count !== false) { + return $count; + } + $conditions = $rule->get_condition_records(); if (empty($conditions)) { + $matchinguserscache->set($rule->get('id'), 0); return 0; } $sql = "SELECT COUNT(DISTINCT u.id) cnt FROM {user} u"; try { - $sqldata = condition_manager::build_sql_data($conditions, $rule->get('operator'), $userid); + $sqldata = condition_manager::build_sql_data($conditions, $rule->get('operator')); } catch (\Exception $exception ) { self::trigger_matching_failed_event($rule, $exception->getMessage()); $rule->mark_broken(); + $matchinguserscache->set($rule->get('id'), 0); return 0; } try { $result = $DB->get_record_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params()); + $matchinguserscache->set($rule->get('id'), $result->cnt); + return $result->cnt; } catch (\Exception $exception) { self::trigger_matching_failed_event($rule, $exception->getMessage()); $rule->mark_broken(); + $matchinguserscache->set($rule->get('id'), 0); return 0; } } diff --git a/db/caches.php b/db/caches.php index 0ae7751..bc76272 100644 --- a/db/caches.php +++ b/db/caches.php @@ -44,4 +44,9 @@ 'conditionschanged', ], ], + 'matchinguserscount' => [ + 'mode' => cache_store::MODE_APPLICATION, + 'simpledata' => true, + 'staticacceleration' => true, + ], ]; diff --git a/lang/en/tool_dynamic_cohorts.php b/lang/en/tool_dynamic_cohorts.php index 46abb47..2fb13a6 100644 --- a/lang/en/tool_dynamic_cohorts.php +++ b/lang/en/tool_dynamic_cohorts.php @@ -36,6 +36,7 @@ $string['bulkprocessing'] = 'Bulk processing'; $string['bulkprocessing_help'] = 'If this option is enabled, users will be added and removed from cohort in bulk. This will significantly improve processing performance. However, using this option will suppress triggering events when users added or removed from cohort.'; $string['cachedef_conditionrecords'] = 'Conditions for a rule'; +$string['cachedef_matchingusers'] = 'Matching users for a rule'; $string['cachedef_rulesconditions'] = 'Rules with a specific condition'; $string['cohort'] = 'Cohort'; $string['cohortid'] = 'Cohort'; diff --git a/version.php b/version.php index 1865255..6a90a85 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'tool_dynamic_cohorts'; $plugin->release = 2024112100; -$plugin->version = 2024112101; +$plugin->version = 2024112102; $plugin->requires = 2022112800; $plugin->supported = [404, 405]; $plugin->maturity = MATURITY_STABLE;