Add avatar cache busting logic

This commit is contained in:
Daniel Supernault 2018-08-12 21:51:05 -06:00
parent 4cf58e1539
commit d6970d930f
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
3 changed files with 17 additions and 8 deletions

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use Auth; use Auth, Cache;
use App\{ use App\{
Avatar, Avatar,
Like, Like,
@ -83,15 +83,16 @@ class BaseApiController extends Controller
]); ]);
try { try {
$user = Auth::user(); $user = Auth::user();
$profile = $user->profile;
$file = $request->file('upload'); $file = $request->file('upload');
$path = (new AvatarController())->getPath($user, $file); $path = (new AvatarController())->getPath($user, $file);
$dir = $path['root']; $dir = $path['root'];
$name = $path['name']; $name = $path['name'];
$public = $path['storage']; $public = $path['storage'];
$currentAvatar = storage_path('app/'.$user->profile->avatar->media_path); $currentAvatar = storage_path('app/'.$profile->avatar->media_path);
$loc = $request->file('upload')->storeAs($public, $name); $loc = $request->file('upload')->storeAs($public, $name);
$avatar = Avatar::whereProfileId($user->profile->id)->firstOrFail(); $avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
$opath = $avatar->media_path; $opath = $avatar->media_path;
$avatar->media_path = "$public/$name"; $avatar->media_path = "$public/$name";
$avatar->thumb_path = null; $avatar->thumb_path = null;
@ -99,6 +100,7 @@ class BaseApiController extends Controller
$avatar->last_processed_at = null; $avatar->last_processed_at = null;
$avatar->save(); $avatar->save();
Cache::forget("avatar:{$profile->id}");
AvatarOptimize::dispatch($user->profile, $currentAvatar); AvatarOptimize::dispatch($user->profile, $currentAvatar);
} catch (Exception $e) { } catch (Exception $e) {
} }

View File

@ -3,7 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Auth, Log, Storage; use Auth, Cache, Log, Storage;
use App\Avatar; use App\Avatar;
use App\Jobs\AvatarPipeline\AvatarOptimize; use App\Jobs\AvatarPipeline\AvatarOptimize;
@ -21,15 +21,16 @@ class AvatarController extends Controller
]); ]);
try { try {
$user = Auth::user(); $user = Auth::user();
$profile = $user->profile;
$file = $request->file('avatar'); $file = $request->file('avatar');
$path = $this->getPath($user, $file); $path = $this->getPath($user, $file);
$dir = $path['root']; $dir = $path['root'];
$name = $path['name']; $name = $path['name'];
$public = $path['storage']; $public = $path['storage'];
$currentAvatar = storage_path('app/'.$user->profile->avatar->media_path); $currentAvatar = storage_path('app/'.$profile->avatar->media_path);
$loc = $request->file('avatar')->storeAs($public, $name); $loc = $request->file('avatar')->storeAs($public, $name);
$avatar = Avatar::whereProfileId($user->profile->id)->firstOrFail(); $avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
$opath = $avatar->media_path; $opath = $avatar->media_path;
$avatar->media_path = "$public/$name"; $avatar->media_path = "$public/$name";
$avatar->thumb_path = null; $avatar->thumb_path = null;
@ -37,6 +38,7 @@ class AvatarController extends Controller
$avatar->last_processed_at = null; $avatar->last_processed_at = null;
$avatar->save(); $avatar->save();
Cache::forget("avatar:{$profile->id}");
AvatarOptimize::dispatch($user->profile, $currentAvatar); AvatarOptimize::dispatch($user->profile, $currentAvatar);
} catch (Exception $e) { } catch (Exception $e) {
} }

View File

@ -2,7 +2,7 @@
namespace App; namespace App;
use Auth, Storage; use Auth, Cache, Storage;
use App\Util\Lexer\PrettyNumber; use App\Util\Lexer\PrettyNumber;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -130,7 +130,12 @@ class Profile extends Model
public function avatarUrl() public function avatarUrl()
{ {
$url = url(Storage::url($this->avatar->media_path ?? 'public/avatars/default.png')); $url = Cache::remember("avatar:{$this->id}", 1440, function() {
$path = $this->avatar->media_path ?? 'public/avatars/default.png';
$version = hash('sha1', $this->avatar->created_at);
$path = "{$path}?v={$version}";
return url(Storage::url($path));
});
return $url; return $url;
} }