From 78584c7128d678272861d62b80c9ab99ffa0b0a3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 26 Oct 2017 19:56:03 +0200 Subject: [PATCH] Add cache to search. --- app/Http/Controllers/SearchController.php | 25 ++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 39acf95596..0ba7e68a7b 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Controllers; +use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Search\SearchInterface; use Illuminate\Http\Request; use Response; @@ -75,11 +76,25 @@ class SearchController extends Controller { $fullQuery = strval($request->get('query')); - // parse search terms: - $searcher->parseQuery($fullQuery); - $searcher->setLimit(20); - $transactions = $searcher->searchTransactions(); - $html = view('search.search', compact('transactions'))->render(); + // cache + $cache = new CacheProperties; + $cache->addProperty('search'); + $cache->addProperty($fullQuery); + + if ($cache->has()) { + $transactions = $cache->get(); + } + + if (!$cache->has()) { + // parse search terms: + $searcher->parseQuery($fullQuery); + $searcher->setLimit(20); + $transactions = $searcher->searchTransactions(); + $cache->store($transactions); + } + + + $html = view('search.search', compact('transactions'))->render(); return Response::json(['count' => $transactions->count(), 'html' => $html]);