tarkhov / laravel-db-cache
Laravel database query caching.
v1.0.0
2026-02-01 16:55 UTC
Requires
- php: >=8.2
- illuminate/cache: ^12.0
- illuminate/database: ^12.0
- illuminate/support: ^12.0
README
Laravel database query caching.
Contents
Compatibility
| Library | Version |
|---|---|
| Laravel | >= 12.0 |
Installation
Composer
composer require tarkhov/laravel-db-cache
Usage
How it work
- Import QueryCache class:
use LaravelDBCache\QueryCache. - Creating a query builder without retrieving data:
$builder = DB::table('users')->select(['id', 'name'])->where('id', $id);. - Get new QueryCache instance by passing query builder as argument in constructor:
$queryCache = new QueryCache($builder);. Since each sql query is unique, the constructor will generate a caching key as a hash from the sql query, which guarantees the absence of collisions and duplicates. Optionally, you can pass the amount of cache time in minutes as the 2nd argument and cache tags as the 3rd argument if you plan to use them instead of the cache key:$queryCache = new QueryCache($builder, 60, ['my_tag']);. - Get cached data from storage or automatically saving the result in the cache if the given query has not yet been added to the cache storage using one method for single row query or many for multiple rows query with callback as argument, this callback has one argument - it's your query builder without any modifications:
$result = $queryCache->one(fn($builder) => $builder->first());or$result = $queryCache->many(fn($builder) => $builder->get());. You can use any method for data retrieving likefirst(),firstOrFail(),get()and others, because it's a native non modified Laravel query builder.
Using DB facade query
Retrieve one row.
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use LaravelDBCache\QueryCache; class UserController extends Controller { public function oneWithDB(string $id): object { // create builder query $builder = DB::table('users')->select(['id', 'name'])->where('id', $id); // retrieve cached result $user = (new QueryCache($builder))->one(fn($builder) => $builder->first()); return $user; } }
Retrieve many rows.
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use LaravelDBCache\QueryCache; class UserController extends Controller { public function manyWithDB(): array { $builder = DB::table('users')->select(['id', 'name'])->limit(15); $user = (new QueryCache($builder))->many(fn($builder) => $builder->get()); return $user; } }
Using Eloquent ORM query
Retrieve one row.
<?php namespace App\Http\Controllers; use LaravelDBCache\QueryCache; use App\Models\User; class UserController extends Controller { public function oneWithORM(string $id): User { $builder = User::select(['id', 'name'])->where('id', $id); $user = (new QueryCache($builder))->one(fn($builder) => $builder->first()); return $user; } }
Retrieve many rows.
<?php namespace App\Http\Controllers; use Illuminate\Database\Eloquent\Collection; use LaravelDBCache\QueryCache; use App\Models\User; class UserController extends Controller { public function manyWithORM(): Collection { $builder = User::select(['id', 'name'])->limit(5); $user = (new QueryCache($builder))->many(fn($builder) => $builder->get()); return $user; } }
Author
License
This project is licensed under the MIT License - see the LICENSE file for details.