2019-06-11 02:10:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2020-12-13 23:08:59 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2019-06-11 02:10:57 +00:00
|
|
|
use App\Status;
|
2020-12-13 23:08:59 +00:00
|
|
|
//use App\Transformer\Api\v3\StatusTransformer;
|
2019-06-11 02:10:57 +00:00
|
|
|
use App\Transformer\Api\StatusStatelessTransformer;
|
2020-12-13 23:08:59 +00:00
|
|
|
use App\Transformer\Api\StatusTransformer;
|
2019-06-11 02:10:57 +00:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
|
|
|
|
class StatusService {
|
|
|
|
|
|
|
|
const CACHE_KEY = 'pf:services:status:';
|
|
|
|
|
|
|
|
public static function get($id)
|
|
|
|
{
|
2020-12-13 23:08:59 +00:00
|
|
|
return json_decode(Redis::get(self::CACHE_KEY . $id) ?? self::coldGet($id));
|
2019-06-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function coldGet($id)
|
|
|
|
{
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
|
|
|
$res = $fractal->createData($resource)->toJson();
|
|
|
|
self::set($id, $res);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function set($key, $val)
|
|
|
|
{
|
|
|
|
return Redis::set(self::CACHE_KEY . $key, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function del($key)
|
|
|
|
{
|
|
|
|
return Redis::del(self::CACHE_KEY . $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function rem($key)
|
|
|
|
{
|
|
|
|
return self::del($key);
|
|
|
|
}
|
|
|
|
}
|