database.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php namespace Laravel\Cache\Drivers;
  2. use Laravel\Config;
  3. use Laravel\Database as DB;
  4. use Laravel\Database\Connection;
  5. class Database extends Driver {
  6. /**
  7. * The cache key from the cache configuration file.
  8. *
  9. * @var string
  10. */
  11. protected $key;
  12. /**
  13. * Create a new database cache driver instance.
  14. *
  15. * @param string $key
  16. * @return void
  17. */
  18. public function __construct($key)
  19. {
  20. $this->key = $key;
  21. }
  22. /**
  23. * Determine if an item exists in the cache.
  24. *
  25. * @param string $key
  26. * @return bool
  27. */
  28. public function has($key)
  29. {
  30. return ( ! is_null($this->get($key)));
  31. }
  32. /**
  33. * Retrieve an item from the cache driver.
  34. *
  35. * @param string $key
  36. * @return mixed
  37. */
  38. protected function retrieve($key)
  39. {
  40. $cache = $this->table()->where('key', '=', $this->key.$key)->first();
  41. if ( ! is_null($cache))
  42. {
  43. if (time() >= $cache->expiration) return $this->forget($key);
  44. return unserialize($cache->value);
  45. }
  46. }
  47. /**
  48. * Write an item to the cache for a given number of minutes.
  49. *
  50. * <code>
  51. * // Put an item in the cache for 15 minutes
  52. * Cache::put('name', 'Taylor', 15);
  53. * </code>
  54. *
  55. * @param string $key
  56. * @param mixed $value
  57. * @param int $minutes
  58. * @return void
  59. */
  60. public function put($key, $value, $minutes)
  61. {
  62. $key = $this->key.$key;
  63. $value = serialize($value);
  64. $expiration = $this->expiration($minutes);
  65. // To update the value, we'll first attempt an insert against the
  66. // database and if we catch an exception we'll assume that the
  67. // primary key already exists in the table and update.
  68. try
  69. {
  70. $this->table()->insert(compact('key', 'value', 'expiration'));
  71. }
  72. catch (\Exception $e)
  73. {
  74. $this->table()->where('key', '=', $key)->update(compact('value', 'expiration'));
  75. }
  76. }
  77. /**
  78. * Write an item to the cache for five years.
  79. *
  80. * @param string $key
  81. * @param mixed $value
  82. * @return void
  83. */
  84. public function forever($key, $value)
  85. {
  86. return $this->put($key, $value, 2628000);
  87. }
  88. /**
  89. * Delete an item from the cache.
  90. *
  91. * @param string $key
  92. * @return void
  93. */
  94. public function forget($key)
  95. {
  96. $this->table()->where('key', '=', $this->key.$key)->delete();
  97. }
  98. /**
  99. * Get a query builder for the database table.
  100. *
  101. * @return Laravel\Database\Query
  102. */
  103. protected function table()
  104. {
  105. $connection = DB::connection(Config::get('cache.database.connection'));
  106. return $connection->table(Config::get('cache.database.table'));
  107. }
  108. }