Skip to content

Commit cd0a45a

Browse files
committed
Fixes caching to actually clear the cache for *all* users, rather than the logged in user by using cache tags.
1 parent 5d59d36 commit cd0a45a

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1212
### Fixed
1313
* Fixes potential bug with invalid CIDR notation (ex: `192.168.1.1/z`) when adding allocations that could cause over 4 million records to be created at once.
1414
* `[pre.4]` — Fixes bug preventing server updates from occurring by the system due to undefined `Auth::user()` in the event listener.
15+
* `[pre.4]` — Fixes `Server::byUuid()` caching to actually clear the cache for *all* users, rather than the logged in user by using cache tags.
1516

1617
### Added
1718
* Ability to assign multiple allocations at once when creating a new server.

app/Models/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use Auth;
2828
use Cache;
29+
use Carbon;
2930
use Javascript;
3031
use Illuminate\Database\Eloquent\Model;
3132
use Illuminate\Notifications\Notifiable;
@@ -113,7 +114,7 @@ class Server extends Model
113114
public static function byUuid($uuid)
114115
{
115116
// Results are cached because we call this functions a few times on page load.
116-
$result = Cache::remember('Server.byUuid.' . $uuid . Auth::user()->uuid, 60, function () use ($uuid) {
117+
$result = Cache::tags(['Model:Server', 'Model:Server:byUuid:' . $uuid])->remember('Model:Server:byUuid:' . $uuid . Auth::user()->uuid, Carbon::now()->addMinutes(15), function () use ($uuid) {
117118
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
118119
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
119120
});

app/Observers/ServerObserver.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace Pterodactyl\Observers;
2626

27-
use Auth;
2827
use Cache;
2928
use Carbon;
3029
use Pterodactyl\Events;
@@ -141,11 +140,15 @@ public function updating(Server $server)
141140
*/
142141
public function updated(Server $server)
143142
{
144-
// Clear Caches
145-
if (Auth::user()) {
146-
Cache::forget('Server.byUuid.' . $server->uuid . Auth::user()->uuid);
147-
Cache::forget('Server.byUuid.' . $server->uuidShort . Auth::user()->uuid);
148-
}
143+
/**
144+
* The cached byUuid model calls are tagged with Model:Server:byUuid:<uuid>
145+
* so that they can be accessed regardless of if there is an Auth::user()
146+
* defined or not.
147+
*
148+
* We can also delete all cached byUuid items using the Model:Server tag.
149+
*/
150+
Cache::tags('Model:Server:byUuid:' . $server->uuid)->flush();
151+
Cache::tags('Model:Server:byUuid:' . $server->uuidShort)->flush();
149152

150153
event(new Events\Server\Updated($server));
151154
}

0 commit comments

Comments
 (0)