File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,17 @@ public function get(string $key): ?string
3232 */
3333 public function set (string $ key , string $ value , int $ ttl ): bool
3434 {
35- return $ this ->store ->put ($ key , $ value , $ ttl );
35+ $ result = $ this ->store ->put ($ key , $ value , $ ttl );
36+
37+ // If the data already exists in the cache, then mysql returns 0 rows affected. Technically, this is not a
38+ // failure. We should check for this before returning anything and return true if it does exist.
39+ if ($ result === false ) {
40+ $ existingValue = $ this ->store ->get ($ key );
41+ if ($ existingValue === $ value ) {
42+ return true ;
43+ }
44+ }
45+
46+ return $ result ;
3647 }
3748}
Original file line number Diff line number Diff line change 44
55use Saloon \RateLimitPlugin \Limit ;
66use Illuminate \Support \Facades \Cache ;
7+ use Illuminate \Contracts \Cache \Repository ;
78use Saloon \RateLimitPlugin \Stores \LaravelCacheStore ;
89
910test ('it records and can check exceeded limits ' , function () {
3738 'hits ' => 1 ,
3839 ]));
3940});
41+
42+ test ('it handles MySQL upsert behavior returning false for identical data ' , function () {
43+ $ mockCache = Mockery::mock (Repository::class);
44+
45+ $ key = 'test:limit ' ;
46+ $ value = json_encode (['timestamp ' => time () + 60 , 'hits ' => 1 ]);
47+
48+ $ mockCache ->shouldReceive ('put ' )->with ($ key , $ value , Mockery::any ())->andReturn (false );
49+ $ mockCache ->shouldReceive ('get ' )->with ($ key )->andReturn ($ value );
50+
51+ $ store = new LaravelCacheStore ($ mockCache );
52+
53+ expect ($ store ->set ($ key , $ value , 60 ))->toBeTrue ();
54+ });
You can’t perform that action at this time.
0 commit comments