mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-12-25 01:05:36 +00:00
Update StatusService, add ttl of 7 days
This commit is contained in:
parent
c898dc5408
commit
6e44ae0b64
1 changed files with 18 additions and 24 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use App\Status;
|
use App\Status;
|
||||||
//use App\Transformer\Api\v3\StatusTransformer;
|
//use App\Transformer\Api\v3\StatusTransformer;
|
||||||
|
@ -15,34 +16,27 @@ class StatusService {
|
||||||
|
|
||||||
const CACHE_KEY = 'pf:services:status:';
|
const CACHE_KEY = 'pf:services:status:';
|
||||||
|
|
||||||
public static function get($id)
|
public static function key($id)
|
||||||
{
|
{
|
||||||
return json_decode(Redis::get(self::CACHE_KEY . $id) ?? self::coldGet($id), true);
|
return self::CACHE_KEY . $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function coldGet($id)
|
public static function get($id)
|
||||||
{
|
{
|
||||||
$status = Status::whereScope('public')->findOrFail($id);
|
return Cache::remember(self::key($id), now()->addDays(7), function() use($id) {
|
||||||
|
$status = Status::whereScope('public')->find($id);
|
||||||
|
if(!$status) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
$fractal = new Fractal\Manager();
|
$fractal = new Fractal\Manager();
|
||||||
$fractal->setSerializer(new ArraySerializer());
|
$fractal->setSerializer(new ArraySerializer());
|
||||||
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
||||||
$res = $fractal->createData($resource)->toJson();
|
return $fractal->createData($resource)->toArray();
|
||||||
self::set($id, $res);
|
});
|
||||||
return $res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function set($key, $val)
|
public static function del($id)
|
||||||
{
|
{
|
||||||
return Redis::set(self::CACHE_KEY . $key, $val);
|
return Cache::forget(self::key($id));
|
||||||
}
|
|
||||||
|
|
||||||
public static function del($key)
|
|
||||||
{
|
|
||||||
return Redis::del(self::CACHE_KEY . $key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function rem($key)
|
|
||||||
{
|
|
||||||
return self::del($key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue