Skip to content

Instantly share code, notes, and snippets.

@mishazapl
Created October 4, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishazapl/c35294c171a5a3018a3068222a719879 to your computer and use it in GitHub Desktop.
Save mishazapl/c35294c171a5a3018a3068222a719879 to your computer and use it in GitHub Desktop.
<?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);
}
}
}
<?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