PHPackages                             sfneal/caching - 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. [Caching](/categories/caching)
4. /
5. sfneal/caching

ActiveLibrary[Caching](/categories/caching)

sfneal/caching
==============

Traits &amp; interfaces for utilizing cache mechanisms to store frequently retrieved data.

4.1.0(2y ago)1117.5k[3 PRs](https://github.com/sfneal/caching/pulls)6MITPHPPHP ^8.0|^8.1|^8.2|^8.3CI passing

Since Aug 18Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/sfneal/caching)[ Packagist](https://packagist.org/packages/sfneal/caching)[ Docs](https://github.com/sfneal/caching)[ RSS](/packages/sfneal-caching/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (7)Versions (26)Used By (6)

Caching
=======

[](#caching)

[![Packagist PHP support](https://camo.githubusercontent.com/7caf053277a554a08b1717589fbe462ae665ff5bf7413ea9e6a5bacbc4c031df/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73666e65616c2f63616368696e67)](https://packagist.org/packages/sfneal/caching)[![Latest Version on Packagist](https://camo.githubusercontent.com/5cf4feb4ed212aeaa412f14da12911b6f586a3569a1e82c7f89b942b210759ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73666e65616c2f63616368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfneal/caching)[![Build Status](https://camo.githubusercontent.com/d5bb3763297d3699bdc0bf2e9b2d3c3ad5de3957e13da580370dd082734e2eb9/68747470733a2f2f7472617669732d63692e636f6d2f73666e65616c2f63616368696e672e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://travis-ci.com/sfneal/caching)[![StyleCI](https://camo.githubusercontent.com/e36f7b40479d44e82c0dab72cf35f0fb56d1dbb439045de64b5cef5b69667db8/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3238383439313933352f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/288491935?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/86317a7edfafd92b1ec29f3469bd7a4ab58d51a23d360c069cb4f8e13a8e58ca/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73666e65616c2f63616368696e672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sfneal/caching/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/80bd0499390232ce866075a795c2df036294c53239ae6855a9cfd780ab417ea7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73666e65616c2f63616368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfneal/caching)

Traits &amp; interfaces for utilizing cache mechanisms to store frequently retrieved data in Laravel applications.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require sfneal/caching
```

Usage
-----

[](#usage)

### 1. Add caching to an eloquent query

[](#1-add-caching-to-an-eloquent-query)

During the first call to `(new CountUnreadInquiriesQuery())->fetch(600)` in a fresh application instance (or for the first time since flushing the cache) the output of the execute method will be stored in the Redis cache for 5 minutes (600 seconds).

If another call to `(new CountUnreadInquiriesQuery())->fetch(600)` is made within the next 5 minutes, the previous output will be retrieved from the Redis cache, forgoing the need to execute a full query to the database. In this example the time saved is minimal but time saving become increasingly significant as the complexity of a query increases.

```
# Importing an example model that extends Sfneal/Models/AbstractModels
use App\Models\Inquiry;

# Import Cacheable trait that stores the output of the execute method in a Redis cache using the cacheKey method to set
# the key.  Any serializable output from execute can be stored, in this case we're simply storing an integer
use Sfneal\Caching\Traits\Cacheable;

# Importing AbstractQuery as we're building an Eloquent query cache
use Sfneal\Queries\AbstractQuery;

class CountUnreadInquiriesQuery extends AbstractQuery
{
    use Cacheable;

    /**
     * Retrieve the number of unread Inquiries.
     *
     * @return int
     */
    public function execute(): int
    {
        return Inquiry::query()->whereUnread()->count();
    }

    /**
     * Retrieve the Queries cache key.
     *
     * @return string
     */
    public function cacheKey(): string
    {
        # using AbstractModel::getTableName() to dynamically retrievethetable name to set the cache prefix
        return Inquiry::getTableName().':unread:count';
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

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)

- [Stephen Neal](https://github.com/sfneal)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

PHP Package Boilerplate
-----------------------

[](#php-package-boilerplate)

This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

48

—

FairBetter than 93% of packages

Maintenance49

Moderate activity, may be stable

Popularity30

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.6% 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 ~65 days

Recently: every ~248 days

Total

22

Last Release

772d ago

Major Versions

0.5.0 → 1.0.02021-01-26

1.3.2 → 2.0.02021-08-31

2.1.2 → 3.0.02022-02-28

3.0.0 → 4.0.02024-04-16

PHP version history (7 changes)0.1.0PHP ^7.1

0.2.1PHP ^7.2

0.3.0PHP ^7.0

0.4.0PHP &gt;=7.0

0.5.0PHP &gt;=7.1

3.0.0PHP ^7.3|^8.0

4.0.0PHP ^8.0|^8.1|^8.2|^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/ec5fdf3b9d2183ae9c777e4aed46fd5cfd9aa4789cd05c669e5907de0b8c865c?d=identicon)[sfneal](/maintainers/sfneal)

---

Top Contributors

[![sfneal](https://avatars.githubusercontent.com/u/23200215?v=4)](https://github.com/sfneal "sfneal (112 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

---

Tags

cachingsfneal

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sfneal-caching/health.svg)

```
[![Health](https://phpackages.com/badges/sfneal-caching/health.svg)](https://phpackages.com/packages/sfneal-caching)
```

###  Alternatives

[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[maartenstaa/laravel-41-route-caching

This package allows you to cache your routes definitions, thereby speeding up each request.

25381.4k](/packages/maartenstaa-laravel-41-route-caching)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21318.6k3](/packages/ecotone-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3416.9k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

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