Add in query caching on server view

This commit is contained in:
Dane Everitt 2017-02-17 18:19:53 -05:00
parent ff9904227b
commit 9af06b4b59
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 263 additions and 19 deletions

View file

@ -134,6 +134,10 @@ class UpdateEnvironment extends Command
$variables['APP_TIMEZONE'] = $this->option('timezone');
}
$variables['APP_THEME'] = 'pterodactyl';
$variables['CACHE_DRIVER'] = 'memcached';
$variables['SESSION_DRIVER'] = 'database';
$bar = $this->output->createProgressBar(count($variables));
$this->line('Writing new environment configuration to file.');

View file

@ -0,0 +1,45 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Events\Server;
use Pterodactyl\Models\Server;
use Illuminate\Queue\SerializesModels;
class Saved
{
use SerializesModels;
public $server;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Server $server)
{
$this->server = $server;
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Events\Server;
use Pterodactyl\Models\Server;
use Illuminate\Queue\SerializesModels;
class Saving
{
use SerializesModels;
public $server;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Server $server)
{
$this->server = $server;
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Events\Server;
use Pterodactyl\Models\Server;
use Illuminate\Queue\SerializesModels;
class Updated
{
use SerializesModels;
public $server;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Server $server)
{
$this->server = $server;
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Events\Server;
use Pterodactyl\Models\Server;
use Illuminate\Queue\SerializesModels;
class Updating
{
use SerializesModels;
public $server;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Server $server)
{
$this->server = $server;
}
}

View file

@ -47,7 +47,7 @@ class IndexController extends Controller
public function getIndex(Request $request)
{
return view('base.index', [
'servers' => $request->user()->serverAccessCollection(10)->load('node', 'allocation'),
'servers' => $request->user()->serverAccessCollection(10),
]);
}

View file

@ -25,6 +25,7 @@
namespace Pterodactyl\Models;
use Auth;
use Cache;
use Javascript;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
@ -113,16 +114,19 @@ class Server extends Model
*/
public static function byUuid($uuid)
{
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
// Results are cached because we call this functions a few times on page load.
$result = Cache::remember('Server.byUuid.' . $uuid, 60, function () use ($uuid) {
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
});
if (! Auth::user()->isRootAdmin()) {
$query->whereIn('id', Auth::user()->serverAccessArray());
}
return $query->first();
});
if (! Auth::user()->isRootAdmin()) {
$query->whereIn('id', Auth::user()->serverAccessArray());
}
$result = $query->first();
if (! is_null($result)) {
$result->daemonSecret = Auth::user()->daemonToken($result);
}

View file

@ -186,9 +186,9 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
*
* @return Collection
*/
public function serverAccessCollection($paginate = null)
public function serverAccessCollection($paginate = null, $load = ['service', 'node', 'allocation'])
{
$query = Server::with('service', 'node');
$query = Server::with($load);
if (! $this->isRootAdmin()) {
$query->whereIn('id', $this->serverAccessArray());
}

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Observers;
use Cache;
use Carbon;
use Pterodactyl\Events;
use Pterodactyl\Models\Server;
@ -97,4 +98,52 @@ class ServerObserver
->onQueue(env('QUEUE_STANDARD', 'standard'))
);
}
/**
* Listen to the Server saving event.
*
* @param Server $server [description]
* @return [type] [description]
*/
public function saving(Server $server)
{
event(new Events\Server\Saving($server));
}
/**
* Listen to the Server saved event.
*
* @param Server $server [description]
* @return [type] [description]
*/
public function saved(Server $server)
{
event(new Events\Server\Saved($server));
}
/**
* Listen to the Server saving event.
*
* @param Server $server [description]
* @return [type] [description]
*/
public function updating(Server $server)
{
event(new Events\Server\Updating($server));
}
/**
* Listen to the Server saved event.
*
* @param Server $server [description]
* @return [type] [description]
*/
public function updated(Server $server)
{
// Clear Caches
Cache::forget('Server.byUuid.' . $server->uuid);
Cache::forget('Server.byUuid.' . $server->uuidShort);
event(new Events\Server\Updated($server));
}
}