Skip to content

Commit 9af06b4

Browse files
committed
Add in query caching on server view
1 parent ff99042 commit 9af06b4

File tree

14 files changed

+262
-18
lines changed

14 files changed

+262
-18
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ DB_DATABASE=homestead
1313
DB_USERNAME=homestead
1414
DB_PASSWORD=secret
1515

16-
CACHE_DRIVER=file
16+
CACHE_DRIVER=memcached
1717
SESSION_DRIVER=database
1818

1919
MAIL_DRIVER=smtp

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
66
## v0.6.0-pre.3 (Courageous Carniadactylus)
77
### Fixed
88
* `[pre.2]` — Fixes bug where servers could not be manually deployed to nodes due to a broken SQL call.
9+
* `[pre.2]` — Fixes inability to edit a server due to owner_id issues.
10+
11+
### Changed
12+
* `[pre.2]` — File Manager now displays relevant information on all screen sizes, and includes better button clicking mechanics for dropdown menu.
13+
* Reduced the number of database queries being executed when viewing a specific server. This is done by caching the query for up to 60 minutes in memcached.
914

1015
## v0.6.0-pre.2 (Courageous Carniadactylus)
1116
### Fixed

app/Console/Commands/UpdateEnvironment.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public function handle()
134134
$variables['APP_TIMEZONE'] = $this->option('timezone');
135135
}
136136

137+
$variables['APP_THEME'] = 'pterodactyl';
138+
$variables['CACHE_DRIVER'] = 'memcached';
139+
$variables['SESSION_DRIVER'] = 'database';
140+
137141
$bar = $this->output->createProgressBar(count($variables));
138142

139143
$this->line('Writing new environment configuration to file.');

app/Events/Server/Saved.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Events\Server;
26+
27+
use Pterodactyl\Models\Server;
28+
use Illuminate\Queue\SerializesModels;
29+
30+
class Saved
31+
{
32+
use SerializesModels;
33+
34+
public $server;
35+
36+
/**
37+
* Create a new event instance.
38+
*
39+
* @return void
40+
*/
41+
public function __construct(Server $server)
42+
{
43+
$this->server = $server;
44+
}
45+
}

app/Events/Server/Saving.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Events\Server;
26+
27+
use Pterodactyl\Models\Server;
28+
use Illuminate\Queue\SerializesModels;
29+
30+
class Saving
31+
{
32+
use SerializesModels;
33+
34+
public $server;
35+
36+
/**
37+
* Create a new event instance.
38+
*
39+
* @return void
40+
*/
41+
public function __construct(Server $server)
42+
{
43+
$this->server = $server;
44+
}
45+
}

app/Events/Server/Updated.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Events\Server;
26+
27+
use Pterodactyl\Models\Server;
28+
use Illuminate\Queue\SerializesModels;
29+
30+
class Updated
31+
{
32+
use SerializesModels;
33+
34+
public $server;
35+
36+
/**
37+
* Create a new event instance.
38+
*
39+
* @return void
40+
*/
41+
public function __construct(Server $server)
42+
{
43+
$this->server = $server;
44+
}
45+
}

app/Events/Server/Updating.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Events\Server;
26+
27+
use Pterodactyl\Models\Server;
28+
use Illuminate\Queue\SerializesModels;
29+
30+
class Updating
31+
{
32+
use SerializesModels;
33+
34+
public $server;
35+
36+
/**
37+
* Create a new event instance.
38+
*
39+
* @return void
40+
*/
41+
public function __construct(Server $server)
42+
{
43+
$this->server = $server;
44+
}
45+
}

app/Http/Controllers/Base/IndexController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct()
4747
public function getIndex(Request $request)
4848
{
4949
return view('base.index', [
50-
'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'),
50+
'servers' => $request->user()->serverAccessCollection(10),
5151
]);
5252
}
5353

app/Models/Server.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Pterodactyl\Models;
2626

2727
use Auth;
28+
use Cache;
2829
use Javascript;
2930
use Illuminate\Database\Eloquent\Model;
3031
use Illuminate\Notifications\Notifiable;
@@ -113,15 +114,18 @@ public function __construct()
113114
*/
114115
public static function byUuid($uuid)
115116
{
116-
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
117-
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
118-
});
117+
// Results are cached because we call this functions a few times on page load.
118+
$result = Cache::remember('Server.byUuid.' . $uuid, 60, function () use ($uuid) {
119+
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
120+
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
121+
});
119122

120-
if (! Auth::user()->isRootAdmin()) {
121-
$query->whereIn('id', Auth::user()->serverAccessArray());
122-
}
123+
if (! Auth::user()->isRootAdmin()) {
124+
$query->whereIn('id', Auth::user()->serverAccessArray());
125+
}
123126

124-
$result = $query->first();
127+
return $query->first();
128+
});
125129

126130
if (! is_null($result)) {
127131
$result->daemonSecret = Auth::user()->daemonToken($result);

app/Models/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ public function serverAccessArray()
186186
*
187187
* @return Collection
188188
*/
189-
public function serverAccessCollection($paginate = null)
189+
public function serverAccessCollection($paginate = null, $load = ['service', 'node', 'allocation'])
190190
{
191-
$query = Server::with('service', 'node');
191+
$query = Server::with($load);
192192
if (! $this->isRootAdmin()) {
193193
$query->whereIn('id', $this->serverAccessArray());
194194
}

0 commit comments

Comments
 (0)