Skip to content

Commit 055d673

Browse files
committed
issue #130: add caching of the list of matching users
1 parent b7f4845 commit 055d673

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

classes/rule.php

+1
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,6 @@ protected function after_create() {
189189
*/
190190
protected function after_update($result) {
191191
cache_helper::purge_by_event('ruleschanged');
192+
cache::make('tool_dynamic_cohorts', 'matchinguserscount')->delete($this->get('id'));
192193
}
193194
}

classes/rule_manager.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ public static function delete_rule(rule $rule): void {
225225
public static function get_matching_users(rule $rule, ?int $userid = null): array {
226226
global $DB;
227227

228-
$conditions = $rule->get_condition_records();
228+
$matchinguserscache = cache::make('tool_dynamic_cohorts', 'matchinguserscount');
229229

230+
$conditions = $rule->get_condition_records();
230231
if (empty($conditions)) {
232+
$matchinguserscache->set($rule->get('id'), 0);
231233
return [];
232234
}
233235

@@ -238,15 +240,21 @@ public static function get_matching_users(rule $rule, ?int $userid = null): arra
238240
} catch (\Exception $exception ) {
239241
self::trigger_matching_failed_event($rule, $exception->getMessage());
240242
$rule->mark_broken();
243+
$matchinguserscache->set($rule->get('id'), 0);
241244

242245
return [];
243246
}
244247

245248
try {
246-
return $DB->get_records_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params());
249+
$users = $DB->get_records_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params());
250+
if (empty($userid)) {
251+
$matchinguserscache->set($rule->get('id'), count($users));
252+
}
253+
return $users;
247254
} catch (\Exception $exception) {
248255
self::trigger_matching_failed_event($rule, $exception->getMessage());
249256
$rule->mark_broken();
257+
$matchinguserscache->set($rule->get('id'), 0);
250258

251259
return [];
252260
}
@@ -256,37 +264,48 @@ public static function get_matching_users(rule $rule, ?int $userid = null): arra
256264
* Get count of matching users for a given rule.
257265
*
258266
* @param \tool_dynamic_cohorts\rule $rule
259-
* @param int|null $userid
260267
*
261268
* @return int
262269
*/
263-
public static function get_matching_users_count(rule $rule, ?int $userid = null): int {
270+
public static function get_matching_users_count(rule $rule): int {
264271
global $DB;
265272

273+
$matchinguserscache = cache::make('tool_dynamic_cohorts', 'matchinguserscount');
274+
275+
$count = $matchinguserscache->get($rule->get('id'));
276+
if ($count !== false) {
277+
return $count;
278+
}
279+
266280
$conditions = $rule->get_condition_records();
267281

268282
if (empty($conditions)) {
283+
$matchinguserscache->set($rule->get('id'), 0);
269284
return 0;
270285
}
271286

272287
$sql = "SELECT COUNT(DISTINCT u.id) cnt FROM {user} u";
273288

274289
try {
275-
$sqldata = condition_manager::build_sql_data($conditions, $rule->get('operator'), $userid);
290+
$sqldata = condition_manager::build_sql_data($conditions, $rule->get('operator'));
276291
} catch (\Exception $exception ) {
277292
self::trigger_matching_failed_event($rule, $exception->getMessage());
278293
$rule->mark_broken();
279294

295+
$matchinguserscache->set($rule->get('id'), 0);
280296
return 0;
281297
}
282298

283299
try {
284300
$result = $DB->get_record_sql($sql . $sqldata->get_join() . ' WHERE ' . $sqldata->get_where(), $sqldata->get_params());
301+
$matchinguserscache->set($rule->get('id'), $result->cnt);
302+
285303
return $result->cnt;
286304
} catch (\Exception $exception) {
287305
self::trigger_matching_failed_event($rule, $exception->getMessage());
288306
$rule->mark_broken();
289307

308+
$matchinguserscache->set($rule->get('id'), 0);
290309
return 0;
291310
}
292311
}

db/caches.php

+5
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@
4444
'conditionschanged',
4545
],
4646
],
47+
'matchinguserscount' => [
48+
'mode' => cache_store::MODE_APPLICATION,
49+
'simpledata' => true,
50+
'staticacceleration' => true,
51+
],
4752
];

lang/en/tool_dynamic_cohorts.php

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$string['bulkprocessing'] = 'Bulk processing';
3737
$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.';
3838
$string['cachedef_conditionrecords'] = 'Conditions for a rule';
39+
$string['cachedef_matchingusers'] = 'Matching users for a rule';
3940
$string['cachedef_rulesconditions'] = 'Rules with a specific condition';
4041
$string['cohort'] = 'Cohort';
4142
$string['cohortid'] = 'Cohort';

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
2828
$plugin->release = 2024112100;
29-
$plugin->version = 2024112101;
29+
$plugin->version = 2024112102;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [404, 405];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)