Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Rework Part 1 #413

Open
wants to merge 14 commits into
base: 5.0.x
Choose a base branch
from
25 changes: 25 additions & 0 deletions src/Config/eveapi.cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

return [
'respect_cache' => env('EVEAPI_RESPECT_CACHE', false),
];
2 changes: 2 additions & 0 deletions src/EveapiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function register()
\Seat\Eveapi\Database\Seeders\ScheduleSeeder::class,
// \Seat\Eveapi\Database\Seeders\Sde\SdeSeeder::class, -- Disabled until later implemented again in services
]);

$this->mergeConfigFrom(__DIR__ . '/Config/eveapi.cache.php', 'eveapi.cache');
}

private function addCommands()
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/Alliances/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public function handle()
'alliance_id' => $this->alliance_id,
]);

$info = $response->getBody();

$model = Alliance::firstOrNew([
'alliance_id' => $this->alliance_id,
]);

if ($this->shouldUseCache($response) && $model->exists) return; // No need to hit the DB here

$info = $response->getBody();

InfoMapping::make($model, $info, [
'alliance_id' => function () {
return $this->alliance_id;
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Alliances/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function handle()
'alliance_id' => $this->alliance_id,
]);

if ($this->shouldUseCache($response) &&
AllianceMember::where('alliance_id', $this->alliance_id)->exists())
return;

$corporation_ids = collect($response->getBody());

$corporation_ids->each(function ($corporation_id) {
Expand Down
22 changes: 17 additions & 5 deletions src/Jobs/Assets/Character/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,26 @@ public function handle(): void
$structure_batch->addStructure($asset->location_id);
}

AssetMapping::make($model, $asset, [
// The model returned here will be modified based on the esi data.
// However Laravel is smart and the isClean() method will only return false
// If the esi fields have changed. Simply applying the same data keeps a clean model.
$model = AssetMapping::make($model, $asset, [
'character_id' => function () {
return $this->getCharacterId();
},
'updated_at' => function () use ($start) {
return $start;
},
])->save();
]);

if ($model->exists && $model->isClean()){
// No ESI data updated, just touch the timestamp
$model->updated_at = $start;

// There is no point triggering events when nothing has changed
$model->saveQuietly();
} else {
// ESI data has changed. So just save the model.
// This will update the timestamp and also trigger events.
$model->save();
}
});

if (! $this->nextPage($response->getPagesCount()))
Expand Down
29 changes: 21 additions & 8 deletions src/Jobs/Assets/Corporation/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,32 @@ public function handle()
'item_id' => $asset->item_id,
]);

// The model returned here will be modified based on the esi data.
// However Laravel is smart and the isClean() method will only return false
// If the esi fields have changed. Simply applying the same data keeps a clean model.
$model = AssetMapping::make($model, $asset, [
'corporation_id' => function () {
return $this->getCorporationId();
},
]);

//make sure that the location is loaded if it is in a station or citadel
if (in_array($asset->location_flag, StructureBatch::RESOLVABLE_LOCATION_FLAGS) && in_array($asset->location_type, StructureBatch::RESOLVABLE_LOCATION_TYPES)) {
$structure_batch->addStructure($asset->location_id);
}

AssetMapping::make($model, $asset, [
'corporation_id' => function () {
return $this->getCorporationId();
},
'updated_at' => function () use ($start) {
return $start;
},
])->save();
if ($model->exists && $model->isClean()){
// No ESI data updated, just touch the timestamp
$model->updated_at = $start;

// There is no point triggering events when nothing has changed
$model->saveQuietly();
} else {
// ESI data has changed. So just save the model.
// This will update the timestamp and also trigger events.
$model->save();
}

});
});

Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/Calendar/Attendees.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ private function updateAttendees(string $owner_type, int $owner_id)
'event_id' => $event->event_id,
]);

if ($this->shouldUseCache($response) && CharacterCalendarAttendee::where('event_id', $event->event_id)->exists())
return true; // Return true to move onto the next event

$attendees = collect($response->getBody());

$attendees->each(function ($attendee) use ($event) {
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/Calendar/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ public function handle()
'event_id' => $event_id,
]);

$detail = $response->getBody();

$model = CharacterCalendarEventDetail::firstOrNew([
'event_id' => $event_id,
]);

if ($this->shouldUseCache($response) && $model->exists) return true; // Move onto the next detail if this is cached

$detail = $response->getBody();

CalendarDetailMapping::make($model, $detail, [
'event_id' => function () use ($event_id) {
return $event_id;
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/Calendar/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) && CharacterCalendarEvent::where('character_id', $this->getCharacterId())->exists())
return;

$events = collect($response->getBody());

// if we have no more entries, break the loop.
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Character/AgentsResearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterAgentResearch::where('character_id', $this->getCharacterId()->exists()))
return;

