Add cache to search.

This commit is contained in:
James Cole 2017-10-26 19:56:03 +02:00
parent 1217ff7a7f
commit 78584c7128
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E

View File

@ -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]);