Cache misses from the backend cache the same as hits.
Bug: T127772
Change-Id: If2fe1920411b24862acea888c627db13717da8bd
protected function doGet( $key, $flags = 0 ) {
$ret = parent::doGet( $key, $flags );
- if ( $ret === false ) {
+ if ( $ret === false && !$this->hasKey( $key ) ) {
$ret = $this->backend->doGet( $key, $flags );
- if ( $ret !== false ) {
- $this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY );
- }
+ $this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY );
}
return $ret;
}
return true;
}
+ /**
+ * Does this bag have a non-null value for the given key?
+ *
+ * @param string $key
+ * @return bool
+ * @since 1.27
+ */
+ protected function hasKey( $key ) {
+ return isset( $this->bag[$key] );
+ }
+
protected function doGet( $key, $flags = 0 ) {
- if ( !isset( $this->bag[$key] ) ) {
+ if ( !$this->hasKey( $key ) ) {
return false;
}
$cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
$this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
}
+
+ public function testCacheBackendMisses() {
+ $backend = new HashBagOStuff;
+ $cache = new CachedBagOStuff( $backend );
+
+ // First hit primes the cache with miss from the backend
+ $this->assertEquals( false, $cache->get( 'foo' ) );
+
+ // Change the value in the backend
+ $backend->set( 'foo', true );
+
+ // Second hit returns the cached miss
+ $this->assertEquals( false, $cache->get( 'foo' ) );
+
+ // But a fresh value is read from the backend
+ $backend->set( 'bar', true );
+ $this->assertEquals( true, $cache->get( 'bar' ) );
+ }
}