PHPackages                             tarkhov/laravel-db-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. tarkhov/laravel-db-cache

ActiveLibrary[Database &amp; ORM](/categories/database)

tarkhov/laravel-db-cache
========================

Laravel database query caching.

v1.0.0(3mo ago)40MITPHPPHP &gt;=8.2

Since Mar 12Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/tarkhov/laravel-db-cache)[ Packagist](https://packagist.org/packages/tarkhov/laravel-db-cache)[ Docs](https://github.com/tarkhov/laravel-db-cache)[ RSS](/packages/tarkhov-laravel-db-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Laravel DB Cache
================

[](#laravel-db-cache)

Laravel database query caching.

### Contents

[](#contents)

1. [Compatibility](#compatibility)
2. [Installation](#installation)
    1. [Composer](#composer)
3. [Usage](#usage)
    1. [How it work](#how-it-work)
    2. [Using DB facade query](#using-db-facade-query)
    3. [Using Eloquent ORM query](#using-eloquent-orm-query)
4. [Author](#author)
5. [License](#license)

Compatibility
-------------

[](#compatibility)

LibraryVersionLaravel&gt;= 12.0Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require tarkhov/laravel-db-cache
```

Usage
-----

[](#usage)

### How it work

[](#how-it-work)

1. Import **QueryCache** class: `use LaravelDBCache\QueryCache`.
2. Creating a query builder without retrieving data: `$builder = DB::table('users')->select(['id', 'name'])->where('id', $id);`.
3. 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']);`.
4. 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 like `first()`, `firstOrFail()`, `get()` and others, because it's a native non modified Laravel query builder.

### Using DB facade query

[](#using-db-facade-query)

Retrieve one row.

```
