PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/ealexis.t/trends
PHP | 266 lines | 115 code | 37 blank | 114 comment | 6 complexity | ec56c3117ce3acc8a0eae5c00b002ad0 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Closure;
  4. use Exception;
  5. use Illuminate\Contracts\Cache\Store;
  6. use Illuminate\Database\ConnectionInterface;
  7. use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
  8. class DatabaseStore implements Store
  9. {
  10. use RetrievesMultipleKeys;
  11. /**
  12. * The database connection instance.
  13. *
  14. * @var \Illuminate\Database\ConnectionInterface
  15. */
  16. protected $connection;
  17. /**
  18. * The encrypter instance.
  19. *
  20. * @var \Illuminate\Contracts\Encryption\Encrypter
  21. */
  22. protected $encrypter;
  23. /**
  24. * The name of the cache table.
  25. *
  26. * @var string
  27. */
  28. protected $table;
  29. /**
  30. * A string that should be prepended to keys.
  31. *
  32. * @var string
  33. */
  34. protected $prefix;
  35. /**
  36. * Create a new database store.
  37. *
  38. * @param \Illuminate\Database\ConnectionInterface $connection
  39. * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
  40. * @param string $table
  41. * @param string $prefix
  42. * @return void
  43. */
  44. public function __construct(ConnectionInterface $connection, EncrypterContract $encrypter, $table, $prefix = '')
  45. {
  46. $this->table = $table;
  47. $this->prefix = $prefix;
  48. $this->encrypter = $encrypter;
  49. $this->connection = $connection;
  50. }
  51. /**
  52. * Retrieve an item from the cache by key.
  53. *
  54. * @param string|array $key
  55. * @return mixed
  56. */
  57. public function get($key)
  58. {
  59. $prefixed = $this->prefix.$key;
  60. $cache = $this->table()->where('key', '=', $prefixed)->first();
  61. // If we have a cache record we will check the expiration time against current
  62. // time on the system and see if the record has expired. If it has, we will
  63. // remove the records from the database table so it isn't returned again.
  64. if (! is_null($cache)) {
  65. if (is_array($cache)) {
  66. $cache = (object) $cache;
  67. }
  68. if (time() >= $cache->expiration) {
  69. $this->forget($key);
  70. return;
  71. }
  72. return $this->encrypter->decrypt($cache->value);
  73. }
  74. }
  75. /**
  76. * Store an item in the cache for a given number of minutes.
  77. *
  78. * @param string $key
  79. * @param mixed $value
  80. * @param int $minutes
  81. * @return void
  82. */
  83. public function put($key, $value, $minutes)
  84. {
  85. $key = $this->prefix.$key;
  86. // All of the cached values in the database are encrypted in case this is used
  87. // as a session data store by the consumer. We'll also calculate the expire
  88. // time and place that on the table so we will check it on our retrieval.
  89. $value = $this->encrypter->encrypt($value);
  90. $expiration = $this->getTime() + ($minutes * 60);
  91. try {
  92. $this->table()->insert(compact('key', 'value', 'expiration'));
  93. } catch (Exception $e) {
  94. $this->table()->where('key', '=', $key)->update(compact('value', 'expiration'));
  95. }
  96. }
  97. /**
  98. * Increment the value of an item in the cache.
  99. *
  100. * @param string $key
  101. * @param mixed $value
  102. * @return int|bool
  103. */
  104. public function increment($key, $value = 1)
  105. {
  106. return $this->incrementOrDecrement($key, $value, function ($current, $value) {
  107. return $current + $value;
  108. });
  109. }
  110. /**
  111. * Increment the value of an item in the cache.
  112. *
  113. * @param string $key
  114. * @param mixed $value
  115. * @return int|bool
  116. */
  117. public function decrement($key, $value = 1)
  118. {
  119. return $this->incrementOrDecrement($key, $value, function ($current, $value) {
  120. return $current - $value;
  121. });
  122. }
  123. /**
  124. * Increment or decrement an item in the cache.
  125. *
  126. * @param string $key
  127. * @param mixed $value
  128. * @param \Closure $callback
  129. * @return int|bool
  130. */
  131. protected function incrementOrDecrement($key, $value, Closure $callback)
  132. {
  133. return $this->connection->transaction(function () use ($key, $value, $callback) {
  134. $prefixed = $this->prefix.$key;
  135. $cache = $this->table()->where('key', $prefixed)->lockForUpdate()->first();
  136. if (is_null($cache)) {
  137. return false;
  138. }
  139. if (is_array($cache)) {
  140. $cache = (object) $cache;
  141. }
  142. $current = $this->encrypter->decrypt($cache->value);
  143. $new = $callback($current, $value);
  144. if (! is_numeric($current)) {
  145. return false;
  146. }
  147. $this->table()->where('key', $prefixed)->update([
  148. 'value' => $this->encrypter->encrypt($new),
  149. ]);
  150. return $new;
  151. });
  152. }
  153. /**
  154. * Get the current system time.
  155. *
  156. * @return int
  157. */
  158. protected function getTime()
  159. {
  160. return time();
  161. }
  162. /**
  163. * Store an item in the cache indefinitely.
  164. *
  165. * @param string $key
  166. * @param mixed $value
  167. * @return void
  168. */
  169. public function forever($key, $value)
  170. {
  171. $this->put($key, $value, 5256000);
  172. }
  173. /**
  174. * Remove an item from the cache.
  175. *
  176. * @param string $key
  177. * @return bool
  178. */
  179. public function forget($key)
  180. {
  181. $this->table()->where('key', '=', $this->prefix.$key)->delete();
  182. return true;
  183. }
  184. /**
  185. * Remove all items from the cache.
  186. *
  187. * @return void
  188. */
  189. public function flush()
  190. {
  191. $this->table()->delete();
  192. }
  193. /**
  194. * Get a query builder for the cache table.
  195. *
  196. * @return \Illuminate\Database\Query\Builder
  197. */
  198. protected function table()
  199. {
  200. return $this->connection->table($this->table);
  201. }
  202. /**
  203. * Get the underlying database connection.
  204. *
  205. * @return \Illuminate\Database\ConnectionInterface
  206. */
  207. public function getConnection()
  208. {
  209. return $this->connection;
  210. }
  211. /**
  212. * Get the encrypter instance.
  213. *
  214. * @return \Illuminate\Contracts\Encryption\Encrypter
  215. */
  216. public function getEncrypter()
  217. {
  218. return $this->encrypter;
  219. }
  220. /**
  221. * Get the cache key prefix.
  222. *
  223. * @return string
  224. */
  225. public function getPrefix()
  226. {
  227. return $this->prefix;
  228. }
  229. }