Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Stores/LaravelCacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public function get(string $key): ?string
*/
public function set(string $key, string $value, int $ttl): bool
{
return $this->store->put($key, $value, $ttl);
$result = $this->store->put($key, $value, $ttl);

// If the data already exists in the cache, then mysql returns 0 rows affected. Technically, this is not a
// failure. We should check for this before returning anything and return true if it does exist.
if ($result === false) {
$existingValue = $this->store->get($key);
if ($existingValue === $value) {
return true;
}
}

return $result;
}
}
15 changes: 15 additions & 0 deletions tests/Laravel/Feature/LaravelStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Saloon\RateLimitPlugin\Limit;
use Illuminate\Support\Facades\Cache;
use Illuminate\Contracts\Cache\Repository;
use Saloon\RateLimitPlugin\Stores\LaravelCacheStore;

test('it records and can check exceeded limits', function () {
Expand Down Expand Up @@ -37,3 +38,17 @@
'hits' => 1,
]));
});

test('it handles MySQL upsert behavior returning false for identical data', function () {
$mockCache = Mockery::mock(Repository::class);

$key = 'test:limit';
$value = json_encode(['timestamp' => time() + 60, 'hits' => 1]);

$mockCache->shouldReceive('put')->with($key, $value, Mockery::any())->andReturn(false);
$mockCache->shouldReceive('get')->with($key)->andReturn($value);

$store = new LaravelCacheStore($mockCache);

expect($store->set($key, $value, 60))->toBeTrue();
});