FEATURE: cache most used SQL queries

This commit is contained in:
Michal Kváček
2017-02-22 01:40:26 +01:00
parent cad205960d
commit f3b1951d25
10 changed files with 74 additions and 35 deletions
+13 -6
View File
@@ -2,6 +2,7 @@
namespace App\CMS\Helpers;
use App\Models\Widgets\WidgetArea;
use Illuminate\Support\Facades\Cache;
class WidgetAreas
{
@@ -14,8 +15,9 @@ class WidgetAreas
protected static function saveRenderedWidgetArea($widgets)
{
foreach ($widgets as $widget) {
if (!isset(self::$widgetAreas[$widget->widget_area_code]))
if (!isset(self::$widgetAreas[$widget->widget_area_code])) {
self::$widgetAreas[$widget->widget_area_code] = '';
}
self::$widgetAreas[$widget->widget_area_code] .= Widget::render($widget);
}
@@ -23,9 +25,11 @@ class WidgetAreas
protected static function loadWidgetArea($uniqueCode)
{
$widgetArea = new WidgetArea();
$widgets = $widgetArea->getWidgets($uniqueCode)
$widgets = Cache::remember('widget-' . $uniqueCode, setting('widgetCacheLifetime', 5), function () use ($uniqueCode) {
$widgetArea = new WidgetArea();
return $widgetArea->getWidgets($uniqueCode)
->get();
});
WidgetArea::where('code', $uniqueCode)
->increment('viewed');
@@ -35,12 +39,15 @@ class WidgetAreas
protected static function initWidgetAreas()
{
if (self::$initialized)
if (self::$initialized) {
return;
}
$widgetArea = new WidgetArea();
$widgets = $widgetArea->getWidgets()
$widgets = Cache::remember('widgets', setting('widgetCacheLifetime', 5), function () {
$widgetArea = new WidgetArea();
return $widgetArea->getWidgets()
->get();
});
self::saveRenderedWidgetArea($widgets);
self::$initialized = true;