FilesystemCache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. /**
  21. * Filesystem cache driver.
  22. *
  23. * @since 2.3
  24. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  25. */
  26. class FilesystemCache extends FileCache
  27. {
  28. const EXTENSION = '.doctrinecache.data';
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
  33. {
  34. parent::__construct($directory, $extension, $umask);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch($id)
  40. {
  41. $data = '';
  42. $lifetime = -1;
  43. $filename = $this->getFilename($id);
  44. if ( ! is_file($filename)) {
  45. return false;
  46. }
  47. $resource = fopen($filename, "r");
  48. if (false !== ($line = fgets($resource))) {
  49. $lifetime = (int) $line;
  50. }
  51. if ($lifetime !== 0 && $lifetime < time()) {
  52. fclose($resource);
  53. return false;
  54. }
  55. while (false !== ($line = fgets($resource))) {
  56. $data .= $line;
  57. }
  58. fclose($resource);
  59. return unserialize($data);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function doContains($id)
  65. {
  66. $lifetime = -1;
  67. $filename = $this->getFilename($id);
  68. if ( ! is_file($filename)) {
  69. return false;
  70. }
  71. $resource = fopen($filename, "r");
  72. if (false !== ($line = fgets($resource))) {
  73. $lifetime = (int) $line;
  74. }
  75. fclose($resource);
  76. return $lifetime === 0 || $lifetime > time();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function doSave($id, $data, $lifeTime = 0)
  82. {
  83. if ($lifeTime > 0) {
  84. $lifeTime = time() + $lifeTime;
  85. }
  86. $data = serialize($data);
  87. $filename = $this->getFilename($id);
  88. return $this->writeFile($filename, $lifeTime . PHP_EOL . $data);
  89. }
  90. }