Skip to content

Commit baf1178

Browse files
authored
Fix: Ignore cache when sticky configuration is false (#17)
* Fix: Ignore cache when sticky configuration is false * Test: Add unit tests * Test: Add feature test * Doc: Update config suggestion in README
1 parent ba9749f commit baf1178

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ return [
112112
'host' => env('DB_HOST_READONLY'),
113113
] : null,
114114
'write' => [],
115-
'sticky' => true,
115+
'sticky' => (bool)env('DB_HOST_READONLY'),
116116
'stickiness_ttl' => 3, // Set the stickiness TTL to 3 seconds
117117
'driver' => 'mysql',
118118
'host' => env('DB_HOST', '127.0.0.1'),

src/StickinessEventListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function onConnectionCreated(ConnectionCreated $event): void
9191
$this->currentJobInitialization->initializeOnNewConnection($this->currentJobProcessingEvent, $event);
9292
}
9393

94-
$this->stickiness->resolveRecordsModified($event->connection);
94+
if ($event->connection->getConfig('sticky')) {
95+
$this->stickiness->resolveRecordsModified($event->connection);
96+
}
9597
}
9698

9799
/**
@@ -105,6 +107,8 @@ public function onRecordsHaveBeenModified(RecordsHaveBeenModified $event): void
105107
$this->currentJobInitialization->dontRevokeEffectsOn($event->connection);
106108
}
107109

108-
$this->stickiness->markAsModified($event->connection);
110+
if ($event->connection->getConfig('sticky')) {
111+
$this->stickiness->markAsModified($event->connection);
112+
}
109113
}
110114
}

tests/Feature/CachingTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Closure;
77
use Illuminate\Http\Request;
88
use Illuminate\Support\Facades\Cache;
9+
use Illuminate\Support\Facades\Config;
910
use Illuminate\Support\Facades\DB;
1011
use Mpyw\LaravelCachedDatabaseStickiness\ConnectionServiceProvider;
1112
use Mpyw\LaravelCachedDatabaseStickiness\StickinessServiceProvider;
@@ -105,4 +106,24 @@ public function testAffectingStatement(): void
105106

106107
$this->assertNull(Cache::get('database-stickiness:connection=test,resolver=ip,ip=192.168.0.1'));
107108
}
109+
110+
public function testAffectingStatementWhenStickyDisabled(): void
111+
{
112+
// Stickiness caching should be disabled when sticky configuration is false
113+
Config::set('database.connections.test.sticky', false);
114+
115+
Carbon::setTestNow('2020-01-01 00:00:00');
116+
117+
$this->assertInstanceOf(Closure::class, $this->getReadPdoViaReflection());
118+
$this->assertInstanceOf(Closure::class, $this->getWritePdoViaReflection());
119+
120+
/* @var \Illuminate\Database\Connection $connection */
121+
$connection = DB::connection();
122+
$connection->statement('select 1'); // This is a fake of insert/update/delete
123+
124+
$this->assertInstanceOf(Closure::class, $this->getReadPdoViaReflection());
125+
$this->assertInstanceOf(PDO::class, $this->getWritePdoViaReflection());
126+
127+
$this->assertNull(Cache::get('database-stickiness:connection=test,resolver=ip,ip=192.168.0.1'));
128+
}
108129
}

tests/Feature/ResolvingTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\Facades\Config;
89
use Illuminate\Support\Facades\DB;
910
use Mpyw\LaravelCachedDatabaseStickiness\ConnectionServiceProvider;
1011
use Mpyw\LaravelCachedDatabaseStickiness\StickinessServiceProvider;
@@ -89,6 +90,21 @@ public function testNonAffectingStatementWhenCacheExists(): void
8990
$this->assertInstanceOf(PDO::class, $this->getWritePdoViaReflection());
9091
}
9192

