PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php

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