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 // Time travel is easy.
68 'data' => time() + 60,
70 $cache_item = (object) [
74 'expire' => time() + 3600,
78 $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
79 $fast_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
81 // We should get a call for the timestamp on the consistent backend.
82 $consistent_cache->expects($this->once())
84 ->with($timestamp_item->cid)
85 ->will($this->returnValue($timestamp_item));
87 // We should get a call for the cache item on the consistent backend.
88 $consistent_cache->expects($this->once())
89 ->method('getMultiple')
90 ->with([$cache_item->cid])
91 ->will($this->returnValue([$cache_item->cid => $cache_item]));
93 // We should get a call for the cache item on the fast backend.
94 $fast_cache->expects($this->once())
95 ->method('getMultiple')
96 ->with([$cache_item->cid])
97 ->will($this->returnValue([$cache_item->cid => $cache_item]));
99 // We should get a call to set the cache item on the fast backend.
100 $fast_cache->expects($this->once())
102 ->with($cache_item->cid, $cache_item->data, $cache_item->expire);
104 $chained_fast_backend = new ChainedFastBackend(
109 $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);