|
@@ -3,7 +3,7 @@
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
-use Auth, Cache;
|
|
|
+use Auth, Cache, Redis;
|
|
|
use App\{Notification, Profile, User};
|
|
|
|
|
|
class AccountController extends Controller
|
|
@@ -15,8 +15,33 @@ class AccountController extends Controller
|
|
|
|
|
|
public function notifications(Request $request)
|
|
|
{
|
|
|
- $user = Auth::user();
|
|
|
- $profile = $user->profile;
|
|
|
- return view('account.activity', compact('profile'));
|
|
|
+ $profile = Auth::user()->profile;
|
|
|
+ $notifications = $this->fetchNotifications($profile->id);
|
|
|
+ return view('account.activity', compact('profile', 'notifications'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fetchNotifications($id)
|
|
|
+ {
|
|
|
+ $key = config('cache.prefix') . ":user.{$id}.notifications";
|
|
|
+ $redis = Redis::connection();
|
|
|
+ $notifications = $redis->lrange($key, 0, 30);
|
|
|
+ if(empty($notifications)) {
|
|
|
+ $notifications = Notification::whereProfileId($id)
|
|
|
+ ->orderBy('id','desc')->take(30)->get();
|
|
|
+ } else {
|
|
|
+ $notifications = $this->hydrateNotifications($notifications);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $notifications;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function hydrateNotifications($keys)
|
|
|
+ {
|
|
|
+ $prefix = 'notification.';
|
|
|
+ $notifications = [];
|
|
|
+ foreach($keys as $key) {
|
|
|
+ $notifications[] = Cache::get($prefix . $key);
|
|
|
+ }
|
|
|
+ return $notifications;
|
|
|
}
|
|
|
}
|