diff --git a/classes/local/tag/tag_manager.php b/classes/local/tag/tag_manager.php index 603d04a4..45e6e7d5 100644 --- a/classes/local/tag/tag_manager.php +++ b/classes/local/tag/tag_manager.php @@ -227,9 +227,21 @@ public static function get_sync_status_string(int $tagsyncstatus): string { */ public static function get_tag_sync_status_summary(): array { global $DB; - return $DB->get_records_sql("SELECT tagsyncstatus, COUNT(tagsyncstatus) as statuscount + $cachekey = 'tag_sync_status_summary'; + $cache = \cache::make('tool_objectfs', 'tagsummary'); + $cachedresult = $cache->get($cachekey); + + if ($cachedresult !== false) { + return $cachedresult; + } + + $result = $DB->get_records_sql("SELECT tagsyncstatus, COUNT(tagsyncstatus) as statuscount FROM {tool_objectfs_objects} GROUP BY tagsyncstatus"); + + $cache->set($cachekey, $result); + + return $result; } /** diff --git a/db/caches.php b/db/caches.php new file mode 100644 index 00000000..4dc86ab3 --- /dev/null +++ b/db/caches.php @@ -0,0 +1,34 @@ +. + +/** + * Cache definitions for tool_objectfs + * + * @package tool_objectfs + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$definitions = [ + 'tagsummary' => [ + 'mode' => cache_store::MODE_APPLICATION, + 'ttl' => 1800, + 'simplekeys' => true, + 'simpledata' => true, + ], +]; diff --git a/db/upgrade.php b/db/upgrade.php index 107f8293..9bdc3c9f 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -223,5 +223,15 @@ function xmldb_tool_objectfs_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2024120600, 'tool', 'objectfs'); } + if ($oldversion < 2025071100) { + $table = new xmldb_table('tool_objectfs_objects'); + $index = new xmldb_index('tagsyncstatus_idx', XMLDB_INDEX_NOTUNIQUE, ['tagsyncstatus']); + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + upgrade_plugin_savepoint(true, 2025071100, 'tool', 'objectfs'); + } + return true; } diff --git a/tests/check/tagging_sync_status_test.php b/tests/check/tagging_sync_status_test.php index eba545cf..53c99d1a 100644 --- a/tests/check/tagging_sync_status_test.php +++ b/tests/check/tagging_sync_status_test.php @@ -65,4 +65,31 @@ public function test_get_result_warning() { $check = new tagging_sync_status(); $this->assertEquals(result::WARNING, $check->get_result()->get_status()); } + + /** + * Test caching + */ + public function test_tag_sync_status_summary_caching() { + global $DB; + $this->enable_filesystem_and_set_tagging(true); + + $cache = \cache::make('tool_objectfs', 'tagsummary'); + $cache->delete('tag_sync_status_summary'); + + $this->assertEquals($cache->get('tag_sync_status_summary'), false); + + // Test: First call should hit DB, second should use cache. + $before = $DB->perf_get_queries(); + $result1 = tag_manager::get_tag_sync_status_summary(); + $after1 = $DB->perf_get_queries(); + + $this->assertNotEquals($cache->get('tag_sync_status_summary'), false); + + $result2 = tag_manager::get_tag_sync_status_summary(); + $after2 = $DB->perf_get_queries(); + + $this->assertGreaterThan($before, $after1, 'First call should hit DB'); + $this->assertEquals($after1, $after2, 'Second call should use cache'); + $this->assertEquals($result1, $result2, 'Cached and DB results should match'); + } } diff --git a/version.php b/version.php index d536ec90..92f54b7e 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2025040900; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = 2025040900; // Same as version. +$plugin->version = 2025071100; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2025071100; // Same as version. $plugin->requires = 2024042200; // Requires 4.4. $plugin->component = "tool_objectfs"; $plugin->maturity = MATURITY_STABLE;