Add cache to main chart.

This commit is contained in:
James Cole 2015-06-01 18:41:18 +02:00
parent 193a1b0325
commit 294d0e388a
2 changed files with 92 additions and 1 deletions

View File

@ -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);
}

View 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);
}
}