Add caching to various queries and lists.

This commit is contained in:
James Cole
2015-12-26 08:44:34 +01:00
parent 3ca1207231
commit 79392ab656
3 changed files with 42 additions and 3 deletions

View File

@@ -244,7 +244,6 @@ class JsonController extends Controller
foreach ($list as $entry) { foreach ($list as $entry) {
$return[] = $entry->name; $return[] = $entry->name;
} }
sort($return);
return Response::json($return); return Response::json($return);
} }

View File

@@ -78,6 +78,14 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function getAccounts(array $types) public function getAccounts(array $types)
{ {
$cache = new CacheProperties();
$cache->addProperty('get-accounts');
$cache->addProperty($types);
if ($cache->has()) {
return $cache->get();
}
/** @var Collection $result */ /** @var Collection $result */
$result = Auth::user()->accounts()->with( $result = Auth::user()->accounts()->with(
['accountmeta' => function (HasMany $query) { ['accountmeta' => function (HasMany $query) {
@@ -91,6 +99,8 @@ class AccountRepository implements AccountRepositoryInterface
} }
); );
$cache->store($result);
return $result; return $result;
} }
@@ -120,8 +130,18 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function getFirstTransaction(TransactionJournal $journal, Account $account) 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) 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') ::orderBy('transaction_journals.date', 'ASC')
->accountIs($account) ->accountIs($account)
->transactionTypes([TransactionType::OPENING_BALANCE]) ->transactionTypes([TransactionType::OPENING_BALANCE])
->orderBy('created_at', 'ASC') ->orderBy('created_at', 'ASC')
->first(['transaction_journals.*']); ->first(['transaction_journals.*']);
$cache->store($journal);
return $journal;
} }
/** /**

View File

@@ -50,6 +50,13 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito
*/ */
public function getCategories() public function getCategories()
{ {
$cache = new CacheProperties;
$cache->addProperty('category-list');
if($cache->has()) {
return $cache->get();
}
/** @var Collection $set */ /** @var Collection $set */
$set = Auth::user()->categories()->orderBy('name', 'ASC')->get(); $set = Auth::user()->categories()->orderBy('name', 'ASC')->get();
$set = $set->sortBy( $set = $set->sortBy(
@@ -58,6 +65,8 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito
} }
); );
$cache->store($set);
return $set; return $set;
} }