PHPackages                             yaroslavpopovic/laravel-policy-soft-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. yaroslavpopovic/laravel-policy-soft-cache

ActiveLibrary[Caching](/categories/caching)

yaroslavpopovic/laravel-policy-soft-cache
=========================================

Fork of innoge/laravel-policy-soft-cache that keys cache entries by spl\_object\_id, removing the json\_encode + sha512 overhead for high-volume policy checks.

v2.0.1(3w ago)013MITPHPPHP ^8.1

Since Nov 2Pushed 3w agoCompare

[ Source](https://github.com/yaroslavpopovic/laravel-policy-soft-cache)[ Packagist](https://packagist.org/packages/yaroslavpopovic/laravel-policy-soft-cache)[ Docs](https://github.com/yaroslavpopovic/laravel-policy-soft-cache)[ RSS](/packages/yaroslavpopovic-laravel-policy-soft-cache/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (7)Versions (16)Used By (0)

Laravel Policy Soft Cache — Fork
================================

[](#laravel-policy-soft-cache--fork)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3bf732d71423d35c7e579cf98c77e859dcab130e479f67a60d15c19e6331174d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7961726f736c6176706f706f7669632f6c61726176656c2d706f6c6963792d736f66742d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yaroslavpopovic/laravel-policy-soft-cache)

> **This is a fork of [`innoge/laravel-policy-soft-cache`](https://github.com/InnoGE/laravel-policy-soft-cache) by [Tim Geisendörfer](https://github.com/geisi).**All credit for the original design, API, and implementation goes to the upstream author. This fork only diverges on the cache-key strategy — see below.

Optimize your Laravel application's performance with soft caching for policy checks. This package caches policy invocations to prevent redundant checks within the same request lifecycle, enhancing your application's response times.

Why this fork
-------------

[](#why-this-fork)

The upstream package keys its in-memory cache by hashing every argument with `hash_hmac('sha512', json_encode($args), config('app.key'))`. That's robust against in-request model mutations (any attribute change invalidates the key automatically), but on workloads that perform many policy checks per request — for example a Filament 5 table where each row renders an `ActionGroup` with 7+ actions and Filament re-evaluates `isVisible()` / `isDisabled()` / `isAuthorized()` multiple times per action — the key generation cost alone becomes the bottleneck.

In a real-world Filament list page (≈40 records, ~760 `Gate::check` calls during a single `unmountAction` re-render), we measured:

Key strategyCost per `Gate::check`Overhead for the whole request`hash_hmac('sha512', json_encode($args), app.key)` (upstream)~0.72 ms~550 ms`spl_object_id($model)` (this fork)~0.0003 ms&lt;1 msEnd-to-end the difference moved the same Livewire `unmountAction` request from **9.95 s → 6.6 s** in the application that motivated the fork.

This fork swaps the key strategy: object arguments are fingerprinted by `get_class($obj).':'.spl_object_id($obj)`, scalars are stringified, and non-Eloquent objects/arrays still fall back to a fast `xxh3` hash. The cache is **hit only for the same in-memory instance** — which is what Filament (and most rendering loops) actually pass during a single render, so the practical hit rate is unchanged for the use case the package was built for.

If you need staleness-on-mutation semantics (the cache key invalidating when a model attribute changes within the same request) please use the **upstream package** — that's exactly what its `json` strategy is for.

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

[](#requirements)

PHP ≥ 8.1, Laravel 9 / 10 / 11 / 12 / 13.

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

[](#installation)

```
composer require yaroslavpopovic/laravel-policy-soft-cache
```

Optionally publish the config file:

```
php artisan vendor:publish --provider="Innoge\LaravelPolicySoftCache\LaravelPolicySoftCacheServiceProvider"
```

```
return [
    /*
     * When enabled, the package will cache the results of all Policies in your Laravel application
     */
    'cache_all_policies' => env('CACHE_ALL_POLICIES', true),
];
```

Usage
-----

[](#usage)

By default, all policy calls are cached for the lifetime of the current request. To opt in selectively instead, set `CACHE_ALL_POLICIES=false` and mark the policies you want cached with `Innoge\LaravelPolicySoftCache\Contracts\SoftCacheable`:

```
use Innoge\LaravelPolicySoftCache\Contracts\SoftCacheable;

class UserPolicy implements SoftCacheable
{
    // ...
}
```

Clearing the cache
------------------

[](#clearing-the-cache)

```
\Innoge\LaravelPolicySoftCache\LaravelPolicySoftCache::flushCache();
```

Known issues
------------

[](#known-issues)

### `Gate::before` and Service Provider load order

[](#gatebefore-and-service-provider-load-order)

If your application registers a `Gate::before` callback (typically in `AuthServiceProvider`), Laravel's package auto-discovery may load this provider before yours, which means your `Gate::before` runs first and can short-circuit the cache. To enforce explicit load order, register the provider manually and disable auto-discovery for this package:

1. Add `\Innoge\LaravelPolicySoftCache\LaravelPolicySoftCacheServiceProvider::class` at the end of `bootstrap/providers.php` (Laravel 11+) or `config/app.php` `providers` array.
2. In your `composer.json`:

    ```
    "extra": {
        "laravel": {
            "dont-discover": ["yaroslavpopovic/laravel-policy-soft-cache"]
        }
    }
    ```
3. Run `composer install` so the change takes effect.

Testing
-------

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- **[Tim Geisendörfer](https://github.com/geisi)** — original author of `innoge/laravel-policy-soft-cache`. All the package architecture, the `SoftCacheable` interface, and the `Gate::before` hook strategy are his work.
- [Yaroslav Popovic](https://github.com/yaroslavpopovic) — fork maintainer, swapped the cache-key strategy to `spl_object_id` for high-throughput rendering use cases.
- [Upstream contributors](https://github.com/InnoGE/laravel-policy-soft-cache/graphs/contributors)

License
-------

[](#license)

MIT. See [LICENSE.md](LICENSE.md).

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance94

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 83.8% 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 ~99 days

Recently: every ~189 days

Total

14

Last Release

26d ago

Major Versions

0.1.1 → 1.0.02023-01-01

1.6.0 → v2.0.02026-05-14

### Community

Maintainers

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

---

Top Contributors

[![geisi](https://avatars.githubusercontent.com/u/10728579?v=4)](https://github.com/geisi "geisi (31 commits)")[![yaroslavpopovic](https://avatars.githubusercontent.com/u/12303752?v=4)](https://github.com/yaroslavpopovic "yaroslavpopovic (2 commits)")[![bessone](https://avatars.githubusercontent.com/u/1089510?v=4)](https://github.com/bessone "bessone (1 commits)")[![Perer876](https://avatars.githubusercontent.com/u/70984218?v=4)](https://github.com/Perer876 "Perer876 (1 commits)")[![ruaq](https://avatars.githubusercontent.com/u/25234289?v=4)](https://github.com/ruaq "ruaq (1 commits)")[![vazaha-nl](https://avatars.githubusercontent.com/u/1075085?v=4)](https://github.com/vazaha-nl "vazaha-nl (1 commits)")

---

Tags

laravelinnogelaravel-policy-soft-cache

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/yaroslavpopovic-laravel-policy-soft-cache/health.svg)

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M41](/packages/spatie-laravel-pdf)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k9.9M87](/packages/dedoc-scramble)[spatie/laravel-health

Monitor the health of a Laravel application

88011.3M149](/packages/spatie-laravel-health)[spatie/laravel-passkeys

Use passkeys in your Laravel app

463755.5k32](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3913.7k](/packages/rawilk-profile-filament-plugin)[innoge/laravel-policy-soft-cache

This package helps prevent performance problems with frequent Policy calls within your application lifecycle.

2561.0k](/packages/innoge-laravel-policy-soft-cache)

PHPackages © 2026

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