From 69a3928c4b3a8bd7b288b818fbbdccae5d22189f Mon Sep 17 00:00:00 2001 From: Adam Olley Date: Tue, 17 Aug 2021 11:13:18 +0930 Subject: [PATCH] Support custom locks Without this - there is no way to define custom lock instances and the default file locking is forced. This lets you define your own: ``` $CFG->tool_forcedcache_config_array = [ 'locks' => [ 'muclock_default' => [ 'name' => 'Example custom lock', 'type' => 'cachelock_file', ], ], ]; ``` --- classes/cache_config.php | 41 +++++++++++++++++++++++++++--------- lang/en/tool_forcedcache.php | 2 ++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/classes/cache_config.php b/classes/cache_config.php index deb4e5d..9ee89d9 100644 --- a/classes/cache_config.php +++ b/classes/cache_config.php @@ -113,7 +113,10 @@ private function generate_config_array(): array { } // Generate locks. - $locks = $this->generate_locks(); + if (!isset($config['locks'])) { + $config['locks'] = []; + } + $locks = $this->generate_locks($config['locks']); // Get the siteidentifier. Copies pattern from cache_config. // Uses 'forcedcache' if not known. @@ -387,15 +390,33 @@ private function generate_definition_mappings_from_rules(array $rules, array $de * * @return array array of locks to use. */ - private function generate_locks() : array { - return array( - 'default_file_lock' => array( - 'name' => 'cachelock_file_default', - 'type' => 'cachelock_file', - 'dir' => 'filelocks', - 'default' => true - ) - ); + private function generate_locks(array $locks) : array { + if (empty($locks)) { + return array( + 'default_file_lock' => array( + 'name' => 'cachelock_file_default', + 'type' => 'cachelock_file', + 'dir' => 'filelocks', + 'default' => true + ) + ); + } + + foreach ($locks as $key => $lock) { + // First check that all the required fields are present in the lock. + if (!(array_key_exists('name', $lock) || array_key_exists('type', $lock))) { + throw new cache_exception(get_string('lock_missing_fields', 'tool_forcedcache', $key)); + } + + // Then check the lock plugins exist. + $pluginname = substr($lock['type'], 10); + $cachepath = __DIR__.'/../../../../cache/locks/' . $pluginname . '/lib.php'; + if (!file_exists($cachepath)) { + throw new cache_exception(get_string('lock_bad_type', 'tool_forcedcache', $pluginname)); + } + } + + return $locks; } /** diff --git a/lang/en/tool_forcedcache.php b/lang/en/tool_forcedcache.php index 5196819..ef69ac5 100644 --- a/lang/en/tool_forcedcache.php +++ b/lang/en/tool_forcedcache.php @@ -33,6 +33,8 @@ $string['config_json_missing'] = 'Error reading specified JSON file. File may not exist, or path is incorrect.'; $string['config_path_and_array'] = 'Detected both path to file and config array. Only one can be specified.'; $string['definition_not_found'] = 'Definition not defined for configuration override: {$a}.'; +$string['lock_bad_type'] = 'Error loading lock {$a}. Lock may not exist or type is malformed.'; +$string['lock_missing_fields'] = 'Error reading lock {$a}, it may be missing field or malformed.'; $string['store_missing_fields'] = 'Error reading store {$a}, it may be missing fields or malformed.'; $string['store_bad_type'] = 'Error loading store {$a}. Store may not exist or type is malformed.'; $string['store_not_ready'] = 'Error creating store {$a}, config may be incorrect or missing required fields.';