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

Support custom locks #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 31 additions & 10 deletions classes/cache_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $CFG object should be available here, so it will be a little cleaner to use $CFG->wwwroot . '/cache...'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$CFG->dirroot rather - but otherwise sure.

if (!file_exists($cachepath)) {
throw new cache_exception(get_string('lock_bad_type', 'tool_forcedcache', $pluginname));
}
}

return $locks;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_forcedcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down