Lots of stuff gets cached now.

This commit is contained in:
James Cole
2015-06-03 18:22:47 +02:00
parent 4b0b79199d
commit a7f6848e53
15 changed files with 286 additions and 242 deletions

View File

@@ -8,7 +8,7 @@ use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\ChartProperties;
use FireflyIII\Support\CacheProperties;
use Grumpydictator\Gchart\GChart;
use Illuminate\Support\Collection;
use Log;
@@ -43,7 +43,7 @@ class AccountController extends Controller
$end->endOfMonth();
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('all');
@@ -118,7 +118,7 @@ class AccountController extends Controller
$accounts = $repository->getFrontpageAccounts($frontPage);
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('frontpage');
@@ -182,7 +182,7 @@ class AccountController extends Controller
$today = new Carbon;
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('frontpage');

View File

@@ -8,7 +8,7 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Support\ChartProperties;
use FireflyIII\Support\CacheProperties;
use Grumpydictator\Gchart\GChart;
use Illuminate\Support\Collection;
use Response;
@@ -40,7 +40,7 @@ class BillController extends Controller
$chart->addColumn(trans('firefly.minAmount'), 'number');
$chart->addColumn(trans('firefly.billEntry'), 'number');
$chartProperties = new ChartProperties;
$chartProperties = new CacheProperties;
$chartProperties->addProperty('single');
$chartProperties->addProperty('bill');
$chartProperties->addProperty($bill->id);
@@ -87,7 +87,7 @@ class BillController extends Controller
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('bills');

View File

@@ -8,7 +8,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\ChartProperties;
use FireflyIII\Support\CacheProperties;
use Grumpydictator\Gchart\GChart;
use Illuminate\Support\Collection;
use Log;
@@ -45,7 +45,7 @@ class BudgetController extends Controller
$last = Navigation::endOfX($last, $range, $final);
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($first);
$chartProperties->addProperty($last);
$chartProperties->addProperty('budget');
@@ -93,7 +93,7 @@ class BudgetController extends Controller
$end = $repetition->enddate;
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('budget');
@@ -153,7 +153,7 @@ class BudgetController extends Controller
$allEntries = new Collection;
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('budget');
@@ -227,7 +227,7 @@ class BudgetController extends Controller
$budgets = $repository->getBudgets();
// chart properties for cache:
$chartProperties = new ChartProperties();
$chartProperties = new CacheProperties();
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('budget');

View File

@@ -8,7 +8,7 @@ use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\ChartProperties;
use FireflyIII\Support\CacheProperties;
use Grumpydictator\Gchart\GChart;
use Log;
use Navigation;
@@ -80,7 +80,7 @@ class CategoryController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
// chart properties for cache:
$chartProperties = new ChartProperties;
$chartProperties = new CacheProperties;
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('category');

View File

@@ -62,9 +62,11 @@ class HomeController extends Controller
public function index(AccountRepositoryInterface $repository)
{
$types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types);
if ($count == 0) {
return Redirect::route('new-user.index');
}
@@ -78,8 +80,11 @@ class HomeController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
$savings = $repository->getSavingsAccounts();
$piggyBankAccounts = $repository->getPiggyBankAccounts();
$savingsTotal = 0;
foreach ($savings as $savingAccount) {
$savingsTotal += Steam::balance($savingAccount, $end);
@@ -100,7 +105,6 @@ class HomeController extends Controller
$transactions[] = [$set, $account];
}
}
return view('index', compact('count', 'title', 'savings', 'subTitle', 'mainTitleIcon', 'transactions', 'savingsTotal', 'piggyBankAccounts'));
}

View File

@@ -11,7 +11,7 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\ChartProperties;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Log;
use Response;
@@ -40,7 +40,7 @@ class JsonController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
// works for json too!
$prop = new ChartProperties;
$prop = new CacheProperties;
$prop->addProperty($start);
$prop->addProperty($end);
$prop->addProperty('box-bills-paid');
@@ -96,7 +96,7 @@ class JsonController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
// works for json too!
$prop = new ChartProperties;
$prop = new CacheProperties;
$prop->addProperty($start);
$prop->addProperty($end);
$prop->addProperty('box-bills-unpaid');
@@ -158,7 +158,7 @@ class JsonController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
// works for json too!
$prop = new ChartProperties;
$prop = new CacheProperties;
$prop->addProperty($start);
$prop->addProperty($end);
$prop->addProperty('box-in');
@@ -190,7 +190,7 @@ class JsonController extends Controller
// works for json too!
$prop = new ChartProperties;
$prop = new CacheProperties;
$prop->addProperty($start);
$prop->addProperty($end);
$prop->addProperty('box-out');

View File

