PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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