summaryrefslogtreecommitdiff
path: root/app/Http
diff options
context:
space:
mode:
authorhorus2021-01-08 19:19:11 +0100
committerhorus2021-01-08 19:19:11 +0100
commit53bee00a3b1c68e68111094cbab0289a5b057140 (patch)
tree74636caa603f3d7fc5a0197515247d4031d60bc0 /app/Http
parent98742438f33b79a3c9daa978b77088c8cab215ba (diff)
downloadkategorischeraperitif-53bee00a3b1c68e68111094cbab0289a5b057140.tar.gz
Add full page caching.
Diffstat (limited to 'app/Http')
-rw-r--r--app/Http/Controllers/IndexController.php84
-rw-r--r--app/Http/Controllers/ListOfferController.php83
2 files changed, 92 insertions, 75 deletions
diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php
index de68ed3..3b87c9d 100644
--- a/app/Http/Controllers/IndexController.php
+++ b/app/Http/Controllers/IndexController.php
@@ -6,6 +6,8 @@ use App\Helpers\CryptoHelper;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Cache;
+
class IndexController extends Controller {
/**
* Shows the index page.
@@ -14,46 +16,48 @@ class IndexController extends Controller {
*/
public function showPage(Request $request) {
-
- $views = array("whisky", "wodka", "gin", "rum", "misc");
- $query = "";
-
- foreach($views as $view) {
- if ($query != "") {
- $query .= " UNION ";
- }
- # subquery to get procent, because cheap spirits don't look good
- #$query .= "(SELECT name, image_url, spirit_type, spirit_type AS url, spirit_type AS angebotsname, (SELECT MAX(procent) FROM " . $view . "_view) as procent, '' AS linktext FROM ". $view ."_view WHERE original_price > 19.98 AND shop != \"Drankdozijn\" ORDER BY procent DESC LIMIT 1)";
- $query .= "
- (SELECT
- name,
- image_url,
- spirit_type,
- spirit_type AS url,
- spirit_type AS angebotsname,
- (SELECT MAX(procent) FROM " . $view . "_view) as procent,
- '' AS linktext FROM ". $view ."_view
- WHERE
- original_price > 29.98
- AND
- spirit_type != 'Verschiedenes'
- AND
- shop != 'Rum & Co'
- AND
- shop = 'Drankdozijn'
- ORDER BY
- created_at DESC,
- procent DESC LIMIT 1)";
+ if ( Cache::has('index') ) {
+ return Cache::get('index');
+ } else {
+
+ $views = array("whisky", "wodka", "gin", "rum", "misc");
+ $query = "";
+
+ foreach($views as $view) {
+ if ($query != "") {
+ $query .= " UNION ";
+ }
+ $query .= "
+ (SELECT
+ name,
+ image_url,
+ spirit_type,
+ spirit_type AS url,
+ spirit_type AS angebotsname,
+ (SELECT MAX(procent) FROM " . $view . "_view) as procent,
+ '' AS linktext FROM ". $view ."_view
+ WHERE
+ original_price > 29.98
+ AND
+ spirit_type != 'Verschiedenes'
+ AND
+ shop != 'Rum & Co'
+ AND
+ shop = 'Drankdozijn'
+ ORDER BY
+ created_at DESC,
+ procent DESC LIMIT 1)";
+ }
+
+ $data = DB::select($query);
+
+ $count = DB::select("SELECT count(*) as count FROM all_view")[0];
+
+ $cached_data = view('index', ['data' => $data, 'views' => $views, 'hits' => $count])->render();
+
+ Cache::put('index', $cached_data, 600); // 10 minutes
+
+ return $cached_data;
}
- #$query .= " UNION (SELECT 'Alle Angebote' as name, 'https://angebote.fuselkoenig.de/img/feed-icon-gray.jpg' as image_url, 'RSS-Feeds' as spirit_type, 'feeds' AS url, 'RSS-Feeds' AS angebotsname, (SELECT MAX(procent) FROM all_view) as procent, 'Zu den RSS-Feeds' AS linktext FROM all_view LIMIT 1)";
-
- $data = DB::select($query);
-
- $count = DB::select("SELECT count(*) as count FROM all_view")[0];
- #$data = DB::table('whisky_view')->orderBy('procent', 'DESC')->limit(100)->simplePaginate(20);
-
- #$views[] = "rss-feeds";
-
- return view('index', ['data' => $data, 'views' => $views, 'hits' => $count]);
}
}
diff --git a/app/Http/Controllers/ListOfferController.php b/app/Http/Controllers/ListOfferController.php
index 904ee20..7e91d6a 100644
--- a/app/Http/Controllers/ListOfferController.php
+++ b/app/Http/Controllers/ListOfferController.php
@@ -7,6 +7,7 @@ use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Carbon;
+use Illuminate\Support\Facades\Cache;
class ListOfferController extends Controller {
/**
@@ -52,55 +53,67 @@ class ListOfferController extends Controller {
$order_by = "desc";
}
+
/*
* Database query
*/
$view_name = \Request::route()->getName();
- if ( "" != $request->get('type') ) {
- $data = DB::table($view_name . '_view')->where('spirit_type', '=', $request->get('type'))->whereNotNull('url')->orderBy($sort_by, $order_by)->paginate(10);
- $count = DB::table($view_name . '_view')->where('spirit_type', '=', $request->get('type'))->whereNotNull('url')->count();
-
+ /**
+ * Cache output
+ */
+ if ( Cache::has($view_name . $sort_by . $order_by) ) {
+ return Cache::get($view_name . $sort_by . $order_by);
} else {
- $data = DB::table($view_name . '_view')->where('procent', '>=', '5')->whereNotNull('url')->orderBy($sort_by, $order_by)->paginate(10);
- $count = DB::table($view_name . '_view')->where('procent', '>=', '5')->whereNotNull('url')->count();
- }
- # Only used on misc_view, all_view and search.
- $spirit_types = DB::select('SELECT DISTINCT spirit_type FROM ' . $view_name . '_view ' . ' ORDER BY spirit_type' );
+ if ( "" != $request->get('type') ) {
+ $data = DB::table($view_name . '_view')->where('spirit_type', '=', $request->get('type'))->whereNotNull('url')->orderBy($sort_by, $order_by)->paginate(10);
+ $count = DB::table($view_name . '_view')->where('spirit_type', '=', $request->get('type'))->whereNotNull('url')->count();
- $rss_feed = $view_name . "/feed/";
- $query_string = "";
- $query_params = $request->except('page');
- foreach( $query_params as $key => $value) {
- $query_string .= "&" . $key . "=" . $value;
- }
- if ( "search" == $view_name && "" != $query_string ) {
- $query_string = ltrim($query_string, "&");
- $rss_feed .= "?" . $query_string;
- }
+ } else {
+ $data = DB::table($view_name . '_view')->where('procent', '>=', '5')->whereNotNull('url')->orderBy($sort_by, $order_by)->paginate(10);
+ $count = DB::table($view_name . '_view')->where('procent', '>=', '5')->whereNotNull('url')->count();
+ }
- if ( "misc" == $view_name ) {
+ # Only used on misc_view, all_view and search.
+ $spirit_types = DB::select('SELECT DISTINCT spirit_type FROM ' . $view_name . '_view ' . ' ORDER BY spirit_type' );
- $view_name = "Andere Angebote";
- if ( $data->count() ) {
- $title = "andere Angebote";
- } else {
- $title = "anderen Angebote";
+ $rss_feed = $view_name . "/feed/";
+ $query_string = "";
+ $query_params = $request->except('page');
+ foreach( $query_params as $key => $value) {
+ $query_string .= "&" . $key . "=" . $value;
+ }
+ if ( "search" == $view_name && "" != $query_string ) {
+ $query_string = ltrim($query_string, "&");
+ $rss_feed .= "?" . $query_string;
}
- } else if ( "all" == $view_name ) {
- $view_name = "Alle Angebote";
- if ( $data->count() ) {
- $title = "alle Angebote";
+
+ if ( "misc" == $view_name ) {
+
+ $view_name = "Andere Angebote";
+ if ( $data->count() ) {
+ $title = "andere Angebote";
+ } else {
+ $title = "anderen Angebote";
+ }
+ } else if ( "all" == $view_name ) {
+ $view_name = "Alle Angebote";
+ if ( $data->count() ) {
+ $title = "alle Angebote";
+ } else {
+ $title = "Angebote";
+ }
} else {
- $title = "Angebote";
+ $spirit_types = array();
+
+ $title = ucfirst($view_name) . "-Angebote";
}
- } else {
- $spirit_types = array();
- $title = ucfirst($view_name) . "-Angebote";
+ $cached_data = view('listoffer', ['data' => $data, 'count' => $count, 'spirit_type' => $view_name, 'spirit_types' => $spirit_types, 'rss_feed' => $rss_feed, 'title' => $title ])->render();
+
+ Cache::put($view_name . $sort_by . $order_by, $cached_data, 600); // 10 minutes
+ return $cached_data;
}
-
- return view('listoffer', ['data' => $data, 'count' => $count, 'spirit_type' => $view_name, 'spirit_types' => $spirit_types, 'rss_feed' => $rss_feed, 'title' => $title ]);
}
}