$agents = collect($response->getBody());

$agents->each(function ($agent) {
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/Character/Blueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public function handle()
'item_id' => $blueprint->item_id,
]);

// Exit early in the case that the model exists.
if ($model->exists) return;

BlueprintMapping::make($model, $blueprint, [
'character_id' => function () {
return $this->getCharacterId();
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Character/CorporationHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterCorporationHistory::where('character_id', $this->getCharacterId())->exists())
return;

$corporations = collect($response->getBody());

$corporations->each(function ($corporation) {
Expand Down
10 changes: 7 additions & 3 deletions src/Jobs/Character/Fatigue.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

$model = CharacterFatigue::firstOrNew([
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) && $model->exists) return;

$fatigue = $response->getBody();

CharacterFatigue::firstOrNew([
'character_id' => $this->getCharacterId(),
])->fill([
$model->fill([
'last_jump_date' => property_exists($fatigue, 'last_jump_date') ?
carbon($fatigue->last_jump_date) : null,
'jump_fatigue_expire_date' => property_exists($fatigue, 'jump_fatigue_expire_date') ?
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/Character/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

$info = $response->getBody();

$model = CharacterInfo::firstOrNew([
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) && $model->exists) return;

$info = $response->getBody();

InfoMapping::make($model, $info, [
'character_id' => function () {
return $this->getCharacterId();
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/Character/LoyaltyPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Seat\Eveapi\Jobs\AbstractAuthCharacterJob;
use Seat\Eveapi\Jobs\Corporation\Info as CorporationInfoJob;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Eveapi\Models\Character\CharacterLoyaltyPoints;
use Seat\Eveapi\Models\Corporation\CorporationInfo;

/**
Expand Down Expand Up @@ -85,6 +86,8 @@ public function handle()
'character_id' => $character_id,
]);

if ($this->shouldUseCache($response) && CharacterLoyaltyPoints::where('character_id', $character_id)->exists()) return;

//get the lp data as collection
$loyalty_points = collect($response->getBody());

Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Character/Medals.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterMedal::where('character_id', $this->getCharacterId())->exists())
return;

$medals = collect($response->getBody());

$medals->each(function ($medal) {
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Character/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterNotification::where('character_id', $this->getCharacterId())->exists())
return;

$notifications = collect($response->getBody());

$notifications->each(function ($notification) {
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/Character/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) && CharacterRole::where('character_id', $this->getCharacterId())->exists())
return;

$roles = $response->getBody();

foreach (['roles', 'roles_at_hq', 'roles_at_base', 'roles_at_other'] as $scope) {
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Character/Standings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterStanding::where('character_id', $this->getCharacterId())->exists())
return;

$standings = collect($response->getBody());

$standings->each(function ($standing) {
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Clones/Clones.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterClone::where('character_id', $this->getCharacterId())->exists())
return;

$clone_informations = $response->getBody();

// Populate current clone information
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Clones/Implants.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function handle()
'character_id' => $this->getCharacterId(),
]);

if ($this->shouldUseCache($response) &&
CharacterImplant::where('character_id', $this->getCharacterId())->exists())
return;

$implants = $response->getBody();

collect($implants)->each(function ($implant) {
Expand Down
17 changes: 10 additions & 7 deletions src/Jobs/Contacts/Alliance/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,22 @@ public function __construct(int $alliance_id, RefreshToken $token)
*/
public function handle()
{
while (true) {
do {

$response = $this->retrieve([
'alliance_id' => $this->getAllianceId(),
]);

$contacts = $response->getBody();

$this->known_contact_ids->push(collect($contacts)
->pluck('contact_id')->flatten()->all());

// This wont save network calls, but should save DB writes
if ($this->shouldUseCache($response) &&
AllianceContact::where('alliance_id', $this->getAllianceId())->exists())
continue; // This page has no changes so move onto the next page

collect($contacts)->each(function ($contact) {

AllianceContact::firstOrNew([
Expand All @@ -110,12 +118,7 @@ public function handle()
])->save();
});

$this->known_contact_ids->push(collect($contacts)
->pluck('contact_id')->flatten()->all());

if (! $this->nextPage($response->getPagesCount()))
break;
}
} while ($this->nextPage($response->getPagesCount()));

// Cleanup old contacts
AllianceContact::where('alliance_id', $this->getAllianceId())
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/Contacts/Alliance/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function handle()
'alliance_id' => $this->getAllianceId(),
]);

if ($this->shouldUseCache($response) &&
AllianceLabel::where('alliance_id', $this->getAllianceId())->exists())
return;

$labels = $response->getBody();

collect($labels)->each(function ($label) {
Expand Down
Loading