diff --git a/src/Database/QueryBuilder.php b/src/Database/QueryBuilder.php index 5324dc435..73f637ebe 100644 --- a/src/Database/QueryBuilder.php +++ b/src/Database/QueryBuilder.php @@ -1,7 +1,5 @@ pluck($column, $key)->all(); } - /** - * Indicate that the query results should be cached. - * - * @param \DateTime|int $minutes - * @param string $key - * @return $this - */ - public function remember($minutes, $key = null) - { - $this->cacheMinutes = $minutes; - $this->cacheKey = $key; - - return $this; - } - - /** - * Indicate that the query results should be cached forever. - * - * @param string $key - * @return \Illuminate\Database\Query\Builder|static - */ - public function rememberForever($key = null) - { - return $this->remember(-1, $key); - } - - /** - * Indicate that the results, if cached, should use the given cache tags. - * - * @param array|mixed $cacheTags - * @return $this - */ - public function cacheTags($cacheTags) - { - $this->cacheTags = $cacheTags; - return $this; - } - - /** - * @inheritDoc - */ - public function get($columns = ['*']) - { - if (!is_null($this->cacheMinutes)) { - return $this->getCached((array) $columns); - } - - return parent::get($columns); - } - - /** - * getCached executes the query as a cached "select" statement. - */ - public function getCached(array $columns = ['*']) - { - if (is_null($this->columns)) { - $this->columns = $columns; - } - - // If the query is requested to be cached, we will cache it using a unique key - // for this database connection and query statement, including the bindings - // that are used on this query, providing great convenience when caching. - [$key, $minutes] = $this->getCacheInfo(); - - $cache = $this->getCache(); - - $callback = $this->getCacheCallback($columns); - - // If the "minutes" value is less than zero, we will use that as the indicator - // that the value should be remembered values should be stored indefinitely - // and if we have minutes we will use the typical remember function here. - if ($minutes < 0) { - $results = $cache->rememberForever($key, $callback); - } - else { - $expiresAt = Carbon::now()->addMinutes($minutes); - $results = $cache->remember($key, $expiresAt, $callback); - } - - return collect($results); - } - - /** - * Get the cache object with tags assigned, if applicable. - * - * @return \Illuminate\Cache\CacheManager - */ - protected function getCache() - { - $cache = App::make('cache'); - - return $this->cacheTags ? $cache->tags($this->cacheTags) : $cache; - } - - /** - * getCacheInfo returns key and cache minutes - */ - protected function getCacheInfo(): array - { - return [$this->getCacheKey(), $this->cacheMinutes]; - } - - /** - * getCacheKey returns a unique cache key for the complete query - */ - public function getCacheKey(): string - { - return $this->cacheKey ?: $this->generateCacheKey(); - } - - /** - * Generate the unique cache key for the query. - * - * @return string - */ - public function generateCacheKey() - { - $name = $this->connection->getName(); - - return md5($name.$this->toSql().serialize($this->getBindings())); - } - - /** - * Get the Closure callback used when caching queries. - * - * @param array $columns - * @return \Closure - */ - protected function getCacheCallback($columns) - { - return function () use ($columns) { - return parent::get($columns)->all(); - }; - } - /** * Retrieve the "count" result of the query, * also strips off any orderBy clause.