getCache(); if (! $cache) { return parent::get($columns); } if (! $this->hasColumns()) { $this->columns = $columns; } $key = $this->getCacheKey(); if (($result = $cache->get($key)) !== false) { return $result; } $result = parent::get($columns); if ($result !== false) { $cache->set($key, $result, $this->cacheSeconds); } return $result; } /** * Get cache instance * @param mixed $tags * @return \Psr\SimpleCache\CacheInterface|bool */ protected function getCache($tags = null) { return bff::database()->cache( $this->cacheStore, $tags ?? $this->cacheTags ); } /** * Flush cache for current model or a given tag name * @param mixed $tags * @return bool */ public function flushCache($tags = null) { $this->getCache($tags)->flush(); return true; } /** * Set cache store. * * @param string $store * @return static */ public function cacheStore(string $store) { $this->cacheStore = $store; return $this; } /** * Generate cache key for "select" query. * * @return string */ protected function getCacheKey() { $name = $this->connection->getName(); return ($this->cachePrefix ? $this->cachePrefix : '') . bff::database()->cacheKey($name . $this->toSql() . serialize($this->getBindings())); } /** * Cache query results * * @param int $seconds * @param array|null $tags * @return static */ public function cache(int $seconds = 0, ?array $tags = null) { $this->cacheSeconds = $seconds; if (! empty($tags)) { $this->cacheTags = $tags; } return $this; } /** * Don't cache * * @return static */ public function dontCache() { $this->cacheSeconds = null; $this->cacheTags = null; return $this; } }