Skip to content

Commit 6b648f9

Browse files
authored
Cache Rework Part 1... (#421)
* feat: cache reintroduction 1 * feat: decrease load of asset updates * feat: decrease load of asset updates corp * fix: exists() is faster than count() * feat: cache reintroduction 1 * feat: decrease load of asset updates * feat: decrease load of asset updates corp * fix: exists() is faster than count() * feat: add cache behaviour configurability * style: styleci * feat: cache rework continues * style: styleci * keep v5 cache default
1 parent ec0017a commit 6b648f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+334
-132
lines changed

src/Config/eveapi.cache.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of SeAT
5+
*
6+
* Copyright (C) 2015 to present Leon Jacobs
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along
19+
* with this program; if not, write to the Free Software Foundation, Inc.,
20+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*/
22+
23+
return [
24+
'respect_cache' => env('EVEAPI_RESPECT_CACHE', false),
25+
];

src/EveapiServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function register()
7171
\Seat\Eveapi\Database\Seeders\ScheduleSeeder::class,
7272
// \Seat\Eveapi\Database\Seeders\Sde\SdeSeeder::class, -- Disabled until later implemented again in services
7373
]);
74+
75+
$this->mergeConfigFrom(__DIR__ . '/Config/eveapi.cache.php', 'eveapi.cache');
7476
}
7577

7678
private function addCommands()

src/Jobs/Alliances/Info.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public function handle()
6060
'alliance_id' => $this->alliance_id,
6161
]);
6262

63-
$info = $response->getBody();
64-
6563
$model = Alliance::firstOrNew([
6664
'alliance_id' => $this->alliance_id,
6765
]);
6866

67+
if ($this->shouldUseCache($response) && $model->exists) return; // No need to hit the DB here
68+
69+
$info = $response->getBody();
70+
6971
InfoMapping::make($model, $info, [
7072
'alliance_id' => function () {
7173
return $this->alliance_id;

src/Jobs/Alliances/Members.php

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function handle()
5757
'alliance_id' => $this->alliance_id,
5858
]);
5959

60+
if ($this->shouldUseCache($response) &&
61+
AllianceMember::where('alliance_id', $this->alliance_id)->exists())
62+
return;
63+
6064
$corporation_ids = collect($response->getBody());
6165

6266
$corporation_ids->each(function ($corporation_id) {

src/Jobs/Assets/Character/Assets.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,26 @@ public function handle(): void
109109
$structure_batch->addStructure($asset->location_id);
110110
}
111111

112-
AssetMapping::make($model, $asset, [
112+
// The model returned here will be modified based on the esi data.
113+
// However Laravel is smart and the isClean() method will only return false
114+
// If the esi fields have changed. Simply applying the same data keeps a clean model.
115+
$model = AssetMapping::make($model, $asset, [
113116
'character_id' => function () {
114117
return $this->getCharacterId();
115118
},
116-
'updated_at' => function () use ($start) {
117-
return $start;
118-
},
119-
])->save();
119+
]);
120+
121+
if ($model->exists && $model->isClean()){
122+
// No ESI data updated, just touch the timestamp
123+
$model->updated_at = $start;
124+
125+
// There is no point triggering events when nothing has changed
126+
$model->saveQuietly();
127+
} else {
128+
// ESI data has changed. So just save the model.
129+
// This will update the timestamp and also trigger events.
130+
$model->save();
131+
}
120132
});
121133

122134
if (! $this->nextPage($response->getPagesCount()))

src/Jobs/Assets/Corporation/Assets.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,32 @@ public function handle()
112112
'item_id' => $asset->item_id,
113113
]);
114114

115+
// The model returned here will be modified based on the esi data.
116+
// However Laravel is smart and the isClean() method will only return false
117+
// If the esi fields have changed. Simply applying the same data keeps a clean model.
118+
$model = AssetMapping::make($model, $asset, [
119+
'corporation_id' => function () {
120+
return $this->getCorporationId();
121+
},
122+
]);
123+
115124
//make sure that the location is loaded if it is in a station or citadel
116125
if (in_array($asset->location_flag, StructureBatch::RESOLVABLE_LOCATION_FLAGS) && in_array($asset->location_type, StructureBatch::RESOLVABLE_LOCATION_TYPES)) {
117126
$structure_batch->addStructure($asset->location_id);
118127
}
119128