93+
public function testNonAffectingStatementWhenStickyDisabled(): void
94+
{
95+
// Cached stickiness should be ignored when sticky configuration is false
96+
Config::set('database.connections.test.sticky', false);
97+
98+
Cache::put('database-stickiness:connection=test,resolver=ip,ip=192.168.0.1', true, 5);
99+
100+
/* @var \Illuminate\Database\Connection $connection */
101+
$connection = DB::connection();
102+
$connection->select('select 1');
103+
104+
$this->assertInstanceOf(PDO::class, $this->getReadPdoViaReflection());
105+
$this->assertInstanceOf(Closure::class, $this->getWritePdoViaReflection());
106+
}
107+
92108
public function testAffectingStatementWhenCacheDoesNotExist(): void
93109
{
94110
/* @var \Illuminate\Database\Connection $connection */

tests/Unit/StickinessEventListenerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function onConnectionCreatedWithCurrentJob(): void
137137
$this->setCurrentJobInitialization($listener, $this->initialization);
138138

139139
$this->initialization->shouldReceive('initializeOnNewConnection')->once()->with($event)->andReturnSelf();
140+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnTrue();
140141
$this->stickiness->shouldReceive('resolveRecordsModified')->once();
141142

142143
$listener->onConnectionCreated(new ConnectionCreated($connection));
@@ -152,11 +153,28 @@ public function onConnectionCreatedWithoutCurrentJob(): void
152153
$this->setCurrentJobInitialization($listener, null);
153154

154155
$this->initialization->shouldNotReceive('initializeOnNewConnection');
156+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnTrue();
155157
$this->stickiness->shouldReceive('resolveRecordsModified')->once();
156158

157159
$listener->onConnectionCreated(new ConnectionCreated($connection));
158160
}
159161

162+
public function onConnectionCreatedWhenStickyDisabled(): void
163+
{
164+
$connection = Mockery::mock(Connection::class);
165+
166+
$listener = new StickinessEventListener($this->stickiness);
167+
168+
$this->setCurrentJobProcessingEvent($listener, null);
169+
$this->setCurrentJobInitialization($listener, null);
170+
171+
$this->initialization->shouldNotReceive('initializeOnNewConnection');
172+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnFalse();
173+
$this->stickiness->shouldNotReceive('resolveRecordsModified');
174+
175+
$listener->onConnectionCreated(new ConnectionCreated($connection));
176+
}
177+
160178
public function testOnRecordsHaveBeenModifiedWithCurrentJob(): void
161179
{
162180
$connection = Mockery::mock(ConnectionInterface::class);
@@ -167,6 +185,7 @@ public function testOnRecordsHaveBeenModifiedWithCurrentJob(): void
167185
$this->setCurrentJobInitialization($listener, $this->initialization);
168186

169187
$this->initialization->shouldReceive('dontRevokeEffectsOn')->once()->with($connection)->andReturnSelf();
188+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnTrue();
170189
$this->stickiness->shouldReceive('markAsModified')->once()->with($connection);
171190

172191
$listener->onRecordsHaveBeenModified(new RecordsHaveBeenModified($connection));
@@ -182,8 +201,25 @@ public function testOnRecordsHaveBeenModifiedWithoutCurrentJob(): void
182201
$this->setCurrentJobInitialization($listener, null);
183202

184203
$this->initialization->shouldNotReceive('dontRevokeEffectsOn');
204+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnTrue();
185205
$this->stickiness->shouldReceive('markAsModified')->once()->with($connection);
186206

187207
$listener->onRecordsHaveBeenModified(new RecordsHaveBeenModified($connection));
188208
}
209+
210+
public function testOnRecordsHaveBeenModifiedWhenStickyDisabled(): void
211+
{
212+
$connection = Mockery::mock(ConnectionInterface::class);
213+
214+
$listener = new StickinessEventListener($this->stickiness);
215+
216+
$this->setCurrentJobProcessingEvent($listener, null);
217+
$this->setCurrentJobInitialization($listener, null);
218+
219+
$this->initialization->shouldNotReceive('dontRevokeEffectsOn');
220+
$connection->shouldReceive('getConfig')->once()->with('sticky')->andReturnFalse();
221+
$this->stickiness->shouldNotReceive('markAsModified');
222+
223+
$listener->onRecordsHaveBeenModified(new RecordsHaveBeenModified($connection));
224+
}
189225
}

0 commit comments

Comments
 (0)