FileDriver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * The cache library of zentaopms.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Lu Fei <lufei@easycorp.ltd>
  8. * @package cache
  9. * @link https://www.zentao.net
  10. */
  11. namespace ZenTao\Cache\Driver;
  12. use ZenTao\Cache\SimpleCache\CacheInterface;
  13. use ZenTao\Cache\SimpleCache\InvalidArgumentException;
  14. class FileDriver implements CacheInterface
  15. {
  16. /**
  17. * The file cache directory.
  18. *
  19. * @var string
  20. */
  21. protected $directory;
  22. /**
  23. * @var string
  24. */
  25. private $namespace;
  26. /**
  27. * @var int
  28. */
  29. private $defaultLifetime;
  30. public function __construct($namespace = '', $defaultLifetime = 0, $directory = '')
  31. {
  32. $this->namespace = $namespace;
  33. $this->defaultLifetime = $defaultLifetime;
  34. $this->directory = $directory;
  35. if(!is_dir($this->getPrefix()) && is_writable($this->directory)) mkdir($this->getPrefix(), 0777, true);
  36. }
  37. /**
  38. * @param string $key
  39. */
  40. public function getCacheKey($key)
  41. {
  42. return $this->getPrefix() . DS . md5($key) . '.cache';
  43. }
  44. public function get($key, $default = null)
  45. {
  46. $file = $this->getCacheKey($key);
  47. if(!file_exists($file)) return $default;
  48. $content = unserialize(file_get_contents($file));
  49. if($this->isExpired($content))
  50. {
  51. $this->delete($key);
  52. return $default;
  53. }
  54. return $content['data'];
  55. }
  56. public function set($key, $value, $ttl = null)
  57. {
  58. $file = $this->getCacheKey($key);
  59. $content = serialize($this->generatePayload($value, $ttl));
  60. $result = file_put_contents($file, $content);
  61. return (bool)$result;
  62. }
  63. public function delete($key)
  64. {
  65. $file = $this->getCacheKey($key);
  66. if(file_exists($file))
  67. {
  68. if(!is_writable($file)) return false;
  69. unlink($file);
  70. }
  71. return true;
  72. }
  73. public function clear()
  74. {
  75. return $this->clearPrefix('');
  76. }
  77. public function getMultiple($keys, $default = null)
  78. {
  79. if(!is_array($keys)) throw new InvalidArgumentException('The keys is invalid!');
  80. $result = array();
  81. foreach($keys as $key) $result[$key] = $this->get($key, $default);
  82. return $result;
  83. }
  84. public function setMultiple($values, $ttl = null)
  85. {
  86. if(!is_array($values)) throw new InvalidArgumentException('The values is invalid!');
  87. $time = $this->genLifeTime($ttl);
  88. foreach($values as $key => $value) $this->set($key, $value, $time);
  89. return true;
  90. }
  91. public function deleteMultiple($keys)
  92. {
  93. if(!is_array($keys)) throw new InvalidArgumentException('The keys is invalid!');
  94. foreach($keys as $key) $this->delete($key);
  95. return true;
  96. }
  97. public function has($key)
  98. {
  99. $file = $this->getCacheKey($key);
  100. if(!file_exists($file)) return false;
  101. $content = unserialize(file_get_contents($file));
  102. if($this->isExpired($content))
  103. {
  104. $this->delete($key);
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. * @param string $prefix
  111. */
  112. public function clearPrefix($prefix)
  113. {
  114. $files = glob($this->getPrefix() . DS . $prefix . '*');
  115. foreach($files as $file)
  116. {
  117. if(is_dir($file)) continue;
  118. unlink($file);
  119. }
  120. return true;
  121. }
  122. protected function isExpired($payload)
  123. {
  124. if(is_null($payload['time'])) return false;
  125. return time() > $payload['time'];
  126. }
  127. protected function generatePayload($value, $ttl = null)
  128. {
  129. $time = $this->genLifeTime($ttl);
  130. return array('data' => $value, 'time' => $time);
  131. }
  132. protected function genLifeTime($ttl = null)
  133. {
  134. if(is_null($ttl)) return time() + $this->defaultLifetime;
  135. if($ttl > time()) return $ttl;
  136. return time() + $ttl;
  137. }
  138. protected function getPrefix()
  139. {
  140. return $this->directory . $this->namespace;
  141. }
  142. }