From 92f964f572dee2969ad3371c847ce844620b3288 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 26 Apr 2024 17:27:00 +1000 Subject: [PATCH] Remove static cache from our_address in favour of Cache::class keyed off setup:system_id --- app/helpers.php | 16 ++++++++-------- tests/Feature/RoutingTest.php | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index ac24063..344a136 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -2,6 +2,7 @@ use Carbon\Carbon; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Cache; use App\Models\{Address,Domain,Setup}; @@ -92,16 +93,15 @@ if (! function_exists('hexstr')) { */ function our_address(Domain|Address $o=NULL): Collection|Address|NULL { - static $so = NULL; - static $our = NULL; + $so = Cache::remember('so',5,function() { + return Setup::findOrFail(config('app.id')); + }); - if (! $so) - $so = Setup::findOrFail(config('app.id')); - - if (! $our) { + $our = Cache::remember(sprintf('%d-akas',$so->system_id),60,function() use ($so) { $so->load(['system.akas.zone.domain']); - $our = $so->system->akas; - } + + return $so->system->akas; + }); // If we dont have any addresses if ($our->count() === 0) diff --git a/tests/Feature/RoutingTest.php b/tests/Feature/RoutingTest.php index fdb5af3..f8aa67e 100644 --- a/tests/Feature/RoutingTest.php +++ b/tests/Feature/RoutingTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature; use Illuminate\Database\Eloquent\Collection; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Support\Facades\Cache; use Tests\TestCase; use App\Models\{Address,Domain,Setup,System}; @@ -268,10 +269,12 @@ class RoutingTest extends TestCase public function test_complex_rc_nc_hc_us() { + Cache::forget('so'); $setup = Setup::findOrFail(config('app.id')); $ao = Address::findFTN('100:10/0.0@a'); $setup->system_id = $ao->system_id; $setup->save(); + $this->assertEquals('100:10/0.0@a',our_address($ao)?->ftn); $this->session_rc(); //$this->session_nc(); @@ -303,6 +306,7 @@ class RoutingTest extends TestCase $ao = Address::findFTN('100:10/22.0@a'); $this->assertEquals('100:10/20.0@a',$ao->parent()->ftn); + Cache::forget('so'); } // A points parent is the node, if we have traffic for a point and we have session details for the node