From 79392ab65669a6da72021b94a140b306f5fbae65 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 26 Dec 2015 08:44:34 +0100 Subject: [PATCH] Add caching to various queries and lists. --- app/Http/Controllers/JsonController.php | 1 - .../Account/AccountRepository.php | 35 +++++++++++++++++-- .../Category/CategoryRepository.php | 9 +++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 85fc33ee1d..026d684bf4 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -244,7 +244,6 @@ class JsonController extends Controller foreach ($list as $entry) { $return[] = $entry->name; } - sort($return); return Response::json($return); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index cdadf440c7..12091470b8 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -78,6 +78,14 @@ class AccountRepository implements AccountRepositoryInterface */ public function getAccounts(array $types) { + $cache = new CacheProperties(); + $cache->addProperty('get-accounts'); + $cache->addProperty($types); + + if ($cache->has()) { + return $cache->get(); + } + /** @var Collection $result */ $result = Auth::user()->accounts()->with( ['accountmeta' => function (HasMany $query) { @@ -91,6 +99,8 @@ class AccountRepository implements AccountRepositoryInterface } ); + $cache->store($result); + return $result; } @@ -120,8 +130,18 @@ class AccountRepository implements AccountRepositoryInterface */ public function getFirstTransaction(TransactionJournal $journal, Account $account) { + $cache = new CacheProperties(); + $cache->addProperty('first-transaction'); + $cache->addProperty($journal->id); + $cache->addProperty($account->id); - return $journal->transactions()->where('account_id', $account->id)->first(); + if ($cache->has()) { + return $cache->get(); + } + $transaction = $journal->transactions()->where('account_id', $account->id)->first(); + $cache->store($transaction); + + return $transaction; } /** @@ -383,12 +403,23 @@ class AccountRepository implements AccountRepositoryInterface */ public function openingBalanceTransaction(Account $account) { - return TransactionJournal + $cache = new CacheProperties; + $cache->addProperty($account->id); + $cache->addProperty('opening-balance-journal'); + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + + $journal = TransactionJournal ::orderBy('transaction_journals.date', 'ASC') ->accountIs($account) ->transactionTypes([TransactionType::OPENING_BALANCE]) ->orderBy('created_at', 'ASC') ->first(['transaction_journals.*']); + $cache->store($journal); + + return $journal; } /** diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index b966737d1f..8a94d61781 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -50,6 +50,13 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito */ public function getCategories() { + $cache = new CacheProperties; + $cache->addProperty('category-list'); + + if($cache->has()) { + return $cache->get(); + } + /** @var Collection $set */ $set = Auth::user()->categories()->orderBy('name', 'ASC')->get(); $set = $set->sortBy( @@ -58,6 +65,8 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito } ); + $cache->store($set); + return $set; }