@@ -3,11 +3,12 @@
namespace FireflyIII\Http\Middleware;
use App;
use Cache;
use Carbon\Carbon;
use Closure;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Reminder;
use FireflyIII\User;
use FireflyIII\Support\CacheProperties;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use View;
@@ -47,35 +48,49 @@ class Reminders
*/
public function handle(Request $request, Closure $next)
{
if ($this->auth->check() && !$request->isXmlHttpRequest()) {
// do reminders stuff.
$reminders = [];
if ($this->auth->user() instanceof User) {
$piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get();
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface $helper */
$helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface');
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
$helper->createReminders($piggyBank, new Carbon);
}
// delete invalid reminders
// this is a construction SQLITE cannot handle :(
if (env('DB_CONNECTION') != 'sqlite') {
Reminder::whereUserId($this->auth->user()->id)
->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id')
->whereNull('piggy_banks.id')
->delete();
}
// abuse CacheProperties to find out if we need to do this:
$properties = new CacheProperties;
// get and list active reminders:
$reminders = $this->auth->user()->reminders()->today()->get();
$reminders->each(
function (Reminder $reminder) use ($helper) {
$reminder->description = $helper->getReminderText($reminder);
}
);
$properties->addProperty('reminders');
$md5 = $properties->md5();
if (Cache::has($md5)) {
$reminders = Cache::get($md5);
View::share('reminders', $reminders);
return $next($request);
}
$piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get();
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface $helper */
$helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface');
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
$helper->createReminders($piggyBank, new Carbon);
}
// delete invalid reminders
// this is a construction SQLITE cannot handle :(
if (env('DB_CONNECTION') != 'sqlite') {
Reminder::whereUserId($this->auth->user()->id)->leftJoin('piggy_banks', 'piggy_banks.id', '=', 'remindersable_id')
->whereNull('piggy_banks.id')->delete();
}
// get and list active reminders:
$reminders = $this->auth->user()->reminders()->today()->get();
$reminders->each(
function (Reminder $reminder) use ($helper) {
$reminder->description = $helper->getReminderText($reminder);
}
);
Cache::forever($md5, $reminders);
View::share('reminders', $reminders);
}

View File

@@ -1,7 +1,9 @@
<?php namespace FireflyIII\Models;
use Cache;
use Carbon\Carbon;
use Crypt;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -134,6 +136,15 @@ class TransactionJournal extends Model
*/
public function getAmountAttribute()
{
$prop = new CacheProperties();
$prop->addProperty($this->id);
$prop->addProperty('amount');
$md5 = $prop->md5();
if (Cache::has($md5)) {
return Cache::get($md5);
}
$amount = '0';
bcscale(2);
/** @var Transaction $t */
@@ -147,6 +158,8 @@ class TransactionJournal extends Model
* If the journal has tags, it gets complicated.
*/
if ($this->tags->count() == 0) {
Cache::forever($md5, $amount);
return $amount;
}
@@ -160,6 +173,7 @@ class TransactionJournal extends Model
foreach ($others as $other) {
$amount = bcsub($amount, $other->actual_amount);
}
Cache::forever($md5, $amount);
return $amount;
}
@@ -167,6 +181,8 @@ class TransactionJournal extends Model
// if this journal is part of an advancePayment AND the journal is a deposit,
// then the journal amount is correcting a withdrawal, and the amount is zero:
if ($advancePayment && $this->transactionType->type == 'Deposit') {
Cache::forever($md5, '0');
return '0';
}
@@ -180,12 +196,15 @@ class TransactionJournal extends Model
$transfer = $balancingAct->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount = bcsub($amount, $transfer->actual_amount);
Cache::forever($md5, $amount);
return $amount;
}
} // @codeCoverageIgnore
} // @codeCoverageIgnore
Cache::forever($md5, $amount);
return $amount;
}
@@ -219,6 +238,15 @@ class TransactionJournal extends Model
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
/**
* @return string
*/
@@ -235,15 +263,6 @@ class TransactionJournal extends Model
return '0';
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
/**
* @codeCoverageIgnore
* @return string[]

View File

@@ -4,6 +4,7 @@ namespace FireflyIII\Repositories\Account;
use App;
use Auth;
use Cache;
use Carbon\Carbon;
use Config;
use DB;
@@ -15,6 +16,7 @@ use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
@@ -109,12 +111,23 @@ class AccountRepository implements AccountRepositoryInterface
*/
public function getFrontpageAccounts(Preference $preference)
{
$cache = new CacheProperties();
$cache->addProperty($preference->data);
$cache->addProperty('frontPageaccounts');
$md5 = $cache->md5();
if (Cache::has($md5)) {
return Cache::get($md5);
}
if ($preference->data == []) {
$accounts = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
} else {
$accounts = Auth::user()->accounts()->whereIn('id', $preference->data)->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
Cache::forever($md5, $accounts);
return $accounts;
}
@@ -131,7 +144,16 @@ class AccountRepository implements AccountRepositoryInterface
*/
public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end)
{
return Auth::user()
$prop = new CacheProperties();
$prop->addProperty($account->id);
$prop->addProperty($start);
$prop->addProperty($end);
$md5 = $prop->md5();
if(Cache::has($md5)) {
return Cache::get($md5);
}
$set = Auth::user()
->transactionjournals()
->with(['transactions'])
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
@@ -144,6 +166,8 @@ class AccountRepository implements AccountRepositoryInterface
->orderBy('transaction_journals.id', 'DESC')
->take(10)
->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']);
Cache::forever($md5, $set);
return $set;
}
/**
@@ -207,6 +231,14 @@ class AccountRepository implements AccountRepositoryInterface
$ids[] = intval($id->account_id);
}
$cache = new CacheProperties;
$cache->addProperty($ids);
$cache->addProperty('piggyAccounts');
$md5 = $cache->md5();
if (Cache::has($md5)) {
return Cache::get($md5);
}
$ids = array_unique($ids);
if (count($ids) > 0) {
$accounts = Auth::user()->accounts()->whereIn('id', $ids)->get();
@@ -231,6 +263,8 @@ class AccountRepository implements AccountRepositoryInterface
}
);
Cache::forever($md5, $accounts);
return $accounts;
}

View File

@@ -10,11 +10,11 @@ use Illuminate\Support\Collection;
use Preferences as Prefs;
/**
* Class ChartProperties
* Class CacheProperties
*
* @package FireflyIII\Support
*/
class ChartProperties
class CacheProperties
{
/** @var Collection */
@@ -56,6 +56,12 @@ class ChartProperties
$string .= $property->toRfc3339String();
continue;
}
if (is_array($property)) {
$string .= print_r($property, true);
continue;
}
if (is_object($property)) {
$string .= $property->__toString();
}

