Skip to content

Commit

Permalink
MDL-36120 cache: Tidied up post peer-review
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Nov 6, 2012
1 parent 47834bc commit 702651c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
5 changes: 4 additions & 1 deletion cache/classes/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,10 @@ public function get_component() {
}

/**
* Returns the PARAM type that best describes the expected keys.
* Returns true if this definition is using simple keys.
*
* Simple keys contain only a-zA-Z0-9_
*
* @return bool
*/
public function uses_simple_keys() {
Expand Down
3 changes: 2 additions & 1 deletion cache/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ public static function get_definition_name($definition) {
*/
public static function hash_key($key, cache_definition $definition) {
if ($definition->uses_simple_keys()) {
return $definition->generate_single_key_prefix() . '-' . (string)$key;
// We put the key first so that we can be sure the start of the key changes.
return (string)$key . '-' . $definition->generate_single_key_prefix();
}
$key = $definition->generate_single_key_prefix() . '-' . $key;
return sha1($key);
Expand Down
17 changes: 8 additions & 9 deletions cache/stores/file/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ public function __construct($name, array $configuration = array()) {
$this->path = $path;
// Check if we should prescan the directory.
if (array_key_exists('prescan', $configuration)) {
$this->prescan = (bool)$configuration['prescan'];
$this->prescan = (bool)$configuration['prescan'];
} else {
// Default is no, we should not prescan.
$this->prescan = false;
$this->prescan = false;
}
// Check if we should be storing in a single directory.
if (array_key_exists('singledirectory', $configuration)) {
$this->singledirectory = (bool)$configuration['singledirectory'];
$this->singledirectory = (bool)$configuration['singledirectory'];
} else {
// Default: No, we will use multiple directories.
$this->singledirectory = false;
$this->singledirectory = false;
}
}

Expand Down Expand Up @@ -268,7 +268,7 @@ protected function glob_keys_pattern() {
if ($this->singledirectory) {
return $this->path . '/*.cache';
} else {
return $this->path . '/*/*/*.cache';
return $this->path . '/*/*.cache';
}
}

Expand All @@ -284,10 +284,9 @@ protected function file_path_for_key($key, $create = false) {
// Its a single directory, easy, just the store instances path + the file name.
return $this->path . '/' . $key . '.cache';
} else {
// We are using multiple subdirectories. We want two levels.
$subdir1 = substr($key, 0, 2);
$subdir2 = substr($key, 2, 2);
$dir = $this->path . '/' . $subdir1 .'/'. $subdir2;
// We are using a single subdirectory to achieve 1 level.
$subdir = substr($key, 0, 3);
$dir = $this->path . '/' . $subdir;
if ($create) {
// Create the directory. This function does it recursivily!
make_writable_directory($dir);
Expand Down
11 changes: 9 additions & 2 deletions cache/tests/cache_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ public function test_definition_simplekeys() {
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));

$cache->purge();

$this->assertTrue($cache->set('1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('1'));
$this->assertTrue($cache->set('2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('2'));
}

public function test_definition_ttl() {
Expand Down Expand Up @@ -552,13 +559,13 @@ public function test_distributed_application_event_invalidation() {

// OK data added, data invalidated, and invalidation time has been set.
// Now we need to manually add back the data and adjust the invalidation time.
$timefile = $CFG->dataroot.'/cache/cachestore_file//default_application/phpunit_eventinvalidationtest/a6/5b/a65b1dc524cf6e03c1795197c84d5231eb229b86.cache';
$timefile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/a65/a65b1dc524cf6e03c1795197c84d5231eb229b86.cache';
$timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
make_writable_directory(dirname($timefile));
file_put_contents($timefile, $timecont);
$this->assertTrue(file_exists($timefile));

$datafile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/62/6e/626e9c7a45febd98f064c2b383de8d9d4ebbde7b.cache';
$datafile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/626/626e9c7a45febd98f064c2b383de8d9d4ebbde7b.cache';
$datacont = serialize("test data 1");
make_writable_directory(dirname($datafile));
file_put_contents($datafile, $datacont);
Expand Down

0 comments on commit 702651c

Please sign in to comment.