Update AccountService, cache object and observe changes

This commit is contained in:
Daniel Supernault 2021-02-01 00:47:54 -07:00
parent 61c71e8905
commit b299da9311
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
4 changed files with 89 additions and 11 deletions

View file

@ -5,6 +5,7 @@ namespace App\Observers;
use App\Avatar;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Services\AccountService;
class AvatarObserver
{
@ -27,7 +28,7 @@ class AvatarObserver
*/
public function updated(Avatar $avatar)
{
//
AccountService::del($avatar->profile_id);
}
/**
@ -64,6 +65,7 @@ class AvatarObserver
$disk->delete($avatar->media_path);
}
}
AccountService::del($avatar->profile_id);
}
/**

View file

@ -0,0 +1,64 @@
<?php
namespace App\Observers;
use App\Profile;
use App\Services\AccountService;
class ProfileObserver
{
/**
* Handle the Profile "created" event.
*
* @param \App\Profile $profile
* @return void
*/
public function created(Profile $profile)
{
//
}
/**
* Handle the Profile "updated" event.
*
* @param \App\Profile $profile
* @return void
*/
public function updated(Profile $profile)
{
AccountService::del($profile->id);
}
/**
* Handle the Profile "deleted" event.
*
* @param \App\Profile $profile
* @return void
*/
public function deleted(Profile $profile)
{
AccountService::del($profile->id);
}
/**
* Handle the Profile "restored" event.
*
* @param \App\Profile $profile
* @return void
*/
public function restored(Profile $profile)
{
//
}
/**
* Handle the Profile "force deleted" event.
*
* @param \App\Profile $profile
* @return void
*/
public function forceDeleted(Profile $profile)
{
//
}
}

View file

@ -6,6 +6,7 @@ use App\Observers\{
AvatarObserver,
NotificationObserver,
ModLogObserver,
ProfileObserver,
StatusHashtagObserver,
UserObserver,
UserFilterObserver,
@ -14,6 +15,7 @@ use App\{
Avatar,
Notification,
ModLog,
Profile,
StatusHashtag,
User,
UserFilter
@ -41,6 +43,7 @@ class AppServiceProvider extends ServiceProvider
Avatar::observe(AvatarObserver::class);
Notification::observe(NotificationObserver::class);
ModLog::observe(ModLogObserver::class);
Profile::observe(ProfileObserver::class);
StatusHashtag::observe(StatusHashtagObserver::class);
User::observe(UserObserver::class);
UserFilter::observe(UserFilterObserver::class);

View file

@ -14,16 +14,25 @@ class AccountService {
public static function get($id)
{
// $key = self::CACHE_KEY . ':' . $id;
// $ttl = now()->addSeconds(10);
// return Cache::remember($key, $ttl, function() use($id) {
// });
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$profile = Profile::whereNull('status')->findOrFail($id);
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return $fractal->createData($resource)->toArray();
if($id > PHP_INT_MAX || $id < 1) {
return [];
}
$key = self::CACHE_KEY . $id;
$ttl = now()->addMinutes(15);
return Cache::remember($key, $ttl, function() use($id) {
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$profile = Profile::whereNull('status')->findOrFail($id);
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return $fractal->createData($resource)->toArray();
});
}
public static function del($id)
{
return Cache::forget(self::CACHE_KEY . $id);
}
}