Download the PHP package aftandilmmd/laravel-cacheable without Composer

On this page you can find all versions of the php package aftandilmmd/laravel-cacheable. 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-cacheable

Laravel Cacheable

Tests Latest Stable Version

Türkçe: README.tr.md

Add #[Cacheable] to any method. That's it — the package handles TTL, tags, locking, invalidation, and stale-while-revalidate automatically.

Requires: PHP 8.2+ · Laravel 10 / 11 / 12 / 13


Installation

Auto-discovered. No provider or alias registration needed.

Optionally publish the config file:


How it works

The package intercepts method calls and stores their return values in cache. On the next call with the same arguments, the cached value is returned without executing the method body.

There are two ways to make this interception happen.


Real-world example

The most natural fit for this package is a Repository — a class that owns all database reads for a model. Annotate the read methods, attach forget to the write methods, register the repository in auto_proxy, and the rest is invisible.

Register it once in config:

Now every injected instance is automatically cached — no change to controllers or other callers:


Calling cached methods

Option A — Auto-proxy (recommended)

Register your service in config/cacheable.php. Every container-resolved instance is then automatically wrapped — you call methods normally and caching is invisible.

Note: new UserService() bypasses the container and won't be proxied. Always resolve through DI or app().

Option B — Manual proxy

No config needed. Wrap on the spot, call methods naturally:

Option C — Explicit dispatcher

If you prefer explicit over magic, use the HasCacheableMethods trait:

Self-call limitation: PHP cannot intercept $this->method() inside the same class. Use $this->cached('method', [...]) for internal calls.

Static methods via proxy

CacheableProxy::wrapClass returns a proxy object that intercepts static calls by name:


Annotating methods

Basic TTL

Key with placeholders

Placeholders resolve to method argument values. Nested properties also work:

Cache forever

Tags

Requires a taggable store (redis, memcached, array). The file and database drivers silently ignore tags.

Conditional caching

Skip caching based on runtime conditions without changing the call site:

unless is the inverse — skip cache when the method returns true.

Exclude arguments from the key

Inject heavy objects (Request, Logger) without polluting the cache key:


Cache invalidation

On write methods

Attach forget or forgetTags to a mutating method. Cache entries are deleted automatically after the method runs:

Via facade

Manually delete a specific entry or flush a tag group:

Version bump

Bump the version parameter to invalidate everything at once without touching the cache store:

Or per attribute:


Stampede protection

When many concurrent requests miss the same key, only one should hit the database. Use a distributed lock:

To spread expiration times across many keys and avoid simultaneous cache misses, add jitter:


Stale-while-revalidate

Serve cached data immediately while refreshing in the background. refreshAhead: 0.2 means "trigger a refresh in the final 20% of the TTL window":

For async refresh via the queue, enable it in config:


Events

Listen in app/Providers/AppServiceProvider.php:

Event Fires when Properties
CacheHit Cached value returned key, class, method, value
CacheMissed No cache — method will run key, class, method
CacheWritten Result stored in cache key, class, method, value, ttl
CacheForgotten Keys/tags invalidated class, method, keys, tags

Disable all events globally: set cacheable.events.enabled = false in config.


Debugging

If the key looks like a hash, set an explicit key: template in the attribute for human-readable keys.


Configuration

Publish and edit config/cacheable.php to set global defaults. Every attribute parameter that accepts null falls back to these values.


Attribute reference

All parameters are optional. Omitting a parameter uses the config default.

Parameter Type Description
key ?string Key template. Supports {param} and {param.property} placeholders. Auto-generated when null.
prefix ?string Prepended to the key.
ttl ?int Seconds. null = cache forever.
tags string[] Tag groups. Requires taggable store.
store ?string Override cache store.
keyParams string[] Whitelist: only these params are used in key generation.
excludeParams string[] Blacklist: these params are excluded from key generation.
when ?string Method name on $this → cache only when it returns true.
unless ?string Method name on $this → skip cache when it returns true.
cacheNull ?bool Store null return values.
cacheEmpty ?bool Store empty arrays / strings / Collections.
lock bool Enable distributed lock for stampede protection.
lockWait ?int Seconds to wait for lock.
jitter ?int Random seconds added to TTL.
refreshAhead ?float 0..1 — fraction of TTL at which to trigger refresh.
forget string[] Key templates to delete after this method runs.
forgetTags string[] Tags to flush after this method runs.
version ?string Embedded in key. Bump to invalidate all entries.
hashAlgo ?string Hash algorithm for auto-generated keys.
serializer ?string Argument serializer: json / serialize / igbinary.

Extending

Custom key resolver

Custom argument normalizer

You can also swap the entire caching pipeline by implementing CacheAspect.


Troubleshooting

$this->method() isn't cached. PHP can't intercept self-calls. Use $this->cached('method', [...]) or inject a proxy.

Static method isn't cached. auto_proxy and Cacheable::proxy() only wrap instances. Use HasCacheableMethods + cached('method', [...]) or CacheableProxy::wrapClass(MyClass::class)->method().

Tags do nothing. Tags require a taggable store (redis, memcached, array). Switch the store or use forget keys instead.

Cache isn't cleared between tests. Add Cache::flush() to your test's setUp().


License

MIT © Aftandilmmd. See LICENSE.md.


All versions of laravel-cacheable with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/cache Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/config Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/container Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/events Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/support Version ^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 aftandilmmd/laravel-cacheable contains the following files

Loading the files please wait ...