removed overuse of cache:clear command
This commit is contained in:
		| @@ -6,7 +6,6 @@ use App\Models\Module\Module; | ||||
| use App\Traits\SiteApi; | ||||
| use App\Utilities\Console; | ||||
| use App\Utilities\Info; | ||||
| use Artisan; | ||||
| use Cache; | ||||
| use Date; | ||||
| use File; | ||||
| @@ -35,12 +34,11 @@ trait Modules | ||||
|     } | ||||
|  | ||||
|     // Get All Modules | ||||
|     public function getModules() | ||||
|     public function getModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.items.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $items = Cache::get('apps.items.page.' . $page); | ||||
|         $items = Cache::get($key); | ||||
|  | ||||
|         if (!empty($items)) { | ||||
|             return $items; | ||||
| @@ -48,7 +46,7 @@ trait Modules | ||||
|  | ||||
|         $items = static::getResponseData('GET', 'apps/items'); | ||||
|  | ||||
|         Cache::put('apps.items.page.' . $page, $items, Date::now()->addHour()); | ||||
|         Cache::put($key, $items, Date::now()->addHour()); | ||||
|  | ||||
|         return $items; | ||||
|     } | ||||
| @@ -63,8 +61,9 @@ trait Modules | ||||
|  | ||||
|     public function getDocumentation($alias) | ||||
|     { | ||||
|         // Get data from cache | ||||
|         $documentation = Cache::get('apps.docs.' . $alias); | ||||
|         $key = 'apps.docs.' . $alias; | ||||
|  | ||||
|         $documentation = Cache::get($key); | ||||
|  | ||||
|         if (!empty($documentation)) { | ||||
|             return $documentation; | ||||
| @@ -72,17 +71,16 @@ trait Modules | ||||
|  | ||||
|         $documentation = static::getResponseData('GET', 'apps/docs/' . $alias); | ||||
|  | ||||
|         Cache::put('apps.docs.' . $alias, $documentation, Date::now()->addHour()); | ||||
|         Cache::put($key, $documentation, Date::now()->addHour()); | ||||
|  | ||||
|         return $documentation; | ||||
|     } | ||||
|  | ||||
|     public function getModuleReviews($alias, $data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.' . $alias . '.reviews.page.'. $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $reviews = Cache::get('apps.' . $alias . '.reviews.page.'. $page); | ||||
|         $reviews = Cache::get($key); | ||||
|  | ||||
|         if (!empty($reviews)) { | ||||
|             return $reviews; | ||||
| @@ -90,17 +88,16 @@ trait Modules | ||||
|  | ||||
|         $reviews = static::getResponseData('GET', 'apps/' . $alias . '/reviews', $data); | ||||
|  | ||||
|         Cache::put('apps.' . $alias . '.reviews.page.' . $page, $reviews, Date::now()->addHour()); | ||||
|         Cache::put($key, $reviews, Date::now()->addHour()); | ||||
|  | ||||
|         return $reviews; | ||||
|     } | ||||
|  | ||||
|     public function getCategories() | ||||
|     public function getCategories($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.categories.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $categories = Cache::get('apps.categories.page.' . $page); | ||||
|         $categories = Cache::get($key); | ||||
|  | ||||
|         if (!empty($categories)) { | ||||
|             return $categories; | ||||
| @@ -108,17 +105,16 @@ trait Modules | ||||
|  | ||||
|         $categories = static::getResponseData('GET', 'apps/categories'); | ||||
|  | ||||
|         Cache::put('apps.categories.page.' . $page, $categories, Date::now()->addHour()); | ||||
|         Cache::put($key, $categories, Date::now()->addHour()); | ||||
|  | ||||
|         return $categories; | ||||
|     } | ||||
|  | ||||
|     public function getModulesByCategory($alias, $data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.categories.' . $alias . '.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $category = Cache::get('apps.categories.' . $alias . '.page.' . $page); | ||||
|         $category = Cache::get($key); | ||||
|  | ||||
|         if (!empty($category)) { | ||||
|             return $category; | ||||
| @@ -126,17 +122,16 @@ trait Modules | ||||
|  | ||||
|         $category = static::getResponseData('GET', 'apps/categories/' . $alias, $data); | ||||
|  | ||||
|         Cache::put('apps.categories.' . $alias . '.page.' . $page, $category, Date::now()->addHour()); | ||||
|         Cache::put($key, $category, Date::now()->addHour()); | ||||
|  | ||||
|         return $category; | ||||
|     } | ||||
|  | ||||
|     public function getVendors() | ||||
|     public function getVendors($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.vendors.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $vendors = Cache::get('apps.vendors.page.' . $page); | ||||
|         $vendors = Cache::get($key); | ||||
|  | ||||
|         if (!empty($vendors)) { | ||||
|             return $vendors; | ||||
| @@ -144,17 +139,16 @@ trait Modules | ||||
|  | ||||
|         $vendors = static::getResponseData('GET', 'apps/vendors'); | ||||
|  | ||||
|         Cache::put('apps.vendors.page.' . $page, $vendors, Date::now()->addHour()); | ||||
|         Cache::put($key, $vendors, Date::now()->addHour()); | ||||
|  | ||||
|         return $vendors; | ||||
|     } | ||||
|  | ||||
|     public function getModulesByVendor($alias, $data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.vendors.' . $alias . '.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $vendor = Cache::get('apps.vendors.' . $alias . '.page.' . $page); | ||||
|         $vendor = Cache::get($key); | ||||
|  | ||||
|         if (!empty($vendor)) { | ||||
|             return $vendor; | ||||
| @@ -162,7 +156,7 @@ trait Modules | ||||
|  | ||||
|         $vendor = static::getResponseData('GET', 'apps/vendors/' . $alias, $data); | ||||
|  | ||||
|         Cache::put('apps.vendors.' . $alias . '.page.' . $page, $vendor, Date::now()->addHour()); | ||||
|         Cache::put($key, $vendor, Date::now()->addHour()); | ||||
|  | ||||
|         return $vendor; | ||||
|     } | ||||
| @@ -172,11 +166,9 @@ trait Modules | ||||
|         return static::getResponseData('GET', 'apps/my', $data); | ||||
|     } | ||||
|  | ||||
|     public function getInstalledModules($data = []) | ||||
|     public function getInstalledModules() | ||||
|     { | ||||
|         $company_id = session('company_id'); | ||||
|  | ||||
|         $key = 'apps.installed.' . $company_id . '.app'; | ||||
|         $key = 'apps.installed.' . session('company_id'); | ||||
|  | ||||
|         if ($installed = Cache::get($key)) { | ||||
|             return $installed; | ||||
| @@ -203,10 +195,9 @@ trait Modules | ||||
|  | ||||
|     public function getPreSaleModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.pre_sale.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $pre_sale = Cache::get('apps.pre_sale.page.' . $page); | ||||
|         $pre_sale = Cache::get($key); | ||||
|  | ||||
|         if (!empty($pre_sale)) { | ||||
|             return $pre_sale; | ||||
| @@ -214,17 +205,16 @@ trait Modules | ||||
|  | ||||
|         $pre_sale = static::getResponseData('GET', 'apps/pre_sale', $data); | ||||
|  | ||||
|         Cache::put('apps.pre_sale.page.' . $page, $pre_sale, Date::now()->addHour()); | ||||
|         Cache::put($key, $pre_sale, Date::now()->addHour()); | ||||
|  | ||||
|         return $pre_sale; | ||||
|     } | ||||
|  | ||||
|     public function getPaidModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.paid.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $paid = Cache::get('apps.paid.page.' . $page); | ||||
|         $paid = Cache::get($key); | ||||
|  | ||||
|         if (!empty($paid)) { | ||||
|             return $paid; | ||||
| @@ -232,17 +222,16 @@ trait Modules | ||||
|  | ||||
|         $paid = static::getResponseData('GET', 'apps/paid', $data); | ||||
|  | ||||
|         Cache::put('apps.paid.page.' . $page, $paid, Date::now()->addHour()); | ||||
|         Cache::put($key, $paid, Date::now()->addHour()); | ||||
|  | ||||
|         return $paid; | ||||
|     } | ||||
|  | ||||
|     public function getNewModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.new.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $new = Cache::get('apps.new.page.' . $page); | ||||
|         $new = Cache::get($key); | ||||
|  | ||||
|         if (!empty($new)) { | ||||
|             return $new; | ||||
| @@ -250,17 +239,16 @@ trait Modules | ||||
|  | ||||
|         $new = static::getResponseData('GET', 'apps/new', $data); | ||||
|  | ||||
|         Cache::put('apps.new.page.' . $page, $new, Date::now()->addHour()); | ||||
|         Cache::put($key, $new, Date::now()->addHour()); | ||||
|  | ||||
|         return $new; | ||||
|     } | ||||
|  | ||||
|     public function getFreeModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.free.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $free = Cache::get('apps.free.page.' . $page); | ||||
|         $free = Cache::get($key); | ||||
|  | ||||
|         if (!empty($free)) { | ||||
|             return $free; | ||||
| @@ -268,17 +256,16 @@ trait Modules | ||||
|  | ||||
|         $free = static::getResponseData('GET', 'apps/free', $data); | ||||
|  | ||||
|         Cache::put('apps.free.page.' . $page, $free, Date::now()->addHour()); | ||||
|         Cache::put($key, $free, Date::now()->addHour()); | ||||
|  | ||||
|         return $free; | ||||
|     } | ||||
|  | ||||
|     public function getFeaturedModules($data = []) | ||||
|     { | ||||
|         $page = isset($data['query']['page']) ? $data['query']['page'] : 1; | ||||
|         $key = 'apps.featured.page.' . $this->getPageNumber($data); | ||||
|  | ||||
|         // Get data from cache | ||||
|         $featured = Cache::get('apps.featured.page.' . $page); | ||||
|         $featured = Cache::get($key); | ||||
|  | ||||
|         if (!empty($featured)) { | ||||
|             return $featured; | ||||
| @@ -286,7 +273,7 @@ trait Modules | ||||
|  | ||||
|         $featured = static::getResponseData('GET', 'apps/featured', $data); | ||||
|  | ||||
|         Cache::put('apps.featured.page.' . $page, $featured, Date::now()->addHour()); | ||||
|         Cache::put($key, $featured, Date::now()->addHour()); | ||||
|  | ||||
|         return $featured; | ||||
|     } | ||||
| @@ -447,7 +434,7 @@ trait Modules | ||||
|         $company_id = session('company_id'); | ||||
|         $locale = app()->getLocale(); | ||||
|  | ||||
|         Cache::forget('installed.' . $company_id . '.module'); | ||||
|         $this->clearModulesCache(); | ||||
|  | ||||
|         $command = "module:install {$module->alias} {$company_id} {$locale}"; | ||||
|  | ||||
| @@ -481,12 +468,9 @@ trait Modules | ||||
|             'version' => $module->get('version'), | ||||
|         ]; | ||||
|  | ||||
|         Artisan::call('cache:clear'); | ||||
|  | ||||
|         $module->delete(); | ||||
|  | ||||
|         // Cache Data clear | ||||
|         File::deleteDirectory(storage_path('framework/cache/data')); | ||||
|         $this->clearModulesCache(); | ||||
|  | ||||
|         return [ | ||||
|             'success' => true, | ||||
| @@ -508,7 +492,7 @@ trait Modules | ||||
|  | ||||
|         $module->enable(); | ||||
|  | ||||
|         Artisan::call('cache:clear'); | ||||
|         $this->clearModulesCache(); | ||||
|  | ||||
|         return [ | ||||
|             'success' => true, | ||||
| @@ -530,7 +514,7 @@ trait Modules | ||||
|  | ||||
|         $module->disable(); | ||||
|  | ||||
|         Artisan::call('cache:clear'); | ||||
|         $this->clearModulesCache($alias); | ||||
|  | ||||
|         return [ | ||||
|             'success' => true, | ||||
| @@ -564,8 +548,9 @@ trait Modules | ||||
|  | ||||
|     public function loadSuggestions() | ||||
|     { | ||||
|         // Get data from cache | ||||
|         $data = Cache::get('suggestions'); | ||||
|         $key = 'apps.suggestions'; | ||||
|  | ||||
|         $data = Cache::get($key); | ||||
|  | ||||
|         if (!empty($data)) { | ||||
|             return $data; | ||||
| @@ -581,15 +566,16 @@ trait Modules | ||||
|             $data[$suggestion->path] = $suggestion; | ||||
|         } | ||||
|  | ||||
|         Cache::put('suggestions', $data, Date::now()->addHour(6)); | ||||
|         Cache::put($key, $data, Date::now()->addHour(6)); | ||||
|  | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
|     public function loadNotifications() | ||||
|     { | ||||
|         // Get data from cache | ||||
|         $data = Cache::get('notifications'); | ||||
|         $key = 'apps.notifications'; | ||||
|  | ||||
|         $data = Cache::get($key); | ||||
|  | ||||
|         if (!empty($data)) { | ||||
|             return $data; | ||||
| @@ -605,15 +591,16 @@ trait Modules | ||||
|             $data[$notification->path][] = $notification; | ||||
|         } | ||||
|  | ||||
|         Cache::put('notifications', $data, Date::now()->addHour(6)); | ||||
|         Cache::put($key, $data, Date::now()->addHour(6)); | ||||
|  | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
|     public function getSuggestions($path) | ||||
|     { | ||||
|         // Get data from cache | ||||
|         $data = Cache::get('suggestions'); | ||||
|         $key = 'apps.suggestions'; | ||||
|  | ||||
|         $data = Cache::get($key); | ||||
|  | ||||
|         if (empty($data)) { | ||||
|             $data = $this->loadSuggestions(); | ||||
| @@ -628,8 +615,9 @@ trait Modules | ||||
|  | ||||
|     public function getNotifications($path) | ||||
|     { | ||||
|         // Get data from cache | ||||
|         $data = Cache::get('notifications'); | ||||
|         $key = 'apps.notifications'; | ||||
|  | ||||
|         $data = Cache::get($key); | ||||
|  | ||||
|         if (empty($data)) { | ||||
|             $data = $this->loadNotifications(); | ||||
| @@ -641,4 +629,24 @@ trait Modules | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     public function getPageNumber($data = []) | ||||
|     { | ||||
|         if (empty($data['query']) || empty($data['query']['page'])) { | ||||
|             return 1; | ||||
|         } | ||||
|  | ||||
|         return $data['query']['page']; | ||||
|     } | ||||
|  | ||||
|     public function clearModulesCache() | ||||
|     { | ||||
|         if (config('module.cache.enabled')) { | ||||
|             Cache::forget(config('module.cache.key')); | ||||
|         } | ||||
|  | ||||
|         Cache::forget('apps.notifications'); | ||||
|         Cache::forget('apps.suggestions'); | ||||
|         Cache::forget('apps.installed.' . session('company_id')); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user