TYPO3 CMS  TYPO3_6-2
RedisBackendTest.php
Go to the documentation of this file.
1 <?php
3 
33 
39  protected $backend = NULL;
40 
46  protected $redis = NULL;
47 
51  public function setUp() {
52  if (!extension_loaded('redis')) {
53  $this->markTestSkipped('redis extension was not available');
54  }
55  try {
56  if (!@fsockopen('127.0.0.1', 6379)) {
57  $this->markTestSkipped('redis server not reachable');
58  }
59  } catch (\Exception $e) {
60  $this->markTestSkipped('redis server not reachable');
61  }
62  }
63 
69  protected function setUpBackend(array $backendOptions = array()) {
70  $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
71  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
72  $this->backend = new \TYPO3\CMS\Core\Cache\Backend\RedisBackend('Testing', $backendOptions);
73  $this->backend->setCache($mockCache);
74  $this->backend->initializeObject();
75  }
76 
80  protected function setUpRedis() {
81  $this->redis = new \Redis();
82  $this->redis->connect('127.0.0.1', 6379);
83  }
84 
88  public function tearDown() {
89  if ($this->backend instanceof \TYPO3\CMS\Core\Cache\Backend\RedisBackend) {
90  $this->backend->flush();
91  }
92  parent::tearDown();
93  }
94 
99  try {
100  $this->setUpBackend(array('database' => 1));
101  } catch (Exception $e) {
102  $this->assertTrue();
103  }
104  }
105 
111  $this->setUpBackend(array('database' => 'foo'));
112  }
113 
119  $this->setUpBackend(array('database' => -1));
120  }
121 
127  $this->setUpBackend(array('compression' => 'foo'));
128  }
129 
135  $this->setUpBackend(array('compressionLevel' => 'foo'));
136  }
137 
143  $this->setUpBackend(array('compressionLevel' => 11));
144  }
145 
151  $this->setUpBackend();
152  $this->backend->set(array(), 'data');
153  }
154 
160  $this->setUpBackend();
161  $this->backend->set($this->getUniqueId('identifier'), array());
162  }
163 
169  $this->setUpBackend();
170  $this->backend->set($this->getUniqueId('identifier'), 'data', array(), -42);
171  }
172 
178  $this->setUpBackend();
179  $this->backend->set($this->getUniqueId('identifier'), 'data', array(), array());
180  }
181 
186  $this->setUpRedis();
187  $this->redis->select(1);
188  $this->setUpBackend(array('database' => 1));
189  $identifier = $this->getUniqueId('identifier');
190  $this->backend->set($identifier, 'data');
191  $this->assertTrue($this->redis->exists('identData:' . $identifier));
192  }
193 
198  $this->setUpBackend();
199  $this->setUpRedis();
200  $identifier = $this->getUniqueId('identifier');
201  $this->backend->set($identifier, 'data');
202  $this->assertSame(\Redis::REDIS_STRING, $this->redis->type('identData:' . $identifier));
203  }
204 
209  $this->setUpBackend();
210  $this->setUpRedis();
211  $identifier = $this->getUniqueId('identifier');
212  $defaultLifetime = 42;
213  $this->backend->setDefaultLifetime($defaultLifetime);
214  $this->backend->set($identifier, 'data');
215  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
216  $this->assertSame($defaultLifetime, $lifetimeRegisteredInBackend);
217  }
218 
223  $this->setUpBackend();
224  $this->setUpRedis();
225  $identifier = $this->getUniqueId('identifier');
226  $lifetime = 43;
227  $this->backend->set($identifier, 'data', array(), $lifetime);
228  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
229  $this->assertSame($lifetime, $lifetimeRegisteredInBackend);
230  }
231 
236  $this->setUpBackend();
237  $this->setUpRedis();
238  $identifier = $this->getUniqueId('identifier');
239  $this->backend->set($identifier, 'data', array(), 0);
240  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
241  $this->assertSame(31536000, $lifetimeRegisteredInBackend);
242  }
243 
248  $this->setUpBackend();
249  $data = 'data 1';
250  $identifier = $this->getUniqueId('identifier');
251  $this->backend->set($identifier, $data);
252  $otherData = 'data 2';
253  $this->backend->set($identifier, $otherData);
254  $fetchedData = $this->backend->get($identifier);
255  $this->assertSame($otherData, $fetchedData);
256  }
257 
262  $this->setUpBackend();
263  $this->setUpRedis();
264  $data = 'data';
265  $identifier = $this->getUniqueId('identifier');
266  $this->backend->set($identifier, $data);
267  $lifetime = 42;
268  $this->backend->set($identifier, $data, array(), $lifetime);
269  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
270  $this->assertSame($lifetime, $lifetimeRegisteredInBackend);
271  }
272 
277  $this->setUpBackend();
278  $this->setUpRedis();
279  $data = 'data';
280  $identifier = $this->getUniqueId('identifier');
281  $lifetime = 42;
282  $this->backend->set($identifier, $data, array(), $lifetime);
283  $newDefaultLifetime = 43;
284  $this->backend->setDefaultLifetime($newDefaultLifetime);
285  $this->backend->set($identifier, $data, array(), $newDefaultLifetime);
286  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
287  $this->assertSame($newDefaultLifetime, $lifetimeRegisteredInBackend);
288  }
289 
294  $this->setUpBackend();
295  $this->setUpRedis();
296  $data = 'data';
297  $identifier = $this->getUniqueId('identifier');
298  $lifetime = 42;
299  $this->backend->set($identifier, $data, array(), $lifetime);
300  $this->backend->set($identifier, $data, array(), 0);
301  $lifetimeRegisteredInBackend = $this->redis->ttl('identData:' . $identifier);
302  $this->assertSame(31536000, $lifetimeRegisteredInBackend);
303  }
304 
309  $this->setUpBackend();
310  $this->setUpRedis();
311  $identifier = $this->getUniqueId('identifier');
312  $this->backend->set($identifier, 'data', array('tag'));
313  $this->assertSame(\Redis::REDIS_SET, $this->redis->type('identTags:' . $identifier));
314  }
315 
320  $this->setUpBackend();
321  $this->setUpRedis();
322  $identifier = $this->getUniqueId('identifier');
323  $tags = array('thatTag', 'thisTag');
324  $this->backend->set($identifier, 'data', $tags);
325  $savedTags = $this->redis->sMembers('identTags:' . $identifier);
326  sort($savedTags);
327  $this->assertSame($tags, $savedTags);
328  }
329 
334  $this->setUpBackend();
335  $this->setUpRedis();
336  $identifier = $this->getUniqueId('identifier');
337  $tags = array('fooTag', 'barTag');
338  $this->backend->set($identifier, 'data', $tags);
339  $this->backend->set($identifier, 'data', array());
340  $this->assertSame(array(), $this->redis->sMembers('identTags:' . $identifier));
341  }
342 
347  $this->setUpBackend();
348  $this->setUpRedis();
349  $identifier = $this->getUniqueId('identifier');
350  $firstTagSet = array('tag1', 'tag2', 'tag3', 'tag4');
351  $this->backend->set($identifier, 'data', $firstTagSet);
352  $secondTagSet = array('tag1', 'tag3');
353  $this->backend->set($identifier, 'data', $secondTagSet);
354  $actualTagSet = $this->redis->sMembers('identTags:' . $identifier);
355  sort($actualTagSet);
356  $this->assertSame($secondTagSet, $actualTagSet);
357  }
358 
363  $this->setUpBackend();
364  $this->setUpRedis();
365  $identifier = $this->getUniqueId('identifier');
366  $tag = 'tag';
367  $this->backend->set($identifier, 'data', array($tag));
368  $this->assertSame(\Redis::REDIS_SET, $this->redis->type('tagIdents:' . $tag));
369  }
370 
375  $this->setUpBackend();
376  $this->setUpRedis();
377  $identifier = $this->getUniqueId('identifier');
378  $tag = 'thisTag';
379  $this->backend->set($identifier, 'data', array($tag));
380  $savedTagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
381  $this->assertSame(array($identifier), $savedTagToIdentifiersMemberArray);
382  }
383 
388  $this->setUpBackend();
389  $this->setUpRedis();
390  $firstIdentifier = $this->getUniqueId('identifier1-');
391  $tag = 'thisTag';
392  $this->backend->set($firstIdentifier, 'data', array($tag));
393  $secondIdentifier = $this->getUniqueId('identifier2-');
394  $this->backend->set($secondIdentifier, 'data', array($tag));
395  $savedTagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
396  sort($savedTagToIdentifiersMemberArray);
397  $identifierArray = array($firstIdentifier, $secondIdentifier);
398  sort($identifierArray);
399  $this->assertSame(array($firstIdentifier, $secondIdentifier), $savedTagToIdentifiersMemberArray);
400  }
401 
406  $this->setUpBackend();
407  $this->setUpRedis();
408  $identifier = $this->getUniqueId('identifier');
409  $tag = 'thisTag';
410  $this->backend->set($identifier, 'data', array($tag));
411  $this->backend->set($identifier, 'data', array());
412  $savedTagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
413  $this->assertSame(array(), $savedTagToIdentifiersMemberArray);
414  }
415 
420  $this->setUpBackend();
421  $this->setUpRedis();
422  $identifier = $this->getUniqueId('identifier');
423  $this->backend->set($identifier, 'data');
424  $tag = 'thisTag';
425  $this->backend->set($identifier, 'data', array($tag));
426  $savedTagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
427  $this->assertSame(array($identifier), $savedTagToIdentifiersMemberArray);
428  }
429 
434  $this->setUpBackend(array(
435  'compression' => TRUE
436  ));
437  $this->setUpRedis();
438  $identifier = $this->getUniqueId('identifier');
439  $data = 'some data ' . microtime();
440  $this->backend->set($identifier, $data);
441  $uncompresedStoredData = '';
442  try {
443  $uncompresedStoredData = @gzuncompress($this->redis->get(('identData:' . $identifier)));
444  } catch (\Exception $e) {
445 
446  }
447  $this->assertEquals($data, $uncompresedStoredData, 'Original and compressed data don\'t match');
448  }
449 
454  $this->setUpBackend(array(
455  'compression' => TRUE,
456  'compressionLevel' => 0
457  ));
458  $this->setUpRedis();
459  $identifier = $this->getUniqueId('identifier');
460  $data = 'some data ' . microtime();
461  $this->backend->set($identifier, $data);
462  $this->assertGreaterThan(0, substr_count($this->redis->get('identData:' . $identifier), $data), 'Plaintext data not found');
463  }
464 
470  $this->setUpBackend();
471  $this->backend->has(array());
472  }
473 
478  $this->setUpBackend();
479  $identifier = $this->getUniqueId('identifier');
480  $this->assertFalse($this->backend->has($identifier));
481  }
482 
487  $this->setUpBackend();
488  $identifier = $this->getUniqueId('identifier');
489  $this->backend->set($identifier, 'data');
490  $this->assertTrue($this->backend->has($identifier));
491  }
492 
498  $this->setUpBackend();
499  $this->backend->get(array());
500  }
501 
506  $this->setUpBackend(array(
507  'compression' => TRUE
508  ));
509  $data = 'data';
510  $identifier = $this->getUniqueId('identifier');
511  $this->backend->set($identifier, $data);
512  $fetchedData = $this->backend->get($identifier);
513  $this->assertSame($data, $fetchedData);
514  }
515 
519  public function getReturnsPreviouslySetEntry() {
520  $this->setUpBackend();
521  $data = 'data';
522  $identifier = $this->getUniqueId('identifier');
523  $this->backend->set($identifier, $data);
524  $fetchedData = $this->backend->get($identifier);
525  $this->assertSame($data, $fetchedData);
526  }
527 
533  $this->setUpBackend();
534  $this->backend->remove(array());
535  }
536 
541  $this->setUpBackend();
542  $this->assertFalse($this->backend->remove($this->getUniqueId('identifier')));
543  }
544 
549  $this->setUpBackend();
550  $identifier = $this->getUniqueId('identifier');
551  $this->backend->set($identifier, 'data');
552  $this->assertTrue($this->backend->remove($identifier));
553  }
554 
558  public function removeDeletesEntryFromCache() {
559  $this->setUpBackend();
560  $identifier = $this->getUniqueId('identifier');
561  $this->backend->set($identifier, 'data');
562  $this->backend->remove($identifier);
563  $this->assertFalse($this->backend->has($identifier));
564  }
565 
570  $this->setUpBackend();
571  $this->setUpRedis();
572  $identifier = $this->getUniqueId('identifier');
573  $tag = 'thisTag';
574  $this->backend->set($identifier, 'data', array($tag));
575  $this->backend->remove($identifier);
576  $this->assertFalse($this->redis->exists('identTags:' . $identifier));
577  }
578 
583  $this->setUpBackend();
584  $this->setUpRedis();
585  $identifier = $this->getUniqueId('identifier');
586  $tag = 'thisTag';
587  $this->backend->set($identifier, 'data', array($tag));
588  $this->backend->remove($identifier);
589  $tagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
590  $this->assertSame(array(), $tagToIdentifiersMemberArray);
591  }
592 
597  $this->setUpBackend();
598  $this->setUpRedis();
599  $firstIdentifier = $this->getUniqueId('identifier');
600  $secondIdentifier = $this->getUniqueId('identifier');
601  $tag = 'thisTag';
602  $this->backend->set($firstIdentifier, 'data', array($tag));
603  $this->backend->set($secondIdentifier, 'data', array($tag));
604  $this->backend->remove($firstIdentifier);
605  $tagToIdentifiersMemberArray = $this->redis->sMembers('tagIdents:' . $tag);
606  $this->assertSame(array($secondIdentifier), $tagToIdentifiersMemberArray);
607  }
608 
614  $this->setUpBackend();
615  $this->backend->findIdentifiersByTag(array());
616  }
617 
622  $this->setUpBackend();
623  $this->assertSame(array(), $this->backend->findIdentifiersByTag('thisTag'));
624  }
625 
630  $this->setUpBackend();
631  $firstIdentifier = $this->getUniqueId('identifier1-');
632  $secondIdentifier = $this->getUniqueId('identifier2-');
633  $thirdIdentifier = $this->getUniqueId('identifier3-');
634  $tagsForFirstIdentifier = array('thisTag');
635  $tagsForSecondIdentifier = array('thatTag');
636  $tagsForThirdIdentifier = array('thisTag', 'thatTag');
637  $this->backend->set($firstIdentifier, 'data', $tagsForFirstIdentifier);
638  $this->backend->set($secondIdentifier, 'data', $tagsForSecondIdentifier);
639  $this->backend->set($thirdIdentifier, 'data', $tagsForThirdIdentifier);
640  $expectedResult = array($firstIdentifier, $thirdIdentifier);
641  $actualResult = $this->backend->findIdentifiersByTag('thisTag');
642  sort($actualResult);
643  $this->assertSame($expectedResult, $actualResult);
644  }
645 
650  $this->setUpBackend();
651  $this->setUpRedis();
652  $identifier = $this->getUniqueId('identifier');
653  $this->backend->set($identifier, 'data');
654  $this->backend->flush();
655  $this->assertSame(array(), $this->redis->getKeys('*'));
656  }
657 
663  $this->setUpBackend();
664  $this->backend->flushByTag(array());
665  }
666 
671  $this->setUpBackend();
672  $identifier = $this->getUniqueId('identifier');
673  $this->backend->set($identifier . 'A', 'data', array('tag1'));
674  $this->backend->set($identifier . 'B', 'data', array('tag2'));
675  $this->backend->set($identifier . 'C', 'data', array('tag1', 'tag2'));
676  $this->backend->flushByTag('tag1');
677  $expectedResult = array(FALSE, TRUE, FALSE);
678  $actualResult = array(
679  $this->backend->has($identifier . 'A'),
680  $this->backend->has($identifier . 'B'),
681  $this->backend->has($identifier . 'C')
682  );
683  $this->assertSame($expectedResult, $actualResult);
684  }
685 
689  public function flushByTagRemovesTemporarySet() {
690  $this->setUpBackend();
691  $this->setUpRedis();
692  $identifier = $this->getUniqueId('identifier');
693  $this->backend->set($identifier . 'A', 'data', array('tag1'));
694  $this->backend->set($identifier . 'C', 'data', array('tag1', 'tag2'));
695  $this->backend->flushByTag('tag1');
696  $this->assertSame(array(), $this->redis->getKeys('temp*'));
697  }
698 
703  $this->setUpBackend();
704  $this->setUpRedis();
705  $identifier = $this->getUniqueId('identifier');
706  $tag = 'tag1';
707  $this->backend->set($identifier, 'data', array($tag));
708  $this->backend->flushByTag($tag);
709  $this->assertFalse($this->redis->exists('identTags:' . $identifier));
710  }
711 
716  $this->setUpBackend();
717  $this->setUpRedis();
718  $identifierToBeRemoved = $this->getUniqueId('identifier');
719  $tagToRemove = 'tag1';
720  $this->backend->set($identifierToBeRemoved, 'data', array($tagToRemove));
721  $identifierNotToBeRemoved = $this->getUniqueId('identifier');
722  $tagNotToRemove = 'tag2';
723  $this->backend->set($identifierNotToBeRemoved, 'data', array($tagNotToRemove));
724  $this->backend->flushByTag($tagToRemove);
725  $this->assertSame(array($tagNotToRemove), $this->redis->sMembers('identTags:' . $identifierNotToBeRemoved));
726  }
727 
732  $this->setUpBackend();
733  $this->setUpRedis();
734  $identifier = $this->getUniqueId('identifier');
735  $tag = 'tag1';
736  $this->backend->set($identifier, 'data', array($tag));
737  $this->backend->flushByTag($tag);
738  $this->assertFalse($this->redis->exists('tagIdents:' . $tag));
739  }
740 
745  $this->setUpBackend();
746  $this->setUpRedis();
747  $identifier = $this->getUniqueId('identifier');
748  $this->backend->set($identifier . 'A', 'data', array('tag1', 'tag2'));
749  $this->backend->set($identifier . 'B', 'data', array('tag1', 'tag2'));
750  $this->backend->set($identifier . 'C', 'data', array('tag2'));
751  $this->backend->flushByTag('tag1');
752  $this->assertSame(array($identifier . 'C'), $this->redis->sMembers('tagIdents:tag2'));
753  }
754 
759  $this->setUpBackend();
760  $this->setUpRedis();
761  $identifier = $this->getUniqueId('identifier');
762  $this->backend->set($identifier . 'A', 'data', array('tag'));
763  $this->backend->set($identifier . 'B', 'data', array('tag'));
764  $this->redis->delete('identData:' . $identifier . 'A');
765  $this->backend->collectGarbage();
766  $this->assertTrue($this->redis->exists('identData:' . $identifier . 'B'));
767  }
768 
773  $this->setUpBackend();
774  $this->setUpRedis();
775  $identifier = $this->getUniqueId('identifier');
776  $this->backend->set($identifier . 'A', 'data', array('tag'));
777  $this->backend->set($identifier . 'B', 'data', array('tag'));
778  $this->redis->delete('identData:' . $identifier . 'A');
779  $this->backend->collectGarbage();
780  $expectedResult = array(FALSE, TRUE);
781  $actualResult = array(
782  $this->redis->exists('identTags:' . $identifier . 'A'),
783  $this->redis->exists('identTags:' . $identifier . 'B')
784  );
785  $this->assertSame($expectedResult, $actualResult);
786  }
787 
792  $this->setUpBackend();
793  $this->setUpRedis();
794  $identifier = $this->getUniqueId('identifier');
795  $this->backend->set($identifier . 'A', 'data', array('tag1', 'tag2'));
796  $this->backend->set($identifier . 'B', 'data', array('tag2'));
797  $this->redis->delete('identData:' . $identifier . 'A');
798  $this->backend->collectGarbage();
799  $expectedResult = array(
800  array(),
801  array($identifier . 'B')
802  );
803  $actualResult = array(
804  $this->redis->sMembers('tagIdents:tag1'),
805  $this->redis->sMembers('tagIdents:tag2')
806  );
807  $this->assertSame($expectedResult, $actualResult);
808  }
809 
810 }