120-
AssetMapping::make($model, $asset, [
121-
'corporation_id' => function () {
122-
return $this->getCorporationId();
123-
},
124-
'updated_at' => function () use ($start) {
125-
return $start;
126-
},
127-
])->save();
129+
if ($model->exists && $model->isClean()){
130+
// No ESI data updated, just touch the timestamp
131+
$model->updated_at = $start;
132+
133+
// There is no point triggering events when nothing has changed
134+
$model->saveQuietly();
135+
} else {
136+
// ESI data has changed. So just save the model.
137+
// This will update the timestamp and also trigger events.
138+
$model->save();
139+
}
140+
128141
});
129142
});
130143

src/Jobs/Calendar/Attendees.php

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ private function updateAttendees(string $owner_type, int $owner_id)
9191
'event_id' => $event->event_id,
9292
]);
9393

94+
if ($this->shouldUseCache($response) && CharacterCalendarAttendee::where('event_id', $event->event_id)->exists())
95+
return true; // Return true to move onto the next event
96+
9497
$attendees = collect($response->getBody());
9598

9699
$attendees->each(function ($attendee) use ($event) {

src/Jobs/Calendar/Detail.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ public function handle()
8787
'event_id' => $event_id,
8888
]);
8989

90-
$detail = $response->getBody();
91-
9290
$model = CharacterCalendarEventDetail::firstOrNew([
9391
'event_id' => $event_id,
9492
]);
9593

94+
if ($this->shouldUseCache($response) && $model->exists) return true; // Move onto the next detail if this is cached
95+
96+
$detail = $response->getBody();
97+
9698
CalendarDetailMapping::make($model, $detail, [
9799
'event_id' => function () use ($event_id) {
98100
return $event_id;

src/Jobs/Calendar/Events.php

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public function handle()
8686
'character_id' => $this->getCharacterId(),
8787
]);
8888

89+
if ($this->shouldUseCache($response) && CharacterCalendarEvent::where('character_id', $this->getCharacterId())->exists())
90+
return;
91+
8992
$events = collect($response->getBody());
9093

9194
// if we have no more entries, break the loop.

src/Jobs/Character/AgentsResearch.php

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function handle()
7474
'character_id' => $this->getCharacterId(),
7575
]);
7676

77+
if ($this->shouldUseCache($response) &&
78+
CharacterAgentResearch::where('character_id', $this->getCharacterId()->exists()))
79+
return;
80+
7781
$agents = collect($response->getBody());
7882

7983
$agents->each(function ($agent) {

src/Jobs/Character/Blueprints.php

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public function handle()
122122
'item_id' => $blueprint->item_id,
123123
]);
124124

