mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-19 04:49:30 +00:00
Added a routine that will cache stuff that costs a lot of queries.
This commit is contained in:
@@ -198,13 +198,18 @@ class AccountController extends BaseController
|
|||||||
|
|
||||||
$accounts->each(
|
$accounts->each(
|
||||||
function (Account $account) {
|
function (Account $account) {
|
||||||
//$transaction = $account->transactions()->orderBy('updated_at', 'DESC')->first();
|
if (Cache::has('account.' . $account->id . '.lastActivityDate')) {
|
||||||
$transaction = null;
|
\Log::debug('Cache has latest activity date for ' . $account->name . ', and it is: ' . \Cache::get('account.' . $account->id . '.lastActivityDate'));
|
||||||
|
$account->lastActionDate = Cache::get('account.' . $account->id . '.lastActivityDate');
|
||||||
if (is_null($transaction)) {
|
|
||||||
$account->lastActionDate = null;
|
|
||||||
} else {
|
} else {
|
||||||
$account->lastActionDate = $transaction->updated_at;
|
$transaction = $account->transactions()->orderBy('updated_at', 'DESC')->first();
|
||||||
|
if (is_null($transaction)) {
|
||||||
|
$account->lastActionDate = null;
|
||||||
|
Cache::forever('account.' . $account->id . '.lastActivityDate', 0);
|
||||||
|
} else {
|
||||||
|
$account->lastActionDate = $transaction->updated_at;
|
||||||
|
Cache::forever('account.' . $account->id . '.lastActivityDate', $transaction->updated_at);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,14 @@ class TransactionController extends BaseController
|
|||||||
* Trigger creation of new piggy bank event
|
* Trigger creation of new piggy bank event
|
||||||
*/
|
*/
|
||||||
Event::fire('transactionJournal.destroy', [$transactionJournal]); // new and used.
|
Event::fire('transactionJournal.destroy', [$transactionJournal]); // new and used.
|
||||||
|
/*
|
||||||
|
* Since this event will also destroy both transactions, trigger on those as
|
||||||
|
* well because we might want to update some caches and what-not.
|
||||||
|
*/
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($transactionJournal->transactions as $transaction) {
|
||||||
|
Event::fire('transaction.destroy', [$transaction]);
|
||||||
|
}
|
||||||
|
|
||||||
/** @var \FireflyIII\Database\TransactionJournal $repository */
|
/** @var \FireflyIII\Database\TransactionJournal $repository */
|
||||||
$repository = App::make('FireflyIII\Database\TransactionJournal');
|
$repository = App::make('FireflyIII\Database\TransactionJournal');
|
||||||
@@ -363,6 +371,13 @@ class TransactionController extends BaseController
|
|||||||
$piggyID = intval(Input::get('piggybank_id'));
|
$piggyID = intval(Input::get('piggybank_id'));
|
||||||
}
|
}
|
||||||
Event::fire('transactionJournal.store', [$journal, $piggyID]); // new and used.
|
Event::fire('transactionJournal.store', [$journal, $piggyID]); // new and used.
|
||||||
|
/*
|
||||||
|
* Also trigger on both transactions.
|
||||||
|
*/
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($journal->transactions as $transaction) {
|
||||||
|
Event::fire('transaction.store', [$transaction]);
|
||||||
|
}
|
||||||
|
|
||||||
if ($data['post_submit_action'] == 'create_another') {
|
if ($data['post_submit_action'] == 'create_another') {
|
||||||
return Redirect::route('transactions.create', $what)->withInput();
|
return Redirect::route('transactions.create', $what)->withInput();
|
||||||
@@ -406,6 +421,14 @@ class TransactionController extends BaseController
|
|||||||
Session::flash('success', 'Transaction updated!');
|
Session::flash('success', 'Transaction updated!');
|
||||||
Event::fire('transactionJournal.update', [$journal]); // new and used.
|
Event::fire('transactionJournal.update', [$journal]); // new and used.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Also trigger on both transactions.
|
||||||
|
*/
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($journal->transactions as $transaction) {
|
||||||
|
Event::fire('transaction.update', [$transaction]);
|
||||||
|
}
|
||||||
|
|
||||||
if (Input::get('post_submit_action') == 'return_to_edit') {
|
if (Input::get('post_submit_action') == 'return_to_edit') {
|
||||||
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
38
app/lib/FireflyIII/Event/Transaction.php
Normal file
38
app/lib/FireflyIII/Event/Transaction.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Event;
|
||||||
|
|
||||||
|
|
||||||
|
use Illuminate\Events\Dispatcher;
|
||||||
|
|
||||||
|
class Transaction
|
||||||
|
{
|
||||||
|
public function destroy(\Transaction $transaction)
|
||||||
|
{
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.latestBalance');
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.lastActivityDate');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(\Transaction $transaction)
|
||||||
|
{
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.latestBalance');
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.lastActivityDate');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Dispatcher $events
|
||||||
|
*/
|
||||||
|
public function subscribe(Dispatcher $events)
|
||||||
|
{
|
||||||
|
// triggers when others are updated.
|
||||||
|
$events->listen('transaction.store', 'FireflyIII\Event\Transaction@store');
|
||||||
|
$events->listen('transaction.update', 'FireflyIII\Event\Transaction@update');
|
||||||
|
$events->listen('transaction.destroy', 'FireflyIII\Event\Transaction@destroy');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(\Transaction $transaction)
|
||||||
|
{
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.latestBalance');
|
||||||
|
\Cache::forget('account.' . $transaction->account_id . '.lastActivityDate');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,13 +23,26 @@ class Steam
|
|||||||
*/
|
*/
|
||||||
public function balance(\Account $account, Carbon $date = null)
|
public function balance(\Account $account, Carbon $date = null)
|
||||||
{
|
{
|
||||||
$date = is_null($date) ? Carbon::now() : $date;
|
$latest = false;
|
||||||
|
if (is_null($date)) {
|
||||||
|
$latest = true;
|
||||||
|
if (\Cache::has('account.' . $account->id . '.latestBalance')) {
|
||||||
|
\Log::debug('Cache has latest balance for ' . $account->name . ', and it is: ' . \Cache::get('account.' . $account->id . '.latestBalance'));
|
||||||
|
|
||||||
return floatval(
|
return \Cache::get('account.' . $account->id . '.latestBalance');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$date = is_null($date) ? Carbon::now() : $date;
|
||||||
|
$balance = floatval(
|
||||||
$account->transactions()->leftJoin(
|
$account->transactions()->leftJoin(
|
||||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||||
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
|
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
|
||||||
);
|
);
|
||||||
|
if ($latest === true) {
|
||||||
|
\Cache::forever('account.' . $account->id . '.latestBalance', $balance);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ require $framework . '/Illuminate/Foundation/start.php';
|
|||||||
Event::subscribe('FireflyIII\Event\Piggybank');
|
Event::subscribe('FireflyIII\Event\Piggybank');
|
||||||
Event::subscribe('FireflyIII\Event\Budget');
|
Event::subscribe('FireflyIII\Event\Budget');
|
||||||
Event::subscribe('FireflyIII\Event\TransactionJournal');
|
Event::subscribe('FireflyIII\Event\TransactionJournal');
|
||||||
|
Event::subscribe('FireflyIII\Event\Transaction');
|
||||||
|
|
||||||
// event that creates a relationship between transaction journals and recurring events when created.
|
// event that creates a relationship between transaction journals and recurring events when created.
|
||||||
// event that updates the relationship between transaction journals and recurring events when edited.
|
// event that updates the relationship between transaction journals and recurring events when edited.
|
||||||
|
|||||||
Reference in New Issue
Block a user