SHARE
TWEET

HomeController Caching issue

benshepherd Aug 12th, 2017 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace App\Http\Controllers\Frontend;
  3.  
  4. use App\Content;
  5. use App\Http\Controllers\Controller as BaseController;
  6. use Illuminate\Support\Facades\Input;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redirect;
  10. use App\News;
  11. use App\Upload;
  12. use App\HomeOption;
  13. use App\TradingData;
  14. use App\Comment;
  15. use App\FeedChannel;
  16. use App\ForumThread;
  17. use App\Agency;
  18. use App\User;
  19. use App\Setting;
  20. use App\CustomHelper\QuestHelper;
  21. use App\CustomHelper\Helper;
  22.  
  23. class HomeController extends BaseController {
  24.  
  25.     public static $CACHE_KEY = 'HomeController_index';
  26.  
  27.     /**
  28.      * Default URL of website
  29.      * Homepage
  30.      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  31.      */
  32.     public function index()
  33.     {
  34.         return view('frontend.home', $this->indexGetData());
  35.     }
  36.  
  37.  
  38.     /**
  39.      * Get the vars required for the homepage
  40.      * @return mixed
  41.      */
  42.     public function indexGetData()
  43.     {
  44.         $data = cache()->remember(HomeController::$CACHE_KEY, 60, function() {
  45.  
  46.             $news = News::orderBy('id', 'desc')
  47.                 ->where('published', 1)
  48.                 ->take(10)
  49.                 ->get();
  50.             $newsAll = News::orderBy('id', 'desc')
  51.                 ->where('published', 1)
  52.                 ->where('is_advert', 0)
  53.                 ->take(4)
  54.                 ->get();
  55.             $popularQuests = Upload::where('type', 'guide')
  56.                 ->where('approved', 1)
  57.                 ->where('published', 1)
  58.                 ->where('deleted', 0)
  59.                 ->orderBy('rating', 'desc')
  60.                 ->take(3)
  61.                 ->get();
  62.  
  63.             $guides = HomeOption::getGuides(10);
  64.  
  65.  
  66.             $uploads = Upload::whereNull('parent_id')
  67.                 ->where('approved', 1)
  68.                 ->where('published', 1)
  69.                 ->where('deleted', 0)
  70.                 ->where(function($q) {
  71.                     //$q->orWhere('type', 'guide');
  72.                     $q->orWhere('type', 'tutorial');
  73.                     $q->orWhere('type', 'room');
  74.                     $q->orWhere('type', 'video');
  75.                     $q->orWhere('type', 'picture');
  76.                 })
  77.                 ->orderBy('created_at', 'desc')
  78.                 ->take(16)
  79.                 ->get();
  80.             $spotlight = HomeOption::getSpotlight();
  81.  
  82.  
  83.             $trades = TradingData::where(function($q) {
  84.                 $q->where('type', 'buying');
  85.                 $q->orWhere('type', 'selling');
  86.             })
  87.                 ->where('hotel', 'com')
  88.                 ->orderBy('id', 'desc')
  89.                 ->orderByRaw('RAND()')
  90.                 ->take(16)
  91.                 ->get();
  92.  
  93.             $feedComments = Comment::where('url', FeedChannel::getFirstFeedChannelURL())
  94.                 ->whereNull('parent_id')
  95.                 ->whereNull('comment_feed_id')
  96.                 ->orderBy('id', 'desc')
  97.                 ->take(10)
  98.                 ->get();
  99.  
  100.  
  101.             // latest threads
  102.             $threads = ForumThread::orderBy('id', 'desc')
  103.                 ->select(DB::raw('forum_threads.*'))
  104.                 ->where('deleted', 0);
  105.             $threads = ForumThread::addJoinsToQuery($threads);
  106.             $threads = ForumThread::addMemberOnlyPermsToQuery($threads);
  107.             $threads = $threads->take(4)->get();
  108.  
  109.             // hottest thread
  110.             $hotThread = ForumThread::orderBy('forum_activity.updated_at', 'desc')
  111.                 ->select(DB::raw('forum_threads.*'))
  112.                 ->where('deleted', 0);
  113.             $hotThread = ForumThread::addJoinsToQuery($hotThread);
  114.             $hotThread = ForumThread::addMemberOnlyPermsToQuery($hotThread);
  115.             $hotThread = $hotThread->first();
  116.  
  117.  
  118.             $furniture = HomeOption::getFurniture();
  119.             $agencies = Agency::getPartnershipAgencies();
  120.  
  121.             return array(
  122.                 'title' => 'Home',
  123.                 'newss' => $news,
  124.                 'newsAll' => $newsAll,
  125.                 'popularQuests' => $popularQuests,
  126.                 'spotlight' => $spotlight,
  127.                 'guides' => $guides,
  128.                 'uploads' => $uploads,
  129.                 'trades' => $trades,
  130.                 'feedComments' => $feedComments,
  131.                 'threads' => $threads,
  132.                 'hotThread' => $hotThread,
  133.                 'guidedBadges' => [],
  134.                 'furniture' => $furniture,
  135.                 'agencies' => $agencies,
  136.                 'homeContent' => Content::getContent('home_notification', null, true),
  137.                 'questLevel' => null,
  138.                 'questLevelName' => null,
  139.                 'questPercentInfo' => null,
  140.                 'collectedBadges' => null,
  141.                 'cacheTimestamp' => date('Y-m-d H:i:s'),
  142.             );
  143.         });
  144.  
  145.         if(Auth::check()) {
  146.             $questLevel = Auth::user()->getQuestLevel();
  147.             $questLevelName = Auth::user()->getQuestLevelName();
  148.             $questPercentInfo = QuestHelper::getPercentInformation();
  149.             $collectedBadges = QuestHelper::getCollectedBadges();
  150.             $guidedBadges = HomeOption::getGuidedBadges(8);
  151.  
  152.             $data['guidedBadges'] = $guidedBadges;
  153.             $data['questPercentInfo'] = $questPercentInfo;
  154.             $data['questLevel'] = $questLevel;
  155.             $data['questLevelName'] = $questLevelName;
  156.             $data['collectedBadges'] = $collectedBadges;
  157.         }
  158.  
  159.         return $data;
  160.     }
  161.  
  162. ....
  163. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top