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

ActiveLibrary

erjon/laravel-cache-groups
==========================

Laravel cache groups for older versions and for drivers that don't support cache tags

v1.0.0(1mo ago)01↑2900%MITPHPPHP ^7.2|^8.0

Since Mar 25Pushed 1mo agoCompare

[ Source](https://github.com/ErjonStafa/LaravelCacheGroups)[ Packagist](https://packagist.org/packages/erjon/laravel-cache-groups)[ RSS](/packages/erjon-laravel-cache-groups/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Laravel Cache Groups
====================

[](#laravel-cache-groups)

[![Latest Version on Packagist](https://camo.githubusercontent.com/26c17b94ff71344ca8653b865aa5d48071857cf7a2da6b1e7946fb2c73351dac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65726a6f6e2f6c61726176656c2d63616368652d67726f7570732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/erjon/laravel-cache-groups)[![Total Downloads](https://camo.githubusercontent.com/6de49c36423ac28635a43794d18f0af91cbd1a8738f4f3c4f22b3eab52b45f09/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65726a6f6e2f6c61726176656c2d63616368652d67726f7570732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/erjon/laravel-cache-groups)

A Laravel package that adds `cache()->groups()` functionality to cache drivers that don't support `cache()->tags()`. This package provides a drop-in replacement for cache tags, making it work with **all** cache drivers including File, Database, and others that lack native tagging support.

Features
--------

[](#features)

- **Universal Support**: Works with all Laravel cache drivers (File, Database, Redis, Memcached, etc.)
- **Drop-in Replacement**: Identical API to `cache()->tags()` - just replace `tags()` with `groups()`
- **Native Fallback**: Automatically uses native `tags()` when available for optimal performance
- **Laravel 7+**: Supports Laravel 7.x through 11.x

Why Cache Groups?
-----------------

[](#why-cache-groups)

Laravel's `cache()->tags()` is powerful but only works with Redis, Memcached, and Array cache drivers. If you're using File or Database drivers, calling `tags()` throws a `BadMethodCallException`.

**Cache Groups** solves this by:

- Tracking cache keys explicitly for each group
- Enabling selective cache invalidation (flush by group)
- Providing the same API experience across all cache drivers
- Automatically falling back to native tags when supported

### Comparison: Cache Tags vs Cache Groups

[](#comparison-cache-tags-vs-cache-groups)

Feature`cache()->tags()``cache()->groups()`**File Driver Support**❌ No✅ Yes**Database Driver Support**❌ No✅ Yes**Redis Support**✅ Yes✅ Yes (delegates to tags)**Memcached Support**✅ Yes✅ Yes (delegates to tags)**Array Driver Support**✅ Yes✅ Yes (delegates to tags)**API Compatibility**Standard100% Compatible**Performance on Redis**OptimalOptimal (uses native tags)**Performance on File**N/AGood (explicit tracking)**Laravel Version**5.8+7.0+**Migration Effort**N/AReplace `tags()` with `groups()`Installation
------------

[](#installation)

Install the package via Composer:

```
composer require erjon/laravel-cache-groups
```

The package will automatically register itself via Laravel's package auto-discovery.

### Manual Registration (Laravel 5.x - 6.x)

[](#manual-registration-laravel-5x---6x)

If using older Laravel versions without auto-discovery, add the service provider to `config/app.php`:

```
'providers' => [
    // ...
    Erjon\CacheGroups\Providers\CacheGroupsServiceProvider::class,
],
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

The API is identical to Laravel's `cache()->tags()`:

```
use Illuminate\Support\Facades\Cache;

// Store a value in a group
Cache::groups(['products'])->put('item:1', $product, 3600);

// Retrieve a value
$product = Cache::groups(['products'])->get('item:1');

// Store with multiple groups
Cache::groups(['products', 'featured'])->put('item:1', $product, 3600);

// Flush all items in a group
Cache::groups(['products'])->flush();
```

### Available Methods

[](#available-methods)

All standard Laravel cache methods are supported:

```
// Store items
Cache::groups(['products'])->put('key', 'value', $seconds);
Cache::groups(['products'])->forever('key', 'value');
Cache::groups(['products'])->add('key', 'value', $seconds);
Cache::groups(['products'])->putMany(['key1' => 'value1', 'key2' => 'value2'], $seconds);

// Retrieve items
Cache::groups(['products'])->get('key');
Cache::groups(['products'])->get('key', 'default');
Cache::groups(['products'])->many(['key1', 'key2']);
Cache::groups(['products'])->has('key');

// Remember pattern
Cache::groups(['products'])->remember('key', $seconds, function () {
    return expensive_computation();
});

Cache::groups(['products'])->rememberForever('key', function () {
    return expensive_computation();
});

// Increment/Decrement
Cache::groups(['products'])->increment('counter', 1);
Cache::groups(['products'])->decrement('counter', 1);

// Remove items
Cache::groups(['products'])->forget('key');
Cache::groups(['products'])->flush(); // Flush entire group
```

### Variadic Group Syntax

[](#variadic-group-syntax)

You can also pass groups as separate arguments:

```
// These are equivalent:
Cache::groups(['products', 'featured'])->put('key', 'value', 3600);
Cache::groups('products', 'featured')->put('key', 'value', 3600);
```

How It Works
------------

[](#how-it-works)

### For Drivers Without Tag Support (File, Database)

[](#for-drivers-without-tag-support-file-database)

Cache Groups tracks all keys belonging to each group:

1. When you store a value: `Cache::groups(['products'])->put('item:1', $data, 3600)`

    - The key `item:1` is added to the `products` group's key list
    - This list is stored in the cache at `group:products:keys`
2. When you flush a group: `Cache::groups(['products'])->flush()`

    - All keys in the group's list are deleted
    - The group's key list itself is cleared
3. When you forget a key: `Cache::groups(['products'])->forget('item:1')`

    - The key is removed from the cache
    - The key is removed from all group membership lists (prevents memory leaks)

### For Drivers With Tag Support (Redis, Memcached, Array)

[](#for-drivers-with-tag-support-redis-memcached-array)

The package automatically detects if the cache driver supports tags and delegates to the native `tags()` implementation for better performance:

```
// On Redis/Memcached, this internally calls cache()->tags()
Cache::groups(['products'])->put('item:1', $data, 3600);
```

This ensures you get the best possible performance while maintaining API consistency.

Migration from Tags to Groups
-----------------------------

[](#migration-from-tags-to-groups)

If you're currently using `cache()->tags()` with Redis/Memcached and want to support File/Database drivers, simply replace `tags()` with `groups()`:

**Before:**

```
Cache::tags(['products'])->put('item:1', $data, 3600);
$data = Cache::tags(['products'])->get('item:1');
Cache::tags(['products'])->flush();
```

**After:**

```
Cache::groups(['products'])->put('item:1', $data, 3600);
$data = Cache::groups(['products'])->get('item:1');
Cache::groups(['products'])->flush();
```

That's it! Your code now works with all cache drivers.

Performance Considerations
--------------------------

[](#performance-considerations)

### Key Tracking Overhead

[](#key-tracking-overhead)

When using drivers without native tag support (File, Database), the package tracks keys explicitly:

- **Storage**: Each group maintains a list of its member keys in the cache
- **Write Operations**: Each `put()`, `forever()`, `add()` adds to the group's key list
- **Flush Operations**: Iterates through all tracked keys to delete them
- **Memory**: Large groups (10,000+ keys) may impact memory usage

### Best Practices

[](#best-practices)

1. **Use Native Tags When Possible**: If you're on Redis/Memcached, consider using `cache()->tags()` directly for maximum performance
2. **Limit Group Size**: Try to keep groups under 10,000 keys for optimal performance
3. **Group Strategically**: Organize your cache into logical groups that are flushed together

    ```
    // Good: Specific, manageable groups
    Cache::groups(['user:123'])->put('profile', $data, 3600);
    Cache::groups(['user:123'])->put('settings', $data, 3600);

    // Less optimal: One massive group
    Cache::groups(['all-data'])->put('random:key:' . $id, $data, 3600);
    ```
4. **Clean Up Manually**: While the package removes keys from groups when using `forget()`, expired keys will remain in group lists until the group is flushed. Consider periodic group flushes for long-lived groups with high churn.

Testing
-------

[](#testing)

Run the test suite:

```
composer install
vendor/bin/phpunit
```

Requirements
------------

[](#requirements)

- PHP 7.2 or higher
- Laravel 7.x, 8.x, 9.x, 10.x, or 11.x

License
-------

[](#license)

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

Changelog
---------

[](#changelog)

### 1.0.0 - Initial Release

[](#100---initial-release)

- Initial release with full cache groups support
- Support for Laravel 7+ and all cache drivers
- Automatic fallback to native tags when available
- Comprehensive test suite

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance90

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

45d ago

### Community

Maintainers

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

---

Top Contributors

[![ErjonStafa](https://avatars.githubusercontent.com/u/94717352?v=4)](https://github.com/ErjonStafa "ErjonStafa (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)

PHPackages © 2026

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