Download the PHP package yaroslavpopovic/laravel-policy-soft-cache without Composer

On this page you can find all versions of the php package yaroslavpopovic/laravel-policy-soft-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-policy-soft-cache

Laravel Policy Soft Cache — Fork

Latest Version on Packagist

This is a fork of innoge/laravel-policy-soft-cache by Tim Geisendörfer. 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

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 strategy Cost 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 <1 ms

End-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

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

Installation

Optionally publish the config file:

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:

Clearing the cache

Known issues

Gate::before 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:

  3. Run composer install so the change takes effect.

Testing

Credits

License

MIT. See LICENSE.md.


All versions of laravel-policy-soft-cache with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
spatie/laravel-package-tools Version ^1.13.0
illuminate/contracts Version ^9.0|^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package yaroslavpopovic/laravel-policy-soft-cache contains the following files

Loading the files please wait ...