Created
October 4, 2018 10:13
-
-
Save mishazapl/c35294c171a5a3018a3068222a719879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 22.08.18 | |
* Time: 13:51 | |
*/ | |
namespace App\Services\Repositories; | |
abstract class CacheRepository | |
{ | |
protected $minute = 1; | |
protected $forever = false; | |
/** | |
* @param $minute | |
*/ | |
public function setMinute(int $minute): void | |
{ | |
$this->minute = $minute; | |
} | |
/** | |
* @return int | |
*/ | |
public function getMinute(): int | |
{ | |
return $this->minute; | |
} | |
/** | |
* @param bool $forever | |
*/ | |
public function setForever(bool $forever): void | |
{ | |
$this->forever = $forever; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isForever(): bool | |
{ | |
return $this->forever; | |
} | |
/** | |
* @param $key | |
* @return \Illuminate\Cache\CacheManager|mixed | |
* @throws \Exception | |
*/ | |
public function getCache($key) | |
{ | |
return cache($key); | |
} | |
/** | |
* @param callable $func | |
* @param $key | |
* @return \Illuminate\Cache\CacheManager|mixed | |
* @throws \Exception | |
*/ | |
protected function start(callable $func, $key) | |
{ | |
$result = $this->getCache($key); | |
if (!empty($result)) | |
return $result; | |
$value = $func(); | |
$this->setCache($key, $value); | |
return $value; | |
} | |
/** | |
* @param $key | |
* @param $value | |
* @throws \Exception | |
*/ | |
protected function setCache($key, $value): void | |
{ | |
if ($this->forever) { | |
cache()->forever($key, $value); | |
} else { | |
cache()->put($key, $value, $this->minute); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 22.08.18 | |
* Time: 13:51 | |
*/ | |
namespace App\Services\Repositories; | |
use App\Models\Offers\BusinessOffer; | |
class OfferRepository extends CacheRepository | |
{ | |
private const KEY_STATUS = 'statuses-offers'; | |
/** | |
* @throws \Exception | |
*/ | |
public function getStatuses(): array | |
{ | |
return $this->start(function () { | |
$countStatuses = []; | |
$statuses = BusinessOffer::STATUS; | |
foreach ($statuses as $key => $value) { | |
$count = BusinessOffer::where('status', $key) | |
->count('status'); | |
$countStatuses[$key] = $count; | |
} | |
return $countStatuses; | |
}, self::KEY_STATUS); | |
} | |
/** | |
* @return \Illuminate\Cache\CacheManager|mixed | |
* @throws \Exception | |
*/ | |
public function getStatusesCache(): array | |
{ | |
$value = $this->getCache(self::KEY_STATUS); | |
if (empty($value)) { | |
return $this->getStatuses(); | |
} else { | |
return $value; | |
} | |
} | |
/** | |
* @throws \Exception | |
*/ | |
public function setStatusesCache(): void | |
{ | |
$countStatuses = []; | |
$statuses = BusinessOffer::STATUS; | |
foreach ($statuses as $key => $value) { | |
$count = BusinessOffer::where('status', $key) | |
->count('status'); | |
$countStatuses[$key] = $count; | |
} | |
$this->forever = true; | |
$this->setCache(self::KEY_STATUS, $countStatuses); | |
} | |
public static function callSetStatuses() | |
{ | |
$repository = resolve(OfferRepository::class); | |
$repository->setStatusesCache(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment