/** * Gets Tag objects from cache or DB * @param CacheHandlerInterface $cacheHandler * @param TagRepositoryInterface $tagRepo * @return array */ public function getTagsForUser(CacheHandlerInterface $cacheHandler, TagRepositoryInterface $tagRepo) { // Check to see if there is a cache item $tags = $cacheHandler->get(CacheHandlerInterface::TAGS); // No tags found in cache, check DB, cache them, and return them if (!$tags) { $tags = $tagRepo->all(Auth::user()); $tagNames = []; foreach ($tags as $tag) { $tagNames[] = $tag->name; } $cacheHandler->set(CacheHandlerInterface::TAGS, $tagNames); return $tagNames; } return $tags; }
/** * Get items for display * * @param Request $request * @param CacheHandlerInterface $cacheHandler * @param ItemRepositoryInterface $itemRepo * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function getAll(Request $request, CacheHandlerInterface $cacheHandler, ItemRepositoryInterface $itemRepo) { $pageRequested = $request->input('page'); $isMainPage = (is_null($pageRequested) or $pageRequested === 1); if ($isMainPage) { // Load from cache if available $items = $cacheHandler->get(CacheHandlerInterface::MAINPAGE); } else { $items = $itemRepo->getItemsPaginated(20, Auth::user()); } // Main page data not available in cache if (!$items) { // Get paginated items for user $items = $itemRepo->getItemsPaginated(20, Auth::user()); // Save in cache for next request $cacheHandler->set(CacheHandlerInterface::MAINPAGE, $items); } $title = "List"; return view('all', ['items' => $items, 'title' => $title]); }
/** * Execute the console command. * * @param ImageHandlerInterface $imageHandler * @param CacheHandlerInterface $cacheHandler * @param SearchHandlerInterface $searchHandler * @return mixed */ public function handle(ImageHandlerInterface $imageHandler, CacheHandlerInterface $cacheHandler, SearchHandlerInterface $searchHandler) { // Find all links with photos URLs that need thumbnails $links = Link::with('item', 'item.user')->whereNull('photo')->take(100)->get(); /* @var $link \App\Models\Link */ foreach ($links as $link) { // Try to generate a thumbnail for the desired photo try { $generatedFilename = $imageHandler->generateThumbnail($link->photo_url); if ($generatedFilename) { $link->photo = $generatedFilename; $link->save(); // Update Search $searchHandler->update($link); // Update cache for user $cacheHandler->del(CacheHandlerInterface::MAINPAGE, $link->item->user->id); } } catch (\Exception $e) { error_log($e); } } }
/** * Update an item * * @param $inputs * @return bool */ public function update($inputs) { /* @var $item Item */ $item = $this->itemsRepo->get($inputs['itemId']); $item->value = $inputs['value']; $item->description = $inputs['description']; $subclass = get_class($item->itemable); if ($subclass == 'App\\Models\\Link') { $item->itemable->title = $inputs['value']; } // Tags $this->tagsRepo->updateForItem($item, $inputs, Auth::user()); // Cache $this->cacheHandler->del(CacheHandlerInterface::MAINPAGE); $this->cacheHandler->del(CacheHandlerInterface::TAGS); // Save $item->save(); // Update Search $this->searchHandler->update($item->itemable); return $item; }
/** * Generate the cache item for a specific Tag with the given Links * * @param $tag * @param CacheHandlerInterface $cacheHandler * @param SearchHandlerInterface $searchHandler * @internal param $links */ private function generate($tag, CacheHandlerInterface $cacheHandler, SearchHandlerInterface $searchHandler) { // Find items for the requested Tag $filters['must'] = ['tags' => $tag->name]; $filters['should'] = ['discovery_setting' => 'attributed']; $filters['should'] = ['discovery_setting' => 'anonymous']; $links = $searchHandler->filteredSearch($filters, null, 'created_at', 'desc', 10); $cacheHandler->set(CacheHandlerInterface::DISCOVER_TAG, $links, $tag->name); }