View File

@@ -4,7 +4,6 @@ namespace FireflyIII\Support;
use Auth;
use FireflyIII\Models\Preference;
use Log;
/**
* Class Preferences
*
@@ -26,7 +25,6 @@ class Preferences
* @return bool
*/
public function mark() {
Log::debug('MARK!');
$this->set('lastActivity',microtime());
return true;
}

View File

@@ -2,10 +2,9 @@
namespace FireflyIII\Support;
use Cache;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
/**
* Class Steam
@@ -24,6 +23,19 @@ class Steam
*/
public function balance(Account $account, Carbon $date, $ignoreVirtualBalance = false)
{
// abuse chart properties:
$properties = new CacheProperties;
$properties->addProperty($account->id);
$properties->addProperty('balance');
$properties->addProperty($date);
$properties->addProperty($ignoreVirtualBalance);
$md5 = $properties->md5();
if (Cache::has($md5)) {
return Cache::get($md5);
}
// find the first known transaction on this account:
$firstDateObject = $account
->transactions()
@@ -45,6 +57,7 @@ class Steam
if (!$ignoreVirtualBalance) {
$balance = bcadd($balance, $account->virtual_balance);
}
Cache::forever($md5, round($balance, 2));
return round($balance, 2);
}

View File

@@ -4,7 +4,9 @@ namespace FireflyIII\Support\Twig;
use App;
use Cache;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
@@ -26,21 +28,38 @@ class Journal extends Twig_Extension
$filters = [];
$filters[] = new Twig_SimpleFilter(
'typeIcon', function(TransactionJournal $journal) {
'typeIcon', function (TransactionJournal $journal) {
$prop = new CacheProperties();
$prop->addProperty($journal->id);
$prop->addProperty('typeIcon');
$md5 = $prop->md5();
if (Cache::has($md5)) {
return Cache::get($md5);
}
$type = $journal->transactionType->type;
switch ($type) {
case 'Withdrawal':
return '<span class="glyphicon glyphicon-arrow-left" title="' . trans('firefly.withdrawal') . '"></span>';
$txt = '<span class="glyphicon glyphicon-arrow-left" title="' . trans('firefly.withdrawal') . '"></span>';
break;
case 'Deposit':
return '<span class="glyphicon glyphicon-arrow-right" title="' . trans('firefly.deposit') . '"></span>';
$txt = '<span class="glyphicon glyphicon-arrow-right" title="' . trans('firefly.deposit') . '"></span>';
break;
case 'Transfer':
return '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case 'Opening balance':
return '<span class="glyphicon glyphicon-ban-circle" title="' . trans('firefly.openingBalance') . '"></span>';
$txt = '<span class="glyphicon glyphicon-ban-circle" title="' . trans('firefly.openingBalance') . '"></span>';
break;
default:
return '';
$txt = '';
break;
}
Cache::forever($md5, $txt);
return $txt;
}, ['is_safe' => ['html']]
@@ -59,7 +78,7 @@ class Journal extends Twig_Extension
$functions = [];
$functions[] = new Twig_SimpleFunction(
'invalidJournal', function(TransactionJournal $journal) {
'invalidJournal', function (TransactionJournal $journal) {
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
@@ -69,7 +88,7 @@ class Journal extends Twig_Extension
);
$functions[] = new Twig_SimpleFunction(
'relevantTags', function(TransactionJournal $journal) {
'relevantTags', function (TransactionJournal $journal) {
if ($journal->tags->count() == 0) {
return App::make('amount')->formatJournal($journal);
}
@@ -82,7 +101,7 @@ class Journal extends Twig_Extension
$amount = App::make('amount')->format($journal->actual_amount, false);
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>';
. '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>';
}
/*
@@ -92,7 +111,7 @@ class Journal extends Twig_Extension
$amount = App::make('amount')->formatJournal($journal, false);
return '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
}
/*
* AdvancePayment with a withdrawal will show the amount with a link to