PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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