MemoryCache.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * This file is part of the Behat Gherkin.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Gherkin\Cache;
  10. use Behat\Gherkin\Node\FeatureNode;
  11. /**
  12. * Memory cache.
  13. * Caches feature into a memory.
  14. *
  15. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  16. */
  17. class MemoryCache implements CacheInterface
  18. {
  19. private $features = array();
  20. private $timestamps = array();
  21. /**
  22. * Checks that cache for feature exists and is fresh.
  23. *
  24. * @param string $path Feature path
  25. * @param integer $timestamp The last time feature was updated
  26. *
  27. * @return Boolean
  28. */
  29. public function isFresh($path, $timestamp)
  30. {
  31. if (!isset($this->features[$path])) {
  32. return false;
  33. }
  34. return $this->timestamps[$path] > $timestamp;
  35. }
  36. /**
  37. * Reads feature cache from path.
  38. *
  39. * @param string $path Feature path
  40. *
  41. * @return FeatureNode
  42. */
  43. public function read($path)
  44. {
  45. return $this->features[$path];
  46. }
  47. /**
  48. * Caches feature node.
  49. *
  50. * @param string $path Feature path
  51. * @param FeatureNode $feature Feature instance
  52. */
  53. public function write($path, FeatureNode $feature)
  54. {
  55. $this->features[$path] = $feature;
  56. $this->timestamps[$path] = time();
  57. }
  58. }