Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #130: cache number of matching users #131

Draft
wants to merge 1 commit into
base: MOODLE_404_STABLE
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions classes/rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
29 changes: 24 additions & 5 deletions classes/rule_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand All @@ -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 [];
}
Expand All @@ -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;
}
}
Expand Down
5 changes: 5 additions & 0 deletions db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@
'conditionschanged',
],
],
'matchinguserscount' => [
'mode' => cache_store::MODE_APPLICATION,
'simpledata' => true,
'staticacceleration' => true,
],
];
1 change: 1 addition & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading