Skip to content

Commit 54ee632

Browse files
6.x
1 parent d94ca2c commit 54ee632

7 files changed

Lines changed: 32 additions & 30 deletions

File tree

.github/workflows/php.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,15 @@ jobs:
5050
strategy:
5151
matrix:
5252
php-version:
53-
- 8.2
5453
- 8.3
5554
- 8.4
55+
- 8.5
5656
laravel-constraint:
57-
- 11.*
5857
- 12.*
58+
- 13.*
5959
dependencies:
6060
- lowest
6161
- highest
62-
exclude:
63-
- laravel-constraint: 12.*
64-
php-version: 8.2
6562
steps:
6663
- name: Set up PHP
6764
uses: shivammathur/setup-php@v2

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Cache Query
22
[![Latest Version on Packagist](https://img.shields.io/packagist/v/laragear/cache-query.svg)](https://packagist.org/packages/laragear/cache-query)
33
[![Latest stable test run](https://github.com/Laragear/CacheQuery/workflows/Tests/badge.svg)](https://github.com/Laragear/CacheQuery/actions)
4-
[![Codecov coverage](https://codecov.io/gh/Laragear/CacheQuery/branch/5.x/graph/badge.svg?token=IOZS1TFJ5G)](https://codecov.io/gh/Laragear/CacheQuery)
4+
[![Codecov coverage](https://codecov.io/gh/Laragear/CacheQuery/graph/badge.svg?token=IOZS1TFJ5G)](https://codecov.io/gh/Laragear/CacheQuery)
55
[![Maintainability](https://qlty.sh/badges/8738ff13-01ca-4d38-83a8-dd484723093d/maintainability.svg)](https://qlty.sh/gh/Laragear/projects/CacheQuery)
66
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Laragear_CacheQuery&metric=alert_status)](https://sonarcloud.io/dashboard?id=Laragear_CacheQuery)
7-
[![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/11.x/octane#introduction)
7+
[![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/13.x/octane#introduction)
88

99
Remember your query results using only one method. Yes, only one.
1010

@@ -20,7 +20,8 @@ Your support allows me to keep this package free, up-to-date and maintainable. A
2020

2121
## Requirements
2222

23-
* Laravel 11 or later
23+
* PHP 8.3
24+
* Laravel 12 or later
2425

2526
## Installation
2627

@@ -30,15 +31,15 @@ You can install the package via composer:
3031
composer require laragear/cache-query
3132
```
3233

33-
## How it works?
34+
## How does it work?
3435

35-
This library wraps the connection into a proxy object. It proxies all method calls to it except `select()` and `selectOne()`.
36+
This library wraps the database connection into a proxy object. It proxies all method calls to it except `select()` and `selectOne()`.
3637

3738
Once a `SELECT` statement is executed through the aforementioned methods, it will check if the results are in the cache before executing the query. On cache hit, it will return the cached results, otherwise it will continue execution, save the results using the cache configuration, and return them.
3839

3940
## Usage
4041

41-
Just use the `cache()` method to remember the results of a query for a default of 60 seconds.
42+
Use the `cache()` method to remember the results of a query for a default of 60 seconds.
4243

4344
```php
4445
use Illuminate\Support\Facades\DB;
@@ -51,7 +52,7 @@ Article::latest('published_at')->take(10)->cache()->get();
5152

5253
The next time you call the **same** query, the result will be retrieved from the cache instead of running the `SELECT` SQL statement in the database, even if the results are empty, `null` or `false`. You may also desire to [not cache empty results](#cache-except-empty-results).
5354

54-
It's **eager load aware**. This means that it will cache an eager loaded relation automatically, but [you may also disable this](#eager-loaded-queries).
55+
It's **eager load aware**. This means that it will cache an eager loaded relation automatically (but [you may also disable this](#eager-loaded-queries)).
5556

5657
```php
5758
use App\Models\User;

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
"issues": "https://github.com/laragear/cache-query/issues"
1818
},
1919
"require": {
20-
"php": "^8.2",
21-
"illuminate/cache": "11.*|12.*",
22-
"illuminate/config": "11.*|12.*",
23-
"illuminate/database": "11.*|12.*",
24-
"illuminate/support": "11.*|12.*",
25-
"illuminate/container": "11.*|12.*",
26-
"illuminate/contracts": "11.*|12.*"
20+
"php": "^8.3",
21+
"illuminate/cache": "12.*|13.*",
22+
"illuminate/config": "12.*|13.*",
23+
"illuminate/database": "12.*|13.*",
24+
"illuminate/support": "12.*|13.*",
25+
"illuminate/container": "12.*|13.*",
26+
"illuminate/contracts": "12.*|13.*"
2727
},
2828
"require-dev": {
29-
"orchestra/testbench": "9.*|10.*"
29+
"orchestra/testbench": "10.*|11.*"
3030
},
3131
"autoload": {
3232
"psr-4": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
33
<testsuites>
44
<testsuite name="Test Suite">
55
<directory>tests</directory>

src/CacheQueryServiceProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class CacheQueryServiceProvider extends ServiceProvider
2626
{
27-
public const CONFIG = __DIR__.'/../config/cache-query.php';
28-
public const STUBS = __DIR__.'/../.stubs/stubs';
27+
public const string CONFIG = __DIR__.'/../config/cache-query.php';
28+
public const string STUBS = __DIR__.'/../.stubs/stubs';
2929

3030
/**
3131
* Register the service provider.
@@ -95,7 +95,6 @@ protected function macro(): Closure
9595
default => $cache = $ttl
9696
};
9797

98-
// Normalize the TTL argument to a Cache instance.
9998
$this->connection = Proxy::crateNewInstance($this->connection, $cache);
10099

101100
return $this;

src/Proxy.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Str;
1212

1313
use function app;
14+
use function array_filter;
1415
use function array_shift;
1516
use function is_array;
1617
use function max;
@@ -66,7 +67,7 @@ public function getCacheHelperInstance(): Cache
6667
*
6768
* @throws \Psr\SimpleCache\InvalidArgumentException
6869
*/
69-
public function select($query, $bindings = [], $useReadPdo = true): mixed
70+
public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
7071
{
7172
// Create the unique hash for the query to avoid any duplicate query.
7273
$this->computedKey = (static::$queryHasher)($this->connection, $query, $bindings);
@@ -159,7 +160,11 @@ protected function retrieveResultsFromCache(string $key): array
159160
return [$key => null, $this->cache->key => null];
160161
}
161162

162-
return $this->repository->getMultiple([$key, $this->cache->key]);
163+
$result = $this->repository->getMultiple(array_filter([$key, $this->cache->key]));
164+
165+
$result[$this->cache->key] ??= null;
166+
167+
return $result;
163168
}
164169

165170
/**

tests/ProxyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public function test_uses_custom_time_to_live(): void
370370
$interval = $now->diffAsCarbonInterval(now());
371371

372372
$repository = $this->mock(Repository::class);
373-
$repository->expects('getMultiple')->with([$hash, ''])->times(4)->andReturn(['' => null, $hash => null]);
373+
$repository->expects('getMultiple')->with([$hash])->times(4)->andReturn(['' => null, $hash => null]);
374374
$repository->allows('getStore')->never();
375375
$repository->expects('put')->with($hash, Mockery::type('array'), null);
376376
$repository->expects('put')->with($hash, Mockery::type('array'), $seconds);
@@ -560,7 +560,7 @@ public function test_uses_custom_store(): void
560560
$repository = $this->mock(Repository::class);
561561
$repository->expects('flexible')->never();
562562
$repository->expects('put')->with($hash, Mockery::type('array'), 60)->once();
563-
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
563+
$repository->expects('getMultiple')->with([$hash])->times(1)->andReturn(['' => null, $hash => null]);
564564

565565
$this->mock('cache')->expects('store')->with('test-store')->andReturn($repository);
566566

@@ -630,7 +630,7 @@ public function test_doesnt_uses_flexible_caching_if_repository_is_not_flexible(
630630
$repository = $this->mock(Repository::class);
631631
$repository->expects('flexible')->never();
632632
$repository->expects('put')->with($hash, Mockery::type('array'), [5, 300])->once();
633-
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
633+
$repository->expects('getMultiple')->with([$hash])->times(1)->andReturn(['' => null, $hash => null]);
634634

635635
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
636636

@@ -921,7 +921,7 @@ public function test_base_query_uses_cache_callback(): void
921921
$repository = $this->mock(Repository::class);
922922
$repository->expects('flexible')->never();
923923
$repository->expects('put')->with($hash, Mockery::type('array'), [5, 300])->once();
924-
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
924+
$repository->expects('getMultiple')->with([$hash])->times(1)->andReturn(['' => null, $hash => null]);
925925

926926
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
927927

0 commit comments

Comments
 (0)