Skip to content

Firefly III: a personal finances manager

Add cache to main chart.

James Cole294d0e388a51
diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php
index 5c9e11e3c1..645867c445 100644
--- a/app/Http/Controllers/Chart/AccountController.php
+++ b/app/Http/Controllers/Chart/AccountController.php
@@ -2,16 +2,19 @@
namespace FireflyIII\Http\Controllers\Chart;
+use Cache;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Support\ChartProperties;
use Grumpydictator\Gchart\GChart;
use Illuminate\Support\Collection;
use Preferences;
use Response;
use Session;
use Steam;
+use Auth;
/**
* Class AccountController
@@ -93,6 +96,26 @@ class AccountController extends Controller
$end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
+ // chart properties for cache:
+ $chartProperties = new ChartProperties();
+ $chartProperties->addProperty(Auth::user()->id);
+ $chartProperties->addProperty($frontPage);
+ $chartProperties->addProperty($start);
+ $chartProperties->addProperty($end);
+ $chartProperties->addProperty('frontpage');
+
+ /** @var Account $account */
+ foreach($accounts as $account) {
+ $chartProperties->addProperty($repository->getLastActivity($account));
+ }
+
+ $md5 = $chartProperties->md5();
+
+ if (Cache::has($md5)) {
+ return Cache::get($md5);
+ }
+
+
$index = 1;
/** @var Account $account */
foreach ($accounts as $account) {
@@ -115,7 +138,10 @@ class AccountController extends Controller
}
$chart->generate();
- return Response::json($chart->getData());
+ $data = $chart->getData();
+ Cache::forever($md5, $data);
+
+ return Response::json($data);
}
diff --git a/app/Support/ChartProperties.php b/app/Support/ChartProperties.php
new file mode 100644
index 0000000000..2b128274fa
--- /dev/null
+++ b/app/Support/ChartProperties.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace FireflyIII\Support;
+
+
+use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Collection as EloquentCollection;
+use Illuminate\Support\Collection;
+
+/**
+ * Class ChartProperties
+ *
+ * @package FireflyIII\Support
+ */
+class ChartProperties
+{
+
+ /** @var Collection */
+ protected $properties;
+
+ /**
+ *
+ */
+ public function __construct()
+ {
+ $this->properties = new Collection;
+ }
+
+ /**
+ * @param $property
+ */
+ public function addProperty($property)
+ {
+ $this->properties->push($property);
+ }
+
+
+ /**
+ * @return string
+ */
+ public function md5()
+ {
+ $string = '';
+ foreach ($this->properties as $property) {
+
+ if ($property instanceof Collection || $property instanceof EloquentCollection) {
+ $string .= print_r($property->toArray(), true);
+ continue;
+ }
+ if ($property instanceof Carbon) {
+ $string .= $property->toRfc3339String();
+ continue;
+ }
+ if (is_object($property)) {
+ $string .= $property->__toString();
+ }
+ if (is_array($property)) {
+ $string .= print_r($property, true);
+ }
+ $string .= (string)$property;
+ }
+
+ return md5($string);
+ }
+}
\ No newline at end of file