From be17e4481e6f508dc3d5f0a9e8674e2310bdeeea Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 2 Jun 2015 18:05:42 +0200 Subject: [PATCH] Cache boxes. --- app/Http/Controllers/JsonController.php | 89 ++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index b88dc0e0b3..9ad1f8b6a7 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -1,6 +1,7 @@ startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); + $start = Session::get('start', Carbon::now()->startOfMonth()); + $end = Session::get('end', Carbon::now()->endOfMonth()); + + // works for json too! + $prop = new ChartProperties; + $prop->addProperty($start); + $prop->addProperty($end); + $prop->addProperty('box-bills-paid'); + $md5 = $prop->md5(); + if (Cache::has($md5)) { + Log::debug('Successfully returned cached box bills-paid [' . $md5 . ']'); + + return Response::json(Cache::get($md5)); + } + $amount = 0; + // these two functions are the same as the chart $bills = $repository->getActiveBills(); @@ -60,8 +77,10 @@ class JsonController extends Controller $amount += $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount'); } } + $data = ['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; + Cache::forever($md5, $data); - return Response::json(['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); + return Response::json($data); } /** @@ -75,6 +94,19 @@ class JsonController extends Controller $amount = 0; $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); + + // works for json too! + $prop = new ChartProperties; + $prop->addProperty($start); + $prop->addProperty($end); + $prop->addProperty('box-bills-unpaid'); + $md5 = $prop->md5(); + if (Cache::has($md5)) { + Log::debug('Successfully returned cached box bills-unpaid [' . $md5 . ']'); + + return Response::json(Cache::get($md5)); + } + $bills = $repository->getActiveBills(); $unpaid = new Collection; // bills @@ -109,7 +141,10 @@ class JsonController extends Controller $amount += $current; } - return Response::json(['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); + $data = ['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; + Cache::forever($md5, $data); + + return Response::json($data); } /** @@ -119,11 +154,28 @@ class JsonController extends Controller */ public function boxIn(ReportQueryInterface $reportQuery) { - $start = Session::get('start', Carbon::now()->startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); + $start = Session::get('start', Carbon::now()->startOfMonth()); + $end = Session::get('end', Carbon::now()->endOfMonth()); + + // works for json too! + $prop = new ChartProperties; + $prop->addProperty($start); + $prop->addProperty($end); + $prop->addProperty('box-in'); + $md5 = $prop->md5(); + if (Cache::has($md5)) { + Log::debug('Successfully returned cached box in [' . $md5 . ']'); + + return Response::json(Cache::get($md5)); + } + + $amount = $reportQuery->incomeInPeriodCorrected($start, $end, true)->sum('amount'); - return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); + $data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; + Cache::forever($md5, $data); + + return Response::json($data); } /** @@ -133,11 +185,28 @@ class JsonController extends Controller */ public function boxOut(ReportQueryInterface $reportQuery) { - $start = Session::get('start', Carbon::now()->startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); + $start = Session::get('start', Carbon::now()->startOfMonth()); + $end = Session::get('end', Carbon::now()->endOfMonth()); + + + // works for json too! + $prop = new ChartProperties; + $prop->addProperty($start); + $prop->addProperty($end); + $prop->addProperty('box-out'); + $md5 = $prop->md5(); + if (Cache::has($md5)) { + Log::debug('Successfully returned cached box out [' . $md5 . ']'); + + return Response::json(Cache::get($md5)); + } + $amount = $reportQuery->expenseInPeriodCorrected($start, $end, true)->sum('amount'); - return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); + $data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; + Cache::forever($md5, $data); + + return Response::json($data); } /**