125+
// Exit early in the case that the model exists.
126+
if ($model->exists) return;
127+
125128
BlueprintMapping::make($model, $blueprint, [
126129
'character_id' => function () {
127130
return $this->getCharacterId();

src/Jobs/Character/CorporationHistory.php

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function handle()
6262
'character_id' => $this->getCharacterId(),
6363
]);
6464

65+
if ($this->shouldUseCache($response) &&
66+
CharacterCorporationHistory::where('character_id', $this->getCharacterId())->exists())
67+
return;
68+
6569
$corporations = collect($response->getBody());
6670

6771
$corporations->each(function ($corporation) {

src/Jobs/Character/Fatigue.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ public function handle()
7373
'character_id' => $this->getCharacterId(),
7474
]);
7575

76+
$model = CharacterFatigue::firstOrNew([
77+
'character_id' => $this->getCharacterId(),
78+
]);
79+
80+
if ($this->shouldUseCache($response) && $model->exists) return;
81+
7682
$fatigue = $response->getBody();
7783

78-
CharacterFatigue::firstOrNew([
79-
'character_id' => $this->getCharacterId(),
80-
])->fill([
84+
$model->fill([
8185
'last_jump_date' => property_exists($fatigue, 'last_jump_date') ?
8286
carbon($fatigue->last_jump_date) : null,
8387
'jump_fatigue_expire_date' => property_exists($fatigue, 'jump_fatigue_expire_date') ?

src/Jobs/Character/Info.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ public function handle()
6969
'character_id' => $this->getCharacterId(),
7070
]);
7171

72-
$info = $response->getBody();
73-
7472
$model = CharacterInfo::firstOrNew([
7573
'character_id' => $this->getCharacterId(),
7674
]);
7775

76+
if ($this->shouldUseCache($response) && $model->exists) return;
77+
78+
$info = $response->getBody();
79+
7880
InfoMapping::make($model, $info, [
7981
'character_id' => function () {
8082
return $this->getCharacterId();

src/Jobs/Character/LoyaltyPoints.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Seat\Eveapi\Jobs\AbstractAuthCharacterJob;
2626
use Seat\Eveapi\Jobs\Corporation\Info as CorporationInfoJob;
2727
use Seat\Eveapi\Models\Character\CharacterInfo;
28+
use Seat\Eveapi\Models\Character\CharacterLoyaltyPoints;
2829
use Seat\Eveapi\Models\Corporation\CorporationInfo;
2930

3031
/**
@@ -85,6 +86,8 @@ public function handle()
8586
'character_id' => $character_id,
8687
]);
8788

89+
if ($this->shouldUseCache($response) && CharacterLoyaltyPoints::where('character_id', $character_id)->exists()) return;
90+
8891
//get the lp data as collection
8992
$loyalty_points = collect($response->getBody());
9093

src/Jobs/Character/Medals.php

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function handle()
7474
'character_id' => $this->getCharacterId(),
7575
]);
7676

77+
if ($this->shouldUseCache($response) &&
78+
CharacterMedal::where('character_id', $this->getCharacterId())->exists())
79+
return;
80+
7781
$medals = collect($response->getBody());
7882

7983
$medals->each(function ($medal) {

src/Jobs/Character/Notifications.php

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function handle()
7575
'character_id' => $this->getCharacterId(),
7676
]);
7777

78+
if ($this->shouldUseCache($response) &&
79+
CharacterNotification::where('character_id', $this->getCharacterId())->exists())
80+
return;
81+
7882
$notifications = collect($response->getBody());
7983

8084
$notifications->each(function ($notification) {

src/Jobs/Character/Roles.php

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function handle()
7373
'character_id' => $this->getCharacterId(),
7474
]);
7575

76+
if ($this->shouldUseCache($response) && CharacterRole::where('character_id', $this->getCharacterId())->exists())
77+
return;
78+
7679
$roles = $response->getBody();
7780

7881
foreach (['roles', 'roles_at_hq', 'roles_at_base', 'roles_at_other'] as $scope) {

src/Jobs/Character/Standings.php

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function handle()
7373
'character_id' => $this->getCharacterId(),
7474
]);
7575

76+
if ($this->shouldUseCache($response) &&
77+
CharacterStanding::where('character_id', $this->getCharacterId())->exists())
78+
return;
79+
7680
$standings = collect($response->getBody());
7781

7882
$standings->each(function ($standing) {

src/Jobs/Clones/Clones.php

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public function handle()
7777
'character_id' => $this->getCharacterId(),
7878
]);
7979

80+
if ($this->shouldUseCache($response) &&
81+
CharacterClone::where('character_id', $this->getCharacterId())->exists())
82+
return;
83+
8084
$clone_informations = $response->getBody();
8185

8286
// Populate current clone information

src/Jobs/Clones/Implants.php

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function handle()
7373
'character_id' => $this->getCharacterId(),
7474
]);
7575

76+
if ($this->shouldUseCache($response) &&
77+
CharacterImplant::where('character_id', $this->getCharacterId())->exists())
78+
return;
79+
7680
$implants = $response->getBody();
7781

7882
collect($implants)->each(function ($implant) {

src/Jobs/Contacts/Alliance/Contacts.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,22 @@ public function __construct(int $alliance_id, RefreshToken $token)
9090
*/
9191
public function handle()
9292
{
93-
while (true) {
93+
do {
9494

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

9999
$contacts = $response->getBody();
100100

101+
$this->known_contact_ids->push(collect($contacts)
102+
->pluck('contact_id')->flatten()->all());
103+
104+
// This wont save network calls, but should save DB writes
105+
if ($this->shouldUseCache($response) &&
106+
AllianceContact::where('alliance_id', $this->getAllianceId())->exists())
107+
continue; // This page has no changes so move onto the next page
108+
101109
collect($contacts)->each(function ($contact) {
102110

103111
AllianceContact::firstOrNew([
@@ -110,12 +118,7 @@ public function handle()
110118
])->save();
111119
});
112120

113-
$this->known_contact_ids->push(collect($contacts)
114-
->pluck('contact_id')->flatten()->all());
115-
116-
if (! $this->nextPage($response->getPagesCount()))
117-
break;
118-
}
121+
} while ($this->nextPage($response->getPagesCount()));
119122

120123
// Cleanup old contacts
121124
AllianceContact::where('alliance_id', $this->getAllianceId())

src/Jobs/Contacts/Alliance/Labels.php

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function handle()
7070
'alliance_id' => $this->getAllianceId(),
7171
]);
7272

73+
if ($this->shouldUseCache($response) &&
74+
AllianceLabel::where('alliance_id', $this->getAllianceId())->exists())
75+
return;
76+
7377
$labels = $response->getBody();
7478

7579
collect($labels)->each(function ($label) {

0 commit comments

Comments
 (0)