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

ActiveLibrary[Caching](/categories/caching)

dv0vd/laravel-simple-cache
==========================

Simple trait for cache handling with tags support in Laravel

1.0.0(5mo ago)01MITPHPPHP &gt;=8.1

Since Nov 27Pushed 2mo agoCompare

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

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

Laravel Simple Cache Trait
==========================

[](#laravel-simple-cache-trait)

A lightweight Laravel trait that simplifies working with cache keys and tagged cache storage. It automatically generates stable hashed cache keys, handles nested arrays, sorts key data, and supports cache tags across all Laravel versions starting from **8.x**.

Features
--------

[](#features)

- Automatic cache key generation using SHA-256
- Deterministic sorting of array keys before hashing
- Tagged cache support (if the store supports it)
- Graceful fallback when caching fails
- Easy cache flushing for specific tag groups
- Works on Laravel **8+**

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

[](#installation)

```
composer require dv0vd/laravel-simple-cache
```

Usage
-----

[](#usage)

```
use Dv0vD\LaravelSimpleCache\CacheTrait;

class ProductRepository
{
    use CacheTrait;

    public function get(int $id)
    {
        return $this->rememberCache(
            callback: fn () => Product::find($id),
            keys: ['id' => $id],
        );
    }
}
```

Tag Constants (optional):

```
class ProductRepository
{
    use CacheTrait;

    private const CACHE_MAIN_TAG = 'products';
    private const CACHE_TAGS = ['repository', 'models'];

    public function get(int $id)
    {
        return $this->rememberCache(
            callback: fn () => Product::find($id),
            keys: ['id' => $id],
        );
    }
}
```

Clearing cache:

```
$this->clearCache();
```

```
$this->clearCache(['products']);
```

Available Methods
-----------------

[](#available-methods)

### rememberCache(callable $callback, array $keys = \[\], array $tags = \[\], ?int $ttl = null): mixed

[](#remembercachecallable-callback-array-keys---array-tags---int-ttl--null-mixed)

Stores or retrieves a value from cache:

- Automatically generates a cache key based on the calling class, method, and the `$keys` array (sorted &amp; hashed).
- Uses tags if the cache driver supports them.
- Logs and silently falls back to the callback if caching fails.
- `$ttl` (in seconds) is optional. Defaults to `cache.ttl` from your config.

### clearCache(array $tags = \[\]): void

[](#clearcachearray-tags---void)

Flushes all cache entries for the provided tags or default tags.

- By default, tags are taken from the constants `CACHE_MAIN_TAG` and `CACHE_TAGS`.
- If you pass tags as a parameter, they will override the default constants.
- Only cache entries with tags are cleared.
- If no tags are found, the method does nothing to avoid accidental data loss.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance79

Regular maintenance activity

Popularity1

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

164d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/491267c3bba6593625e74fe9908a3316eb5b7af6ef967c45638668d7a67188bd?d=identicon)[dv0vd](/maintainers/dv0vd)

---

Tags

laraveltraitrediscachetagged cache

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[swayok/alternative-laravel-cache

Replacements for Laravel's redis and file cache stores that properly implement tagging idea. Powered by cache pool implementations provided by http://www.php-cache.com/

202541.1k6](/packages/swayok-alternative-laravel-cache)[dragon-code/laravel-cache

An improved interface for working with cache

6844.8k10](/packages/dragon-code-laravel-cache)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)

PHPackages © 2026

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