Skip to content

Commit 1f637d2

Browse files
committed
feat: Implement per-model data refresh option for Sushi in Laravel Octane
1 parent 0b99190 commit 1f637d2

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,24 +289,35 @@ class Role extends Model
289289

290290
## Configuration
291291

292-
Sushi supports several configuration options that can be set in your Laravel config files (typically in `config/sushi.php`):
292+
Sushi supports several configuration options:
293293

294294
### Refresh on Request
295295

296-
When using Laravel Octane (or other long-running server implementations), Sushi boots and migrates the data once and reuses it across requests by default for optimal performance. If your data can change between requests and you need Sushi to re-run the boot and migration process to fetch fresh data for each request, you can enable this option:
296+
When using Laravel Octane (or other long-running server implementations), Sushi boots and migrates the data once and reuses it across requests by default for optimal performance. If your data can change between requests and you need Sushi to re-run the boot and migration process to fetch fresh data for each request, you can enable this option per model by overriding the `shouldRefreshDataOnEachRequest` method:
297297

298298
```php
299-
// config/sushi.php
300-
return [
301-
'refresh-on-request' => true, // Default: false
302-
];
299+
class Role extends Model
300+
{
301+
use \Sushi\Sushi;
302+
303+
public function getRows()
304+
{
305+
// Fetch fresh data from external source
306+
return api()->getRoles();
307+
}
308+
309+
protected function shouldRefreshDataOnEachRequest()
310+
{
311+
return true; // Default: false
312+
}
313+
}
303314
```
304315

305316
> Note: This option is primarily useful when running Laravel Octane and your data source (e.g., `getRows()` implementation) changes between requests and you need fresh data on each request.
306317
307318
### Cache Configuration
308319

309-
You can customize where Sushi stores its cache files and the cache file prefix:
320+
You can customize where Sushi stores its cache files and the cache file prefix in your config file (typically `config/sushi.php`):
310321

311322
```php
312323
// config/sushi.php

src/Sushi.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ protected function sushiShouldCache()
3434
return property_exists(static::class, 'rows');
3535
}
3636

37+
protected function shouldRefreshDataOnEachRequest()
38+
{
39+
return false;
40+
}
41+
3742
public static function resolveConnection($connection = null)
3843
{
39-
$shouldCheckRequestId = config('sushi.refresh-on-request', false);
44+
$instance = new static;
45+
$shouldCheckRequestId = $instance->shouldRefreshDataOnEachRequest();
4046

4147
if ($shouldCheckRequestId) {
4248
$requestId = static::getCurrentRequestId();

0 commit comments

Comments
 (0)