PHPackages                             stevenmaguire/laravel-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. stevenmaguire/laravel-cache

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

stevenmaguire/laravel-cache
===========================

Seamlessly adding caching to Laravel service objects

1.0.0(10y ago)171.8kMITPHPPHP &gt;=5.5.0

Since Apr 28Pushed 10y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (5)Versions (11)Used By (0)

Laravel Cache Services
======================

[](#laravel-cache-services)

[![Latest Version](https://camo.githubusercontent.com/24e45dd6b0a89fa2fa55e20d82dbd04139bff39df32bbe8cb8b95f321494d14b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73746576656e6d6167756972652f6c61726176656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://github.com/stevenmaguire/laravel-cache/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/2ca77a2f80206af5e9460f3da128f46e9f61cc3922ead6c401d5040347522509/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f73746576656e6d6167756972652f6c61726176656c2d63616368652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/stevenmaguire/laravel-cache)[![Coverage Status](https://camo.githubusercontent.com/d739bcac6deb26b7029f499cf1cefb48201de3200eccf19bfb985c47bbbd8573/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f73746576656e6d6167756972652f6c61726176656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/stevenmaguire/laravel-cache/code-structure)[![Quality Score](https://camo.githubusercontent.com/ced81f2513f2bf46813423691470a597b58dbc79368c1a246f5b9f87b037dc4b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f73746576656e6d6167756972652f6c61726176656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/stevenmaguire/laravel-cache)[![Total Downloads](https://camo.githubusercontent.com/168aa46fa562b41b6cdafd04f87d79591567f51bbb685e97afb9a862ee6e8cd2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746576656e6d6167756972652f6c61726176656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stevenmaguire/laravel-cache)

Seamlessly adding caching to Laravel Eloquent service objects.

Install
-------

[](#install)

Via Composer

```
$ composer require stevenmaguire/laravel-cache
```

Usage
-----

[](#usage)

### Include the base service

[](#include-the-base-service)

Extend your service class with the EloquentCache class.

```
class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache
{
    //
}
```

Implement the interface and use the trait.

```
class UserRegistrar implements Stevenmaguire\Laravel\Contracts\Cacheable
{
    use \Stevenmaguire\Laravel\Services\EloquentCacheTrait;
}
```

### Construct queries

[](#construct-queries)

Build queries using Eloquent and request cache object.

```
use App\User;
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getAllUsers()
    {
        $query = $this->user->query();

        return $this->cache('all', $query);
    }

    public function getUserById($id)
    {
        $query = $this->user->where('id', $id);

        return $this->cache('id('.$id.')', $query, 'first');
    }

    public function getRecent($skip = 0, $take = 100)
    {
        $query = $this->user->orderBy('created_at', 'desc')
            ->take($take)
            ->skip($skip);

        return $this->cache('recent('.$skip.','.$take.')', $query);
    }
}
```

The `cache` method takes three parameters:

- The unique key associated with the method's intentions
- The query `Builder` object for the Eloquent query
- The optional verb, `get`, `first`, `list`, `paginate` etc; `get` by default

If the method associated with the optional verb takes parameters, like `paginate`, the parameters can be expressed as a comma separated list following the verb and a colon. If a parameter expects an array of literal values, these may be expressed as a pipe delimited sting.

```
/**
 * Paginate users with all pagination parameters
 */
public function getAllUsers()
{
    $query = $this->user->query();

    return $this->cache('all', $query, 'paginate:15,id|email|name,sheet,2');
    // $query->paginate(15, ['id', 'email', 'name'], 'sheet', 2);
}
```

The cache service will automatically index all of the unique keys used by your application. These keys will be used when the `flushCache` method is called on each service implementing the base cache service.

### Configure caching

[](#configure-caching)

For each of the services you implement using the `EloquentCache` you can configure the following:

#### Duration of cache minutes

[](#duration-of-cache-minutes)

```
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheForMinutes = 15;

    //
}
```

#### Disable caching

[](#disable-caching)

```
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableCaching = false;

    //
}
```

#### Disable logging

[](#disable-logging)

```
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableLogging = false;

    //
}
```

#### Set a custom cache index for the keys

[](#set-a-custom-cache-index-for-the-keys)

```
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheIndexKey = 'my-service-keys-index';

    //
}
```

### Flushing cache

[](#flushing-cache)

Each service object that implements caching via `Stevenmaguire\Laravel\Services\EloquentCache` can flush its own cache, independently of other consuming services.

Your application can flush the cache for all keys within the service object `serviceKey` group.

```
$userRegistrar->flushCache();
```

Your application can only flush the cache for keys within the service object `serviceKey` group that match a particular regular expression pattern.

```
// Flush cache for all cached users with a single digit user id
$userRegistrar->flushCache('^id\([0-9]{1}\)$');
```

### Bind to IoC Container

[](#bind-to-ioc-container)

```
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->when('App\Handlers\Events\UserHandler')
          ->needs('Stevenmaguire\Laravel\Contracts\Cacheable')
          ->give('App\Services\UserRegistrar');
    }
}
```

In this particular example, `UserHandler` is responsible for flushing the user service cache when a specific event occurs. The `UserHandler` takes a dependacy on the `flushCache` method within the `UserRegistrar` service.

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Steven Maguire](https://github.com/stevenmaguire)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~29 days

Recently: every ~10 days

Total

7

Last Release

3861d ago

Major Versions

0.1.6 → 1.0.02015-10-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9c05a30823ae19fc54aa4b4721a696c253f8dec10c7fd22d372cfdc0fcb36d?d=identicon)[stevenmaguire](/maintainers/stevenmaguire)

---

Top Contributors

[![stevenmaguire](https://avatars.githubusercontent.com/u/1851973?v=4)](https://github.com/stevenmaguire "stevenmaguire (35 commits)")

---

Tags

laravelormeloquentcachequery builderstevenmaguire

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/stevenmaguire-laravel-cache/health.svg)

```
[![Health](https://phpackages.com/badges/stevenmaguire-laravel-cache/health.svg)](https://phpackages.com/packages/stevenmaguire-laravel-cache)
```

###  Alternatives

[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[ymigval/laravel-model-cache

Laravel package for caching Eloquent model queries

7642.2k3](/packages/ymigval-laravel-model-cache)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[eusonlito/laravel-database-cache

Cache Database Query results on Laravel Query Builder or Eloquent

194.2k](/packages/eusonlito-laravel-database-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
