3 namespace Drupal\Tests\Core\Cache;
5 use Drupal\Core\Cache\ChainedFastBackend;
6 use Drupal\Core\Cache\MemoryBackend;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\Core\Cache\ChainedFastBackend
13 class ChainedFastBackendTest extends UnitTestCase {
16 * The consistent cache backend.
18 * @var \Drupal\Core\Cache\CacheBackendInterface
20 protected $consistentCache;
23 * The fast cache backend.
25 * @var \Drupal\Core\Cache\CacheBackendInterface
37 * Tests a get() on the fast backend, with no hit on the consistent backend.
39 public function testGetDoesntHitConsistentBackend() {
40 $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
41 $timestamp_cid = ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo';
42 // Use the request time because that is what we will be comparing against.
43 $timestamp_item = (object) ['cid' => $timestamp_cid, 'data' => (int) $_SERVER['REQUEST_TIME'] - 60];
44 $consistent_cache->expects($this->once())
45 ->method('get')->with($timestamp_cid)
46 ->will($this->returnValue($timestamp_item));
47 $consistent_cache->expects($this->never())
48 ->method('getMultiple');
50 $fast_cache = new MemoryBackend();
51 $fast_cache->set('foo', 'baz');
53 $chained_fast_backend = new ChainedFastBackend(
58 $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
62 * Tests a fast cache miss gets data from the consistent cache backend.
64 public function testFallThroughToConsistentCache() {
65 $timestamp_item = (object) [
66 'cid' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
67 'data' => time() + 60, // Time travel is easy.
69 $cache_item = (object) [
73 'expire' => time() + 3600,
77 $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
78 $fast_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
80 // We should get a call for the timestamp on the consistent backend.
81 $consistent_cache->expects($this->once())
83 ->with($timestamp_item->cid)
84 ->will($this->returnValue($timestamp_item));
86 // We should get a call for the cache item on the consistent backend.
87 $consistent_cache->expects($this->once())
88 ->method('getMultiple')
89 ->with([$cache_item->cid])
90 ->will($this->returnValue([$cache_item->cid => $cache_item]));
92 // We should get a call for the cache item on the fast backend.
93 $fast_cache->expects($this->once())
94 ->method('getMultiple')
95 ->with([$cache_item->cid])
96 ->will($this->returnValue([$cache_item->cid => $cache_item]));
98 // We should get a call to set the cache item on the fast backend.
99 $fast_cache->expects($this->once())
101 ->with($cache_item->cid, $cache_item->data, $cache_item->expire);
103 $chained_fast_backend = new ChainedFastBackend(
108 $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);