FEATURE: cache most used SQL queries
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
namespace App\CMS\Enumerations;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ModulesHandlerToId extends BaseEnumeration
|
||||
{
|
||||
@@ -12,7 +13,9 @@ class ModulesHandlerToId extends BaseEnumeration
|
||||
*/
|
||||
protected static function init()
|
||||
{
|
||||
$modules = Module::all(['handler', 'id']);
|
||||
$modules = Cache::rememberForever('module-handler-to-id-enumeration', function () {
|
||||
return Module::all(['handler', 'id']);
|
||||
});
|
||||
|
||||
foreach ($modules as $module) {
|
||||
self::push($module->handler, $module->id);
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Content\Content;
|
||||
use App\Models\Module;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class CurrentTemplate implements TemplateHandler
|
||||
{
|
||||
@@ -20,16 +21,19 @@ class CurrentTemplate implements TemplateHandler
|
||||
public static function get()
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
try {
|
||||
self::$template = Module::where('is_template', true)
|
||||
->where('enabled', true)
|
||||
->first();
|
||||
} catch (QueryException $e) {
|
||||
throw new \ErrorException('DB not migrated, please, do so!');
|
||||
}
|
||||
if (self::$template) {
|
||||
self::$template->module_settings = json_decode(self::$template->module_settings);
|
||||
}
|
||||
self::$template = Cache::rememberForever('current-template', function () {
|
||||
$template = Module::where('is_template', true)
|
||||
->where('enabled', true)
|
||||
->first();
|
||||
|
||||
// todo remove after adding $casts into Module model
|
||||
if ($template) {
|
||||
$template->module_settings = json_decode($template->module_settings);
|
||||
}
|
||||
|
||||
return $template;
|
||||
});
|
||||
|
||||
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\CMS\Helpers;
|
||||
|
||||
use App\Models\Settings as SettingsModel;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* Class Settings
|
||||
@@ -76,8 +77,10 @@ class Settings
|
||||
private static function init()
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
$model = new SettingsModel();
|
||||
self::$settings = $model->getAll();
|
||||
self::$settings = Cache::remember('settings', config('cache.settingCacheLifetime'), function () {
|
||||
$model = new SettingsModel();
|
||||
return $model->getAll();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Console\Commands;
|
||||
use App\Models\Module;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
|
||||
@@ -63,6 +64,10 @@ class InstallModule extends Command
|
||||
// all mandatory fields are present, we can write it into database and create modules.json
|
||||
$this->installModule($config);
|
||||
|
||||
// delete cache
|
||||
Cache::forget('enabled-modules');
|
||||
Cache::forget('module-handler-to-id-enumeration');
|
||||
|
||||
// inform user
|
||||
$this->line('Module ' . $name . ' successfuly installed');
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class SetTemplate extends Command
|
||||
@@ -63,6 +64,9 @@ class SetTemplate extends Command
|
||||
return;
|
||||
}
|
||||
|
||||
// delete cached current template
|
||||
Cache::forget('current-template');
|
||||
|
||||
// theme written
|
||||
$this->line('Template successfully set up.');
|
||||
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
namespace App\Http\Controllers\Visitor;
|
||||
|
||||
use App\Models\Content\Content as ContentModel;
|
||||
use App\CMS\Contracts\Repositories\Content;
|
||||
use App\CMS\Helpers\PageRenderer;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Content\Content as ContentModel;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ContentPage extends Controller
|
||||
{
|
||||
@@ -20,13 +21,15 @@ class ContentPage extends Controller
|
||||
*/
|
||||
function __construct(Content $content)
|
||||
{
|
||||
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function getPage($url)
|
||||
{
|
||||
$page = $this->content->getByURL($url);
|
||||
$contentModel = $this->content;
|
||||
$page = Cache::remember('content-' . $url, setting('contentCacheLifetime', 5), function () use ($contentModel, $url) {
|
||||
return $contentModel->getByURL($url);
|
||||
});
|
||||
|
||||
// no page available
|
||||
if (!$page) {
|
||||
|
||||
@@ -2,12 +2,17 @@
|
||||
|
||||
namespace App\Http\Controllers\Visitor;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class Homepage extends ContentPage
|
||||
{
|
||||
public function getIndex()
|
||||
{
|
||||
// try to find some homepage
|
||||
$page = $this->content->getHomepage();
|
||||
$contentModel = $this->content;
|
||||
$page = Cache::remember('hp', setting('homepageCacheLifetime', 5), function () use ($contentModel) {
|
||||
return $contentModel->getHomepage();
|
||||
});
|
||||
|
||||
// test if we have any homepage
|
||||
if (!$page) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@@ -15,18 +16,19 @@ class ModuleManager extends ServiceProvider
|
||||
public function boot()
|
||||
{
|
||||
// this service provider needs published config - config is merged right after this service provider
|
||||
try {
|
||||
$modules = Cache::rememberForever('enabled-modules', function () {
|
||||
$modules = Module::where('enabled', true)
|
||||
->get();
|
||||
} catch (\Exception $e) {
|
||||
// ignore error
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($modules as $module) {
|
||||
$module->module_settings = json_decode($module->module_settings);
|
||||
}
|
||||
|
||||
return $modules;
|
||||
});
|
||||
|
||||
// iterate over all modules and enable or disable them
|
||||
foreach ($modules as $module) {
|
||||
$module->module_settings = json_decode($module->module_settings);
|
||||
|
||||
if ($module->is_template) {
|
||||
if (isset($module->module_settings->providers) && !empty($module->module_settings->providers)) {
|
||||
foreach ($module->module_settings->providers as $provider) {
|
||||
|
||||
+8
-5
@@ -13,7 +13,8 @@ return [
|
||||
|
|
||||
*/
|
||||
|
||||
'default' => env('CACHE_DRIVER', 'file'),
|
||||
'default' => env('CACHE_DRIVER', 'file'),
|
||||
'settingCacheLifetime' => env('SETTING_CACHE_LIFETIME', 10),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -37,8 +38,8 @@ return [
|
||||
],
|
||||
|
||||
'database' => [
|
||||
'driver' => 'database',
|
||||
'table' => 'cache',
|
||||
'driver' => 'database',
|
||||
'table' => 'cache',
|
||||
'connection' => null,
|
||||
],
|
||||
|
||||
@@ -51,13 +52,15 @@ return [
|
||||
'driver' => 'memcached',
|
||||
'servers' => [
|
||||
[
|
||||
'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'weight' => 100,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
'redis' => [
|
||||
'driver' => 'redis',
|
||||
'driver' => 'redis',
|
||||
'connection' => 'default',
|
||||
],
|
||||
|
||||
|
||||
Reference in New Issue
Block a user