Skip to content

Commit 39fd988

Browse files
committed
chore: refactor cache service to remove dupe method ( clear cache region )
fix: invalidate speaker cache on external member update fix: add headers to prevent cache
1 parent 19ffe50 commit 39fd988

8 files changed

Lines changed: 141 additions & 26 deletions

File tree

Libs/Utils/ICacheService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,10 @@ public function boot();
111111
* @return int
112112
*/
113113
public function ttl($key);
114-
}
114+
115+
/**
116+
* @param string $cache_region_key
117+
* @return void
118+
*/
119+
public function clearCacheRegion(string $cache_region_key):void;
120+
}

app/Jobs/CleanMemberCacheJob.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php namespace App\Jobs;
2+
/**
3+
* Copyright 2018 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use App\Services\Model\IMemberService;
15+
use Illuminate\Bus\Queueable;
16+
use Illuminate\Contracts\Queue\ShouldQueue;
17+
use Illuminate\Foundation\Bus\Dispatchable;
18+
use Illuminate\Queue\InteractsWithQueue;
19+
use Illuminate\Queue\SerializesModels;
20+
use Illuminate\Support\Facades\Log;
21+
22+
class CleanMemberCacheJob implements ShouldQueue
23+
{
24+
public $tries = 2;
25+
26+
// no timeout
27+
public $timeout = 0;
28+
29+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
30+
31+
/**
32+
* @var int
33+
*/
34+
private $member_id;
35+
36+
/**
37+
* NewMember constructor.
38+
* @param int $member_id
39+
*/
40+
public function __construct(int $member_id)
41+
{
42+
$this->member_id = $member_id;
43+
}
44+
45+
/**
46+
* @param IMemberService $service
47+
*/
48+
public function handle(IMemberService $service)
49+
{
50+
Log::debug(sprintf("CleanMemberCacheJob::handle member_id %s", $this->member_id));
51+
$service->cleanMemberRelatedCacheData($this->member_id);
52+
}
53+
}

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use App\Events\ScheduleEntityLifeCycleEvent;
2424
use App\Events\SummitAttendeeCheckInStateUpdated;
2525
use App\Events\TicketUpdated;
26+
use App\Jobs\CleanMemberCacheJob;
2627
use App\Jobs\Emails\BookableRooms\BookableRoomReservationCanceledEmail;
2728
use App\Jobs\Emails\BookableRooms\BookableRoomReservationCreatedEmail;
2829
use App\Jobs\Emails\BookableRooms\BookableRoomReservationPaymentConfirmedEmail;
@@ -175,6 +176,10 @@ public function boot()
175176
logContext: ['member_id' => $event->getMemberId()]
176177
);
177178

179+
JobDispatcher::withDbFallback(
180+
job: new CleanMemberCacheJob($event->getMemberId()),
181+
logContext: ['member_id' => $event->getMemberId()]
182+
);
178183

179184
});
180185

app/Services/Apis/ExternalUserApi.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public function getUserByEmail(string $email)
6969
Log::debug(sprintf("ExternalUserApi::getUserByEmail email %s", $email));
7070

7171
return $this->invokeWithRetry(function () use ($email) {
72-
7372
$query = [
74-
'access_token' => $this->getAccessToken()
73+
'access_token' => $this->getAccessToken(),
7574
];
7675

7776
$params = [
@@ -83,6 +82,11 @@ public function getUserByEmail(string $email)
8382
}
8483

8584
$response = $this->client->get('/api/v1/users', [
85+
'headers' => [
86+
'Accept' => 'application/json',
87+
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
88+
'Pragma' => 'no-cache',
89+
],
8690
'query' => $query,
8791
]
8892
);
@@ -162,12 +166,16 @@ public function getUserById(int $id)
162166
Log::debug(sprintf("ExternalUserApi::getUserById id %s", $id));
163167

164168
return $this->invokeWithRetry(function () use ($id) {
165-
166169
$query = [
167-
'access_token' => $this->getAccessToken()
170+
'access_token' => $this->getAccessToken(),
168171
];
169172

170173
$response = $this->client->get(sprintf('/api/v1/users/%s', $id), [
174+
'headers' => [
175+
'Accept' => 'application/json',
176+
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
177+
'Pragma' => 'no-cache',
178+
],
171179
'query' => $query,
172180
]
173181
);
@@ -237,7 +245,6 @@ public function getUserRegistrationRequest(string $email)
237245
Log::debug(sprintf("ExternalUserApi::getUserRegistrationRequest email %s", $email));
238246

239247
return $this->invokeWithRetry(function () use ($email) {
240-
241248
$query = [
242249
'access_token' => $this->getAccessToken(),
243250
];
@@ -251,6 +258,11 @@ public function getUserRegistrationRequest(string $email)
251258
}
252259

253260
$response = $this->client->get('/api/v1/user-registration-requests', [
261+
'headers' => [
262+
'Accept' => 'application/json',
263+
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
264+
'Pragma' => 'no-cache',
265+
],
254266
'query' => $query,
255267
]);
256268

app/Services/Model/IMemberService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,10 @@ public function registerExternalUserByPayload(array $user_data):Member;
169169
* @throws \Exception
170170
*/
171171
public function signIndividualMembership(Member $member): Member;
172-
}
172+
173+
/**
174+
* @param int $member_id
175+
* @return void
176+
*/
177+
public function cleanMemberRelatedCacheData(int $member_id):void;
178+
}

app/Services/Model/Imp/MemberService.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use App\Services\Apis\IExternalUserApi;
2222
use App\Services\Model\dto\ExternalUserDTO;
2323
use DateTime;
24+
use Illuminate\Support\Facades\Cache;
2425
use Illuminate\Support\Facades\Event;
2526
use Illuminate\Support\Facades\Log;
27+
use libs\utils\CacheRegions;
2628
use libs\utils\ICacheService;
2729
use libs\utils\ITransactionService;
2830
use libs\utils\TextUtils;
@@ -785,4 +787,24 @@ public function signIndividualMembership(Member $member): Member
785787
});
786788
}
787789

788-
}
790+
/**
791+
* @param int $member_id
792+
* @return void
793+
* @throws \Exception
794+
*/
795+
public function cleanMemberRelatedCacheData(int $member_id): void
796+
{
797+
Log::debug(sprintf("MemberService::cleanMemberRelatedCacheData member id %s.", $member_id));
798+
799+
$this->tx_service->transaction(function() use($member_id) {
800+
$member = $this->member_repository->getById($member_id);
801+
if(!$member instanceof Member)
802+
throw new EntityNotFoundException("Member not found.");
803+
if(!$member->hasSpeaker()) return;
804+
$cache_region_key = CacheRegions::getCacheRegionFor(CacheRegions::CacheRegionSpeakers, $member->getSpeakerId());
805+
if (!empty($cache_region_key)) {
806+
$this->cache_service->clearCacheRegion($cache_region_key);
807+
}
808+
});
809+
}
810+
}

app/Services/Model/Imp/ProcessScheduleEntityLifeCycleEventService.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use App\Services\Model\AbstractService;
1717
use App\Services\Model\IProcessScheduleEntityLifeCycleEventService;
1818
use App\Services\Utils\RabbitPublisherService;
19-
use Illuminate\Support\Facades\Cache;
2019
use Illuminate\Support\Facades\Config;
2120
use Illuminate\Support\Facades\Log;
2221
use LaravelDoctrine\ORM\Facades\EntityManager;
@@ -144,21 +143,7 @@ public function process(string $entity_operator, int $summit_id, int $entity_id,
144143
}
145144

146145
if (!empty($cache_region_key)) {
147-
Cache::tags($cache_region_key)->flush();
148-
if($this->cache_service->exists($cache_region_key)){
149-
Log::debug(sprintf("ProcessScheduleEntityLifeCycleEventService::process will clear cache region %s", $cache_region_key));
150-
$region_data = $this->cache_service->getSingleValue($cache_region_key);
151-
if (!empty($region_data)) {
152-
$region = json_decode(gzinflate($region_data), true);
153-
Log::debug(sprintf("ProcessScheduleEntityLifeCycleEventService::process got payload %s region %s", json_encode($region), $cache_region_key));
154-
foreach ($region as $key => $val) {
155-
Log::debug(sprintf("ProcessScheduleEntityLifeCycleEventService::process clearing cache key %s", $key));
156-
$this->cache_service->delete($key);
157-
$this->cache_service->delete($key . 'generated');
158-
}
159-
$this->cache_service->delete($cache_region_key);
160-
}
161-
}
146+
$this->cache_service->clearCacheRegion($cache_region_key);
162147
}
163148

164149
if (is_null($this->rabbit_service)) {
@@ -309,4 +294,4 @@ public function process(string $entity_operator, int $summit_id, int $entity_id,
309294

310295
});
311296
}
312-
}
297+
}

app/Services/Utils/RedisCacheService.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
**/
1414

15+
use Illuminate\Support\Facades\Cache;
1516
use Illuminate\Support\Facades\Log;
1617
use libs\utils\ICacheService;
1718
use Predis\Connection\ConnectionException as PredisConnectionException;
@@ -324,4 +325,29 @@ public function ttl($key)
324325
return (int)$conn->ttl($key);
325326
}, 0);
326327
}
327-
}
328+
329+
/**
330+
* @param string $cache_region_key
331+
* @return void
332+
*/
333+
public function clearCacheRegion(string $cache_region_key): void
334+
{
335+
if (!empty($cache_region_key)) {
336+
Cache::tags($cache_region_key)->flush();
337+
if($this->exists($cache_region_key)){
338+
Log::debug(sprintf("RedisCacheService::clearCacheRegion will clear cache region %s", $cache_region_key));
339+
$region_data = $this->getSingleValue($cache_region_key);
340+
if (!empty($region_data)) {
341+
$region = json_decode(gzinflate($region_data), true);
342+
Log::debug(sprintf("RedisCacheService::clearCacheRegion got payload %s region %s", json_encode($region), $cache_region_key));
343+
foreach ($region as $key => $val) {
344+
Log::debug(sprintf("RedisCacheService::clearCacheRegion clearing cache key %s", $key));
345+
$this->delete($key);
346+
$this->delete($key . 'generated');
347+
}
348+
$this->delete($cache_region_key);
349+
}
350+
}
351+
}
352+
}
353+
}

0 commit comments

Comments
 (0)