private function doCacheTest(PhutilKeyValueCache $cache) { $key1 = 'test:' . mt_rand(); $key2 = 'test:' . mt_rand(); $default = 'cache-miss'; $value1 = 'cache-hit1'; $value2 = 'cache-hit2'; $cache->setProfiler(PhutilServiceProfiler::getInstance()); $test_info = get_class($cache); // Test that we miss correctly on missing values. $this->assertEqual($default, $cache->getKey($key1, $default), $test_info); $this->assertEqual(array(), $cache->getKeys(array($key1, $key2)), $test_info); // Test that we can set individual keys. $cache->setKey($key1, $value1); $this->assertEqual($value1, $cache->getKey($key1, $default), $test_info); $this->assertEqual(array($key1 => $value1), $cache->getKeys(array($key1, $key2)), $test_info); // Test that we can delete individual keys. $cache->deleteKey($key1); $this->assertEqual($default, $cache->getKey($key1, $default), $test_info); $this->assertEqual(array(), $cache->getKeys(array($key1, $key2)), $test_info); // Test that we can set multiple keys. $cache->setKeys(array($key1 => $value1, $key2 => $value2)); $this->assertEqual($value1, $cache->getKey($key1, $default), $test_info); $this->assertEqual(array($key1 => $value1, $key2 => $value2), $cache->getKeys(array($key1, $key2)), $test_info); // Test that we can delete multiple keys. $cache->deleteKeys(array($key1, $key2)); $this->assertEqual($default, $cache->getKey($key1, $default), $test_info); $this->assertEqual(array(), $cache->getKeys(array($key1, $key2)), $test_info); // NOTE: The TTL tests are necessarily slow (we must sleep() through the // TTLs) and do not work with APC (it does not TTL until the next request) // so they're disabled by default. If you're developing the cache stack, // it may be useful to run them. return; // Test that keys expire when they TTL. $cache->setKey($key1, $value1, 1); $cache->setKey($key2, $value2, 5); $this->assertEqual($value1, $cache->getKey($key1, $default)); $this->assertEqual($value2, $cache->getKey($key2, $default)); sleep(2); $this->assertEqual($default, $cache->getKey($key1, $default)); $this->assertEqual($value2, $cache->getKey($key2, $default)); // Test that setting a 0 TTL overwrites a nonzero TTL. $cache->setKey($key1, $value1, 1); $this->assertEqual($value1, $cache->getKey($key1, $default)); $cache->setKey($key1, $value1, 0); $this->assertEqual($value1, $cache->getKey($key1, $default)); sleep(2); $this->assertEqual($value1, $cache->getKey($key1, $default)); }
public function setProfiler(PhutilServiceProfiler $profiler) { parent::setProfiler($profiler); foreach ($this->cachesForward as $cache) { $cache->setProfiler($profiler); } return $this; }