FileStore.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Exception;
  4. use Illuminate\Contracts\Cache\Store;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\InteractsWithTime;
  7. class FileStore implements Store
  8. {
  9. use InteractsWithTime, RetrievesMultipleKeys;
  10. /**
  11. * The Illuminate Filesystem instance.
  12. *
  13. * @var \Illuminate\Filesystem\Filesystem
  14. */
  15. protected $files;
  16. /**
  17. * The file cache directory.
  18. *
  19. * @var string
  20. */
  21. protected $directory;
  22. /**
  23. * Create a new file cache store instance.
  24. *
  25. * @param \Illuminate\Filesystem\Filesystem $files
  26. * @param string $directory
  27. * @return void
  28. */
  29. public function __construct(Filesystem $files, $directory)
  30. {
  31. $this->files = $files;
  32. $this->directory = $directory;
  33. }
  34. /**
  35. * Retrieve an item from the cache by key.
  36. *
  37. * @param string|array $key
  38. * @return mixed
  39. */
  40. public function get($key)
  41. {
  42. return $this->getPayload($key)['data'] ?? null;
  43. }
  44. /**
  45. * Store an item in the cache for a given number of minutes.
  46. *
  47. * @param string $key
  48. * @param mixed $value
  49. * @param float|int $minutes
  50. * @return void
  51. */
  52. public function put($key, $value, $minutes)
  53. {
  54. $this->ensureCacheDirectoryExists($path = $this->path($key));
  55. $this->files->put(
  56. $path, $this->expiration($minutes).serialize($value), true
  57. );
  58. }
  59. /**
  60. * Create the file cache directory if necessary.
  61. *
  62. * @param string $path
  63. * @return void
  64. */
  65. protected function ensureCacheDirectoryExists($path)
  66. {
  67. if (! $this->files->exists(dirname($path))) {
  68. $this->files->makeDirectory(dirname($path), 0777, true, true);
  69. }
  70. }
  71. /**
  72. * Increment the value of an item in the cache.
  73. *
  74. * @param string $key
  75. * @param mixed $value
  76. * @return int
  77. */
  78. public function increment($key, $value = 1)
  79. {
  80. $raw = $this->getPayload($key);
  81. return tap(((int) $raw['data']) + $value, function ($newValue) use ($key, $raw) {
  82. $this->put($key, $newValue, $raw['time'] ?? 0);
  83. });
  84. }
  85. /**
  86. * Decrement the value of an item in the cache.
  87. *
  88. * @param string $key
  89. * @param mixed $value
  90. * @return int
  91. */
  92. public function decrement($key, $value = 1)
  93. {
  94. return $this->increment($key, $value * -1);
  95. }
  96. /**
  97. * Store an item in the cache indefinitely.
  98. *
  99. * @param string $key
  100. * @param mixed $value
  101. * @return void
  102. */
  103. public function forever($key, $value)
  104. {
  105. $this->put($key, $value, 0);
  106. }
  107. /**
  108. * Remove an item from the cache.
  109. *
  110. * @param string $key
  111. * @return bool
  112. */
  113. public function forget($key)
  114. {
  115. if ($this->files->exists($file = $this->path($key))) {
  116. return $this->files->delete($file);
  117. }
  118. return false;
  119. }
  120. /**
  121. * Remove all items from the cache.
  122. *
  123. * @return bool
  124. */
  125. public function flush()
  126. {
  127. if (! $this->files->isDirectory($this->directory)) {
  128. return false;
  129. }
  130. foreach ($this->files->directories($this->directory) as $directory) {
  131. if (! $this->files->deleteDirectory($directory)) {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. /**
  138. * Retrieve an item and expiry time from the cache by key.
  139. *
  140. * @param string $key
  141. * @return array
  142. */
  143. protected function getPayload($key)
  144. {
  145. $path = $this->path($key);
  146. // If the file doesn't exist, we obviously cannot return the cache so we will
  147. // just return null. Otherwise, we'll get the contents of the file and get
  148. // the expiration UNIX timestamps from the start of the file's contents.
  149. try {
  150. $expire = substr(
  151. $contents = $this->files->get($path, true), 0, 10
  152. );
  153. } catch (Exception $e) {
  154. return $this->emptyPayload();
  155. }
  156. // If the current time is greater than expiration timestamps we will delete
  157. // the file and return null. This helps clean up the old files and keeps
  158. // this directory much cleaner for us as old files aren't hanging out.
  159. if ($this->currentTime() >= $expire) {
  160. $this->forget($key);
  161. return $this->emptyPayload();
  162. }
  163. $data = unserialize(substr($contents, 10));
  164. // Next, we'll extract the number of minutes that are remaining for a cache
  165. // so that we can properly retain the time for things like the increment
  166. // operation that may be performed on this cache on a later operation.
  167. $time = ($expire - $this->currentTime()) / 60;
  168. return compact('data', 'time');
  169. }
  170. /**
  171. * Get a default empty payload for the cache.
  172. *
  173. * @return array
  174. */
  175. protected function emptyPayload()
  176. {
  177. return ['data' => null, 'time' => null];
  178. }
  179. /**
  180. * Get the full path for the given cache key.
  181. *
  182. * @param string $key
  183. * @return string
  184. */
  185. protected function path($key)
  186. {
  187. $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
  188. return $this->directory.'/'.implode('/', $parts).'/'.$hash;
  189. }
  190. /**
  191. * Get the expiration time based on the given minutes.
  192. *
  193. * @param float|int $minutes
  194. * @return int
  195. */
  196. protected function expiration($minutes)
  197. {
  198. $time = $this->availableAt((int) ($minutes * 60));
  199. return $minutes === 0 || $time > 9999999999 ? 9999999999 : (int) $time;
  200. }
  201. /**
  202. * Get the Filesystem instance.
  203. *
  204. * @return \Illuminate\Filesystem\Filesystem
  205. */
  206. public function getFilesystem()
  207. {
  208. return $this->files;
  209. }
  210. /**
  211. * Get the working directory of the cache.
  212. *
  213. * @return string
  214. */
  215. public function getDirectory()
  216. {
  217. return $this->directory;
  218. }
  219. /**
  220. * Get the cache key prefix.
  221. *
  222. * @return string
  223. */
  224. public function getPrefix()
  225. {
  226. return '';
  227. }
  228. }