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

ActiveLibrary[Caching](/categories/caching)

abr4xas/cache-version-laravel
=============================

YOLO

00PHP

Since Nov 7Pushed 9mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

 [![Laravel Logo](https://camo.githubusercontent.com/add54281c9fb74f167b2c1f1e13468346d6ef8885dc90eb7036e09b579462947/68747470733a2f2f6c61726176656c2e636f6d2f696d672f6c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/add54281c9fb74f167b2c1f1e13468346d6ef8885dc90eb7036e09b579462947/68747470733a2f2f6c61726176656c2e636f6d2f696d672f6c6f676f6d61726b2e6d696e2e737667)

Cache Versioned for Laravel
===========================

[](#cache-versioned-for-laravel)

`abr4xas/cache-version-laravel` is a lightweight helper around Laravel's cache system that automatically versions cache keys behind a human-friendly prefix. That lets you invalidate entire groups of cached data without touching individual keys or writing custom eviction logic—perfect for paginated feeds, dashboards, and expensive queries.

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

[](#requirements)

- PHP 8.4 or later
- Laravel Framework 12.37 or later

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

[](#installation)

Install via Composer:

```
composer require abr4xas/cache-version-laravel
```

The package registers its service provider automatically thanks to Laravel Package Discovery, so no additional setup is required.

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

[](#how-it-works)

Every cache entry is stored using the following pattern:

```
:v:

```

- **Prefix** — A logical namespace for a group of cached values.
- **Version** — An auto-incremented integer stored in cache to invalidate the entire prefix.
- **Key** — Whatever identifier you need underneath the prefix.

When you call `flush('prefix')`, the version increments and all previously stored entries become unreachable without needing to manually delete any keys.

Usage Examples
--------------

[](#usage-examples)

```
use Abr4xas\CacheVersionedLaravel\Facade\CacheVersioned as VersionedCache;
use App\Models\Post;
use App\Models\Setting;

// 1️⃣ Basic usage with TTL (seconds or DateInterval)
$posts = VersionedCache::remember(
    'angel_cruz_cache_blog_page',
    'posts:page:1',
    3600,
    fn () => Post::latest()->paginate(10)
);

// 2️⃣ Cache forever without a TTL
$settings = VersionedCache::rememberForever(
    'site_settings',
    'home_page',
    fn () => Setting::all()->pluck('value', 'key')
);

// 3️⃣ Invalidate an entire prefix instantly
VersionedCache::flush('angel_cruz_cache_blog_page');

// 4️⃣ Use a specific cache store for an operation
$pageNine = VersionedCache::remember(
    'angel_cruz_cache_blog_page',
    'posts:page:9',
    3600,
    fn () => Post::latest()->paginate(10),
    store: 'array_secondary' // or 'file', 'redis', 'database', etc.
);
```

### Service Container Access

[](#service-container-access)

If you prefer dependency injection, resolve the service from the container:

```
use Abr4xas\CacheVersionedLaravel\CacheVersioned;

class DashboardController
{
    public function __construct(
        private CacheVersioned $cache
    ) {}

    public function __invoke()
    {
        return $this->cache->remember('dashboard', 'stats', 600, function () {
            // Heavy aggregation work...
        });
    }
}
```

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

[](#available-methods)

- `put(string $prefix, string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null, ?string $store = null): bool`
- `get(string $prefix, string $key, mixed $default = null, ?string $store = null): mixed`
- `forget(string $prefix, string $key, ?string $store = null): bool`
- `flush(string $prefix, ?string $store = null): int`
- `remember(string $prefix, string $key, DateInterval|DateTimeInterface|int|null $ttl, callable $callback, ?string $store = null): mixed`
- `rememberForever(string $prefix, string $key, callable $callback, ?string $store = null): mixed`
- `version(string $prefix, ?string $store = null): int`
- `setVersion(string $prefix, int $version, ?string $store = null): bool`
- `reset(string $prefix, ?string $store = null): bool`

All methods accept an optional `$store` argument, allowing you to switch cache drivers per call without touching your business logic.

Testing
-------

[](#testing)

The repository includes a Pest test suite that exercises the full API surface, façade integration, and per-store isolation:

```
composer test
```

> The test suite uses Orchestra Testbench with in-memory cache drivers, so it runs without external services.

Tips &amp; Best Practices
-------------------------

[](#tips--best-practices)

- **Prefix consistently** — Use descriptive prefixes (e.g., `blog_posts`, `user_profile:{$userId}`) to control invalidation boundaries precisely.
- **Flush selectively** — Prefer `flush()` over manual key removal to keep your code succinct and less error-prone.
- **Custom stores** — Use per-call stores for long-lived caches (e.g., Redis) while keeping defaults on faster transient drivers.
- **Observe versions** — `version()` is handy for debugging and for exposing cache state in observability dashboards.

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

[](#contributing)

1. Fork the repository.
2. Install dependencies with `composer install`.
3. Run `composer test` to ensure the suite passes.
4. Open a pull request describing your change.

Please feel free to submit issues or enhancements. Contributions are welcome!

License
-------

[](#license)

The MIT License (MIT). Refer to the [LICENSE](LICENSE) file for more information.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance41

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d947508ce4076ed67ec719eb2c74696e025dab397eabc921d483545fb4c403f?d=identicon)[abr4xas](/maintainers/abr4xas)

---

Top Contributors

[![abr4xas](https://avatars.githubusercontent.com/u/405484?v=4)](https://github.com/abr4xas "abr4xas (1 commits)")

### Embed Badge

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

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

###  Alternatives

[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21114.2k](/packages/iazaran-smart-cache)[phpfastcache/phpssdb

PHP SSDB Driver for phpFastCache

14736.9k1](/packages/phpfastcache-phpssdb)[rapidwebltd/rw-file-cache

RW File Cache is a PHP File-based Caching Library. Its syntax is designed to closely resemble the PHP memcache extension.

15196.8k7](/packages/rapidwebltd-rw-file-cache)[yuzuru-s/redis-recommend

Wrapping Redis's sorted set APIs for specializing recommending operations.

392.9k](/packages/yuzuru-s-redis-recommend)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

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