CacheTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace dcb9\redis\tests;
  3. use dcb9\redis\Cache;
  4. use dcb9\redis\Connection;
  5. class CacheTest extends TestCase
  6. {
  7. private $_cacheInstance = null;
  8. /**
  9. * @return Cache
  10. */
  11. protected function getCacheInstance()
  12. {
  13. $params = self::getParam();
  14. $connection = new Connection($params);
  15. $this->mockApplication(['components' => ['redis' => $connection]]);
  16. if ($this->_cacheInstance === null) {
  17. $this->_cacheInstance = new Cache();
  18. }
  19. return $this->_cacheInstance;
  20. }
  21. /**
  22. * Store a value that is 2 times buffer size big
  23. * https://github.com/yiisoft/yii2/issues/743
  24. */
  25. public function testLargeData()
  26. {
  27. $cache = $this->getCacheInstance();
  28. $data = str_repeat('XX', 8192); // http://www.php.net/manual/en/function.fread.php
  29. $key = 'bigdata1';
  30. $this->assertFalse($cache->get($key));
  31. $cache->set($key, $data);
  32. $this->assertTrue($cache->get($key) === $data);
  33. // try with multibyte string
  34. $data = str_repeat('ЖЫ', 8192); // http://www.php.net/manual/en/function.fread.php
  35. $key = 'bigdata2';
  36. $this->assertFalse($cache->get($key));
  37. $cache->set($key, $data);
  38. $this->assertTrue($cache->get($key) === $data);
  39. }
  40. /**
  41. * Store a megabyte and see how it goes
  42. * https://github.com/yiisoft/yii2/issues/6547
  43. */
  44. public function testReallyLargeData()
  45. {
  46. $cache = $this->getCacheInstance();
  47. $keys = [];
  48. for ($i = 1; $i < 16; $i++) {
  49. $key = 'realbigdata' . $i;
  50. $data = str_repeat('X', 100 * 1024); // 100 KB
  51. $keys[$key] = $data;
  52. // $this->assertTrue($cache->get($key) === false); // do not display 100KB in terminal if this fails :)
  53. $cache->set($key, $data);
  54. }
  55. $values = $cache->mget(array_keys($keys));
  56. foreach ($keys as $key => $value) {
  57. $this->assertArrayHasKey($key, $values);
  58. $this->assertTrue($values[$key] === $value);
  59. }
  60. }
  61. public function testMultiByteGetAndSet()
  62. {
  63. $cache = $this->getCacheInstance();
  64. $data = ['abc' => 'ежик', 2 => 'def'];
  65. $key = 'data1';
  66. $this->assertFalse($cache->get($key));
  67. $cache->set($key, $data);
  68. $this->assertTrue($cache->get($key) === $data);
  69. }
  70. public function testFlushValues()
  71. {
  72. $cache = $this->getCacheInstance();
  73. $key = 'data';
  74. $cache->set($key, 'val');
  75. $cache->flush();
  76. $this->assertFalse($cache->get($key));
  77. }
  78. public function testMultiSet()
  79. {
  80. $cache = $this->getCacheInstance();
  81. $items = [
  82. 'k1' => 'v1',
  83. 'k2' => 'v2',
  84. 'k3' => 'v3',
  85. ];
  86. $this->assertEquals([], $cache->multiSet($items, 1));
  87. $this->assertEquals('v1', $cache->get('k1'));
  88. $this->assertEquals('v2', $cache->get('k2'));
  89. $this->assertEquals('v3', $cache->get('k3'));
  90. sleep(1);
  91. $this->assertFalse($cache->get('k1'));
  92. $this->assertFalse($cache->get('k2'));
  93. $this->assertFalse($cache->get('k3'));
  94. $this->assertFalse($cache->exists('k1'));
  95. $this->assertFalse($cache->exists('k2'));
  96. $this->assertFalse($cache->exists('k3'));
  97. $cache->multiSet($items);
  98. sleep(2);
  99. $this->assertEquals('v1', $cache->get('k1'));
  100. $this->assertEquals('v2', $cache->get('k2'));
  101. $this->assertEquals('v3', $cache->get('k3'));
  102. }
  103. public function testSetGet()
  104. {
  105. $cache = $this->getCacheInstance();
  106. $cache->set('key', 'val', 1);
  107. $this->assertEquals('val', $cache->get('key'));
  108. sleep(1);
  109. $this->assertFalse($cache->get('key'));
  110. $this->assertFalse($cache->exists('key'));
  111. $cache->set('key', 'val');
  112. $this->assertTrue($cache->delete('key'));
  113. $this->assertFalse($cache->exists('key'));
  114. }
  115. public function testMultiAdd()
  116. {
  117. $cache = $this->getCacheInstance();
  118. $items = [
  119. 'k1' => 'v1',
  120. 'k2' => 'v2',
  121. 'k3' => 'v3',
  122. ];
  123. $cache->multiSet($items);
  124. $this->assertEquals(['k2'], $cache->multiAdd([
  125. 'k2' => 'vv22',
  126. 'k4' => 'vv44',
  127. ]));
  128. $this->assertEquals('v2', $cache->get('k2'));
  129. $this->assertEquals('vv44', $cache->get('k4'));
  130. $this->assertEquals(['k1'], $cache->multiAdd([
  131. 'k5' => 'vv55',
  132. 'k1' => 'v1',
  133. ], 1));
  134. $this->assertEquals('vv55', $cache->get('k5'));
  135. sleep(1);
  136. $this->assertFalse($cache->exists('k5'));
  137. $this->assertTrue($cache->exists('k1'));
  138. }
  139. }