RedisExpireTtl0.test.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
  4. * @author Georges.L (Geolim4) <contact@geolim4.com>
  5. */
  6. use phpFastCache\CacheManager;
  7. use phpFastCache\Helper\TestHelper;
  8. chdir(__DIR__);
  9. require_once __DIR__ . '/../vendor/autoload.php';
  10. $testHelper = new TestHelper('(P)Redis Expire TTL to 0');
  11. $cacheInstance = CacheManager::getInstance('Redis', []);
  12. $cacheKey = 'cacheKey';
  13. $RandomCacheValue = str_shuffle(uniqid('pfc', true));
  14. $loops = 10;
  15. $testHelper->printText('See https://redis.io/commands/setex');
  16. $testHelper->printText('See https://redis.io/commands/expire');
  17. $testHelper->printNewLine();
  18. for ($i = 0; $i <= $loops; $i++)
  19. {
  20. $cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");
  21. $cacheItem->set($RandomCacheValue)
  22. ->expiresAt(new DateTime());
  23. $cacheInstance->saveDeferred($cacheItem);
  24. }
  25. try{
  26. $cacheInstance->commit();
  27. $testHelper->printPassText('The COMMIT operation has finished successfully');
  28. }catch (Predis\Response\ServerException $e){
  29. if(strpos($e->getMessage(), 'setex')){
  30. $testHelper->printFailText('The COMMIT operation has failed due to to an invalid time detection.');
  31. }else{
  32. $testHelper->printFailText('The COMMIT operation has failed due to to an unexpected error: ' . $e->getMessage());
  33. }
  34. }
  35. $cacheInstance->detachAllItems();
  36. $testHelper->printText('Sleeping a second...');
  37. sleep(1);
  38. for ($i = 0; $i <= $loops; $i++)
  39. {
  40. $cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");
  41. if($cacheItem->isHit()){
  42. $testHelper->printFailText(sprintf('The cache item "%s" is considered as HIT with the following value: %s', $cacheItem->getKey(), $cacheItem->get()));
  43. }else{
  44. $testHelper->printPassText(sprintf('The cache item "%s" is not considered as HIT.', $cacheItem->getKey()));
  45. }
  46. }
  47. $cacheInstance->clear();
  48. $testHelper->terminateTest();