Skip to content

Commit

Permalink
Merge branch 'MDL-76791-master' of https://github.com/sammarshallou/m…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Feb 16, 2023
2 parents 2e9025e + 3353aec commit 6d31626
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cache/classes/loaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ public function acquire_lock($key) {
if ($this->get_loader() !== false) {
$this->get_loader()->acquire_lock($key);
}
$key = $this->parse_key($key);
$key = cache_helper::hash_key($key, $this->get_definition());
$before = microtime(true);
if ($this->nativelocking) {
$lock = $this->get_store()->acquire_lock($key, $this->get_identifier());
Expand All @@ -1706,7 +1706,7 @@ public function acquire_lock($key) {
* someone else has the lock.
*/
public function check_lock_state($key) {
$key = $this->parse_key($key);
$key = cache_helper::hash_key($key, $this->get_definition());
if (!empty($this->locks[$key])) {
return true; // Shortcut to save having to make a call to the cache store if the lock is held by this process.
}
Expand All @@ -1726,7 +1726,7 @@ public function check_lock_state($key) {
*/
public function release_lock($key) {
$loaderkey = $key;
$key = $this->parse_key($key);
$key = cache_helper::hash_key($key, $this->get_definition());
if ($this->nativelocking) {
$released = $this->get_store()->release_lock($key, $this->get_identifier());
} else {
Expand Down
24 changes: 24 additions & 0 deletions cache/tests/cache_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,30 @@ public function test_application_locking() {
$this->assertFalse($cache->has('a'));
}

/**
* The application locking feature should work with caches that support multiple identifiers
* (static cache and MongoDB with a specific setting).
*
* @covers \cache_application
*/
public function test_application_locking_multiple_identifier_cache() {
// Get an arbitrary definition (modinfo).
$instance = cache_config_testing::instance(true);
$definitions = $instance->get_definitions();
$definition = \cache_definition::load('phpunit', $definitions['core/coursemodinfo']);

// Set up a static cache using that definition, wrapped in cache_application so we can do
// locking.
$store = new \cachestore_static('test');
$store->initialise($definition);
$cache = new cache_application($definition, $store);

// Test the three locking functions.
$cache->acquire_lock('frog');
$this->assertTrue($cache->check_lock_state('frog'));
$cache->release_lock('frog');
}

/**
* Test requiring a lock before attempting to set a key.
*
Expand Down

0 comments on commit 6d31626

Please sign in to comment.