https://github.com/juampi92/laravel-query-cache
This simple package allows you to cache your Database or Eloquent queries with no extra config.
https://github.com/juampi92/laravel-query-cache
cache eloquent laravel
Last synced: 11 days ago
JSON representation
This simple package allows you to cache your Database or Eloquent queries with no extra config.
- Host: GitHub
- URL: https://github.com/juampi92/laravel-query-cache
- Owner: juampi92
- License: mit
- Created: 2021-03-03T14:31:38.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-12T16:42:22.000Z (about 2 years ago)
- Last Synced: 2025-04-09T12:04:21.213Z (13 days ago)
- Topics: cache, eloquent, laravel
- Language: PHP
- Homepage:
- Size: 42 KB
- Stars: 25
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Laravel Query Cache
[](https://packagist.org/packages/juampi92/laravel-query-cache)
[](https://github.com/juampi92/laravel-query-cache/actions?query=workflow%3ATests+branch%3Amaster)
[](https://github.com/juampi92/laravel-query-cache/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster)
[](https://packagist.org/packages/juampi92/laravel-query-cache)This package provides a set of macros to cache your Laravel Queries just like Cache::remember.
```php
$featuredPost = Post::published()->orderByMostViews()
->cacheDay('post:featured') // <- Here
->first();
```## Installation
You can install the package via composer:
```bash
composer require juampi92/laravel-query-cache
```That's it! No config or Trait necessary. The package auto-discovery will boot the macros.
## Usage
Instead of doing:
```php
Cache::remember('post:count', $ttl, () => Post::published()->count());
```You now do:
```php
Post::published()->cache('post:count', $ttl)->count();
```You can use it in Eloquent Queries as well as in normal Queries.
```php
DB::table('posts')
->whereNotNull('published_at')
->latest()
->cacheHour('post:latest')
->first();Posts::published()
->cacheHour('post:count')
->count();
```List of macros:
```php
Post::cache('cache:key', $ttl)->get();
Post::cacheMinute('cache:key')->first();
Post::cacheHour('cache:key')->pluck('id');
Post::cacheDay("cache:key:$id")->find($id);
Post::cacheWeek('cache:key:paginate:10')->paginate(10);
Post::cacheForever('cache:key')->count();
```## Advanced usage
### Different store
```php
Post::query()
->where(...)
->cache('post:count')->store('redis')
->count();
```### Add your custom cache duration
This is maybe more advanced, but you can do so by [opting out of discovery](https://laravel.com/docs/8.x/packages#opting-out-of-package-discovery), and then importing it yourself:
```php
use Juampi92\LaravelQueryCache\LaravelQueryCacheServiceProvider as BaseServiceProvider;class LaravelQueryCacheServiceProvider extends BaseServiceProvider
{
protected function getCustomTimes(): array
{
return array_merge(
parent::getCustomTimes(),
[
'rememberForever' => null,
'cacheFifteenMinutes' => 60 * 15,
]
);
}
}
```The original method has
```php
[
'cacheForever' => null,
'cacheMinute' => 60,
'cacheHour' => 60 * 60,
'cacheDay' => 60 * 60 * 24,
'cacheWeek' => 60 * 60 * 24 * 7,
]
```## Disclaimer
This package is supposed to be a nice integration of Cache remember inside the Query Builder.
If you're looking for a advanced eloquent specific cache, I recommend to check out [laravel-eloquent-query-cache](https://github.com/renoki-co/laravel-eloquent-query-cache).## Testing
```bash
composer test
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Juampi92](https://github.com/juampi92)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.