Browse files

MDL-36120 cache: Tidied up post peer-review

  • Loading branch information...
1 parent 47834bc commit 702651c7a615e93c104ff0ce6c836e4551539b30 Sam Hemelryk committed Nov 5, 2012
Showing with 23 additions and 13 deletions.
  1. +4 −1 cache/classes/definition.php
  2. +2 −1 cache/classes/helper.php
  3. +8 −9 cache/stores/file/lib.php
  4. +9 −2 cache/tests/cache_test.php
View
5 cache/classes/definition.php
@@ -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() {
View
3 cache/classes/helper.php
@@ -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);
View
17 cache/stores/file/lib.php
@@ -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;
}
}
@@ -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';
}
}
@@ -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);
View
11 cache/tests/cache_test.php
@@ -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() {
@@ -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);

0 comments on commit 702651c

Please sign in to comment.