mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-05 21:53:08 -06:00
Add cache to main chart.
This commit is contained in:
parent
193a1b0325
commit
294d0e388a
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
65
app/Support/ChartProperties.php
Normal file
65
app/Support/ChartProperties.php
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user