Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- use App\Managers\AuthorsManager;
- use App\Models\Author;
- use Exception;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Facades\Cache;
- /**
- * Class SearchesCacheService
- * @package App\Services
- */
- class SearchesCacheService
- {
- /** in minutes */
- const USER_DATA_CACHE_TIME = 5;
- /** in minutes */
- const GENERAL_DATA_CACHE_TIME = 30;
- /**
- * @return mixed
- */
- public function getUsersOrderCnt()
- {
- return $this->getData(
- $this->generateKey('users_order_cnt'),
- function () {
- return app(OrdersService::class)->getUsersOrderCnt(auth()->id());
- }
- );
- }
- /**
- * @return mixed
- */
- public function getActiveRestaurants()
- {
- return $this->getData(
- $this->generateKey('active_restaurants'),
- function () {
- return app(KitchenService::class)->getActiveRestaurants();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getCuisines()
- {
- return $this->getData(
- $this->generateKey('cuisines'),
- function () {
- return app(CuisinesService::class)->getAllTransformed();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getSideCuisines()
- {
- return $this->getData(
- $this->generateKey('side_cuisines'),
- function () {
- return app(CuisinesService::class)->getSideCuisines();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getCloudChef()
- {
- return $this->getData(
- $this->generateKey('cloud_chef'),
- function () {
- return app(AuthorsService::class)->getList();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getNewKitchens()
- {
- return $this->getData(
- $this->generateKey('new_kitchens'),
- function () {
- return app(KitchenService::class)->getNewKitchensTransformed();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getTopRatedKitchens()
- {
- return $this->getData(
- $this->generateKey('top_rated_kitchens'),
- function () {
- return app(KitchenService::class)->getTopKitchensArray();
- },
- self::GENERAL_DATA_CACHE_TIME
- );
- }
- /**
- * @return mixed
- */
- public function getNearYouKitchens()
- {
- return $this->getData(
- $this->generateKey('near_you_kitchens'),
- function () {
- return app(KitchenService::class)->nearList();
- }
- );
- }
- /**
- * @return mixed
- */
- public function getFavoriteKitchens()
- {
- return $this->getData(
- $this->generateKey('favorite_kitchens'),
- function () {
- return app(KitchenService::class)->getFavoriteUsersRestaurantsArray(auth()->user());
- }
- );
- }
- /**
- * @return mixed
- */
- public function getLastOrders()
- {
- return $this->getData(
- $this->generateKey('last_orders'),
- function () {
- return (auth()->id()) ? app(OrdersService::class)->getLastUsersOrdersList(auth()->id()) : null;
- }
- );
- }
- /**
- * @param string $key
- * @param $data
- * @param int $ttl
- */
- private function cachedData(string $key, $data, int $ttl)
- {
- Cache::add($key, $data, now()->addMinutes($ttl));
- }
- /**
- * @param string $key
- * @param callable $dataCallback
- * @param int $ttl
- * @return mixed
- */
- private function getData(string $key, callable $dataCallback, $ttl = self::USER_DATA_CACHE_TIME)
- {
- if ($this->hasData($key)) {
- return Cache::get($key);
- }
- $data = $dataCallback();
- $this->cachedData($key, $data, $ttl);
- return $data;
- }
- /**
- * @param $key
- * @return bool
- */
- private function hasData(string $key): bool
- {
- return Cache::has($key);
- }
- /**
- * @param $prefix
- * @return string
- */
- private function generateKey(string $prefix): string
- {
- return sprintf('%s_%s_for_uid_%s', class_basename(self::class), $prefix, auth()->id());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement