Advertisement
BobyskuyMM

Untitled

Jan 14th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use App\Managers\AuthorsManager;
  6. use App\Models\Author;
  7. use Exception;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Pagination\LengthAwarePaginator;
  10.  
  11.  
  12. use Illuminate\Support\Facades\Cache;
  13.  
  14.  
  15. /**
  16.  * Class SearchesCacheService
  17.  * @package App\Services
  18.  */
  19. class SearchesCacheService
  20. {
  21.     /** in minutes */
  22.     const USER_DATA_CACHE_TIME = 5;
  23.  
  24.     /** in minutes */
  25.     const GENERAL_DATA_CACHE_TIME = 30;
  26.  
  27.     /**
  28.      * @return mixed
  29.      */
  30.     public function getUsersOrderCnt()
  31.     {
  32.         return $this->getData(
  33.             $this->generateKey('users_order_cnt'),
  34.             function () {
  35.                 return app(OrdersService::class)->getUsersOrderCnt(auth()->id());
  36.             }
  37.         );
  38.     }
  39.  
  40.     /**
  41.      * @return mixed
  42.      */
  43.     public function getActiveRestaurants()
  44.     {
  45.         return $this->getData(
  46.             $this->generateKey('active_restaurants'),
  47.             function () {
  48.                 return app(KitchenService::class)->getActiveRestaurants();
  49.             },
  50.             self::GENERAL_DATA_CACHE_TIME
  51.         );
  52.     }
  53.  
  54.     /**
  55.      * @return mixed
  56.      */
  57.     public function getCuisines()
  58.     {
  59.         return $this->getData(
  60.             $this->generateKey('cuisines'),
  61.             function () {
  62.                 return app(CuisinesService::class)->getAllTransformed();
  63.             },
  64.             self::GENERAL_DATA_CACHE_TIME
  65.         );
  66.     }
  67.  
  68.     /**
  69.      * @return mixed
  70.      */
  71.     public function getSideCuisines()
  72.     {
  73.         return $this->getData(
  74.             $this->generateKey('side_cuisines'),
  75.             function () {
  76.                 return app(CuisinesService::class)->getSideCuisines();
  77.             },
  78.             self::GENERAL_DATA_CACHE_TIME
  79.         );
  80.     }
  81.  
  82.     /**
  83.      * @return mixed
  84.      */
  85.     public function getCloudChef()
  86.     {
  87.         return $this->getData(
  88.             $this->generateKey('cloud_chef'),
  89.             function () {
  90.                 return app(AuthorsService::class)->getList();
  91.             },
  92.             self::GENERAL_DATA_CACHE_TIME
  93.         );
  94.     }
  95.  
  96.     /**
  97.      * @return mixed
  98.      */
  99.     public function getNewKitchens()
  100.     {
  101.         return $this->getData(
  102.             $this->generateKey('new_kitchens'),
  103.             function () {
  104.                 return app(KitchenService::class)->getNewKitchensTransformed();
  105.             },
  106.             self::GENERAL_DATA_CACHE_TIME
  107.         );
  108.     }
  109.  
  110.     /**
  111.      * @return mixed
  112.      */
  113.     public function getTopRatedKitchens()
  114.     {
  115.         return $this->getData(
  116.             $this->generateKey('top_rated_kitchens'),
  117.             function () {
  118.                 return app(KitchenService::class)->getTopKitchensArray();
  119.             },
  120.             self::GENERAL_DATA_CACHE_TIME
  121.         );
  122.     }
  123.  
  124.     /**
  125.      * @return mixed
  126.      */
  127.     public function getNearYouKitchens()
  128.     {
  129.         return $this->getData(
  130.             $this->generateKey('near_you_kitchens'),
  131.             function () {
  132.                 return app(KitchenService::class)->nearList();
  133.             }
  134.         );
  135.     }
  136.  
  137.  
  138.     /**
  139.      * @return mixed
  140.      */
  141.     public function getFavoriteKitchens()
  142.     {
  143.         return $this->getData(
  144.             $this->generateKey('favorite_kitchens'),
  145.             function () {
  146.                 return app(KitchenService::class)->getFavoriteUsersRestaurantsArray(auth()->user());
  147.             }
  148.         );
  149.     }
  150.  
  151.     /**
  152.      * @return mixed
  153.      */
  154.     public function getLastOrders()
  155.     {
  156.         return $this->getData(
  157.             $this->generateKey('last_orders'),
  158.             function () {
  159.                 return (auth()->id()) ? app(OrdersService::class)->getLastUsersOrdersList(auth()->id()) : null;
  160.             }
  161.         );
  162.     }
  163.  
  164.  
  165.     /**
  166.      * @param string $key
  167.      * @param $data
  168.      * @param int $ttl
  169.      */
  170.     private function cachedData(string $key, $data, int $ttl)
  171.     {
  172.         Cache::add($key, $data, now()->addMinutes($ttl));
  173.     }
  174.  
  175.     /**
  176.      * @param string $key
  177.      * @param callable $dataCallback
  178.      * @param int $ttl
  179.      * @return mixed
  180.      */
  181.     private function getData(string $key, callable $dataCallback, $ttl = self::USER_DATA_CACHE_TIME)
  182.     {
  183.  
  184.         if ($this->hasData($key)) {
  185.             return Cache::get($key);
  186.         }
  187.  
  188.         $data = $dataCallback();
  189.         $this->cachedData($key, $data, $ttl);
  190.  
  191.         return $data;
  192.     }
  193.  
  194.     /**
  195.      * @param $key
  196.      * @return bool
  197.      */
  198.     private function hasData(string $key): bool
  199.     {
  200.         return Cache::has($key);
  201.     }
  202.  
  203.     /**
  204.      * @param $prefix
  205.      * @return string
  206.      */
  207.     private function generateKey(string $prefix): string
  208.     {
  209.         return sprintf('%s_%s_for_uid_%s', class_basename(self::class), $prefix, auth()->id());
  210.     }
  211. }
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement