Skip to content

Commit 209360c

Browse files
committed
add caching with redis
1 parent a794ea9 commit 209360c

22 files changed

+311
-60
lines changed

.env.example

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ DB_DATABASE=laravel
1313
DB_USERNAME=root
1414
DB_PASSWORD=
1515

16-
BROADCAST_DRIVER=log
17-
CACHE_DRIVER=file
18-
QUEUE_CONNECTION=sync
16+
BROADCAST_DRIVER=redis
17+
CACHE_DRIVER=redis
18+
QUEUE_CONNECTION=redis
1919
SESSION_DRIVER=file
2020
SESSION_LIFETIME=120
2121

2222
REDIS_HOST=127.0.0.1
2323
REDIS_PASSWORD=null
2424
REDIS_PORT=6379
25+
REDIS_PREFIX=apsky_laravel_database_
2526

2627
MAIL_MAILER=smtp
2728
MAIL_HOST=smtp.mailtrap.io

app/Events/News/NewsCreated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\News;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class NewsCreated
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public News $news;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @param News $news
20+
*/
21+
public function __construct(News $news)
22+
{
23+
$this->news = $news;
24+
}
25+
}

app/Events/News/NewsDeleted.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\News;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class NewsDeleted
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public News $news;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @param News $news
20+
*/
21+
public function __construct(News $news)
22+
{
23+
$this->news = $news;
24+
}
25+
}

app/Events/News/NewsUpdated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\News;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class NewsUpdated
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public News $news;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @param News $news
20+
*/
21+
public function __construct(News $news)
22+
{
23+
$this->news = $news;
24+
}
25+
}

app/Events/PostCreated.php renamed to app/Events/Posts/PostCreated.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ class PostCreated
1212

1313
public Post $post;
1414

15-
/**
16-
* Create a new event instance.
17-
*
18-
* @param Post $post
19-
*/
2015
public function __construct(Post $post)
2116
{
2217
$this->post = $post;

app/Events/Posts/PostDeleted.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Post;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class PostDeleted
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public Post $post;
15+
16+
public function __construct(Post $post)
17+
{
18+
$this->post = $post;
19+
}
20+
}
File renamed without changes.
File renamed without changes.

app/Http/Controllers/AdministrationController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\News;
66
use App\Post;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Cache;
89

910
class AdministrationController extends Controller
1011
{
@@ -18,12 +19,18 @@ public function index() {
1819
}
1920

2021
public function posts() {
21-
$posts = Post::with('tags')->latest()->get();
22+
$posts = Cache::tags('posts')->remember('user_posts|' . auth()->id(), 3600, function () {
23+
return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get();
24+
});
25+
2226
return view('/admin.posts', compact('posts'));
2327
}
2428

2529
public function news() {
26-
$news = News::latest()->get();
30+
$news = Cache::tags('news')->remember('users_news|' . auth()->id(), 3600, function () {
31+
return News::with(['tags', 'comments'])->latest()->get();
32+
});
33+
2734
return view('/admin.news', compact('news'));
2835
}
2936

app/Http/Controllers/FeedbacksController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace App\Http\Controllers;
44

55
use App\Feedback;
6+
use Illuminate\Filesystem\Cache;
67
use Illuminate\Http\Request;
78

89
class FeedbacksController extends Controller
910
{
1011
public function __construct()
1112
{
12-
$this->middleware('role:admin');
13+
$this->middleware('role:admin')->except('store');
1314
}
1415

1516
public function index()
@@ -28,6 +29,12 @@ public function store(Request $request)
2829

2930
Feedback::create($request->all());
3031

31-
return redirect('/admin/feedbacks');
32+
flash('Feedback successfully send (:', 'success');
33+
34+
if (auth()->user() && auth()->user()->hasRole('admin')) {
35+
return redirect('/admin/feedbacks');
36+
} else {
37+
return back();
38+
}
3239
}
3340
}

0 commit comments

Comments
 (0)