FilesystemCache.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. protected $extension = self::EXTENSION;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function doFetch($id)
  37. {
  38. $data = '';
  39. $lifetime = -1;
  40. $filename = $this->getFilename($id);
  41. if ( ! is_file($filename)) {
  42. return false;
  43. }
  44. $resource = fopen($filename, "r");
  45. if (false !== ($line = fgets($resource))) {
  46. $lifetime = (integer) $line;
  47. }
  48. if ($lifetime !== 0 && $lifetime < time()) {
  49. fclose($resource);
  50. return false;
  51. }
  52. while (false !== ($line = fgets($resource))) {
  53. $data .= $line;
  54. }
  55. fclose($resource);
  56. return unserialize($data);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doContains($id)
  62. {
  63. $lifetime = -1;
  64. $filename = $this->getFilename($id);
  65. if ( ! is_file($filename)) {
  66. return false;
  67. }
  68. $resource = fopen($filename, "r");
  69. if (false !== ($line = fgets($resource))) {
  70. $lifetime = (integer) $line;
  71. }
  72. fclose($resource);
  73. return $lifetime === 0 || $lifetime > time();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function doSave($id, $data, $lifeTime = 0)
  79. {
  80. if ($lifeTime > 0) {
  81. $lifeTime = time() + $lifeTime;
  82. }
  83. $data = serialize($data);
  84. $filename = $this->getFilename($id);
  85. $filepath = pathinfo($filename, PATHINFO_DIRNAME);
  86. if ( ! is_dir($filepath)) {
  87. mkdir($filepath, 0777, true);
  88. }
  89. return file_put_contents($filename, $lifeTime . PHP_EOL . $data);
  90. }
  91. }