PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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