Download the PHP package trm42/cache-decorator without Composer

On this page you can find all versions of the php package trm42/cache-decorator. 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 cache-decorator

(Magical) Cache Decorator for Laravel

Tests PHPStan Code style

A transparent caching decorator for any Laravel-side class — services, API clients, query objects, repositories, you name it. Sub-class CacheDecorator, point it at the object you want to cache, and every public method call is automatically cached on first run and served from the cache on subsequent calls.

Stop writing boilerplate like this for every class whose results you want to cache:

With CacheDecorator the above shrinks to:

TTL is read from config, not from a $ttl property. The constructor calls getConfig(), which overwrites $ttl from cache_decorator.ttl (default 300 seconds; repository_cache.ttl for RepositoryCacheDecorator). To override it per-instance, call ->ttl(...) after construction (e.g. in your subclass constructor) — it accepts int seconds, a DateInterval, a DateTimeInterface, or null to bypass the cache entirely. See Fluent configuration for the full chainable grammar.

…and use it like this:

The decorator forwards any method not listed in $excludes to the underlying object via __call() and caches the result. Forwarding goes through Laravel's ForwardsCalls trait, so calls also reach methods the decorated object exposes through its own __call() magic — not just declared methods. Calling a method that exists nowhere on the decorated object throws UndefinedMethodException (see Object arguments in cache keys.

Fluent configuration

Every configuration setter is chainable (returns the decorator) and there's an expressive, self-documenting grammar for tuning an instance at runtime — no subclass property edits required:

Method Returns Behavior
ttl($ttl) static Set TTL (seconds / DateInterval / DateTimeInterface / null to bypass).
enable() static Turn caching on.
disable() static Turn caching off (forwards straight to the inner object).
prefix(?string) static Set the cache-key prefix.
withTags(array) static Set the cache tags.
tagCleaners(array) static Set the methods that flush the tags.
exclude(string ...) static Append method names to the never-cache list.

If you add your own public methods to a CacheDecorator subclass, remember that any method not listed in $excludes is forwarded to the inner object by __call(). The fluent methods above are already excluded by the base class, so chaining always resolves on the decorator and never leaks to the inner instance.

Object arguments in cache keys

Method arguments may be scalars, arrays, or objects — the decorator folds each argument into the cache key through a stable identity rule:

  1. Scalars / null / bool — cast as-is (existing scalar-only keys are byte-identical, so upgrading invalidates nothing).
  2. UrlRoutable (Eloquent models, etc.) — uses getRouteKey(), the model's natural stable identity. Two distinct instances with the same route key share a cache entry.
  3. BackedEnum — uses its ->value; Stringable / __toString — string-cast.
  4. Anything else (plain objects, nested arrays of objects) — a bounded, stable hash (json_encode, falling back to md5(serialize(...))).

Arrays are flattened (Arr::dot) and each leaf runs through the same rule, so nested objects are handled too.

To customize how a given argument type contributes to the key, override the protected normalizeArgument(mixed $value): string seam in your subclass — it sits alongside generateCacheKey(), getCache(), and putCache() as a first-class extension point.

Fluent / self-returning methods

forwardDecoratedCallTo() rewrites a fluent return $this; from the inner object back to the decorator, so method chaining stays on the cached surface instead of escaping to the bare inner instance.

Two conventions make this transparent and type-safe:

Optional: have the decorator instantiate the inner class for you

If you don't want to wire the inner instance yourself, override decoratedClass() to return its FQCN and you can construct the decorator with no arguments:

The FQCN returned by decoratedClass() is resolved through Laravel's service container (via resolve()), so the decorated class may declare constructor dependencies (they are auto-wired), and decoratedClass() may return an interface that's bound in the container.

Custom caching logic for a single method

If a particular method needs hand-tuned caching, override it in the subclass and use the protected helpers:

getCache() returns the cacheMiss() sentinel (a shared stdClass instance) when the entry is absent — comparing with === against cacheMiss() lets the override round-trip falsy payloads (0, '', [], false, null) correctly. Don't use truthiness checks like if (!$res): they would treat a legitimately cached false/0/[] as a miss and refetch on every call.

Cache tags

If your cache driver supports tags, declare which methods invalidate the tag bucket:

Exceptions

All errors thrown by the package live in the Trm42\CacheDecorator\Exceptions namespace and extend a single abstract base, CacheDecoratorException. Catch that base type to handle any cache-decorator-specific failure in one place, or catch a concrete subclass to distinguish the failure mode:

Exception Thrown when
CacheDecoratorException (abstract base — never thrown directly; catch it to handle every error below)
MissingDecoratedObjectException No instance was passed to the constructor and decoratedClass() returned null, so there is nothing to wrap.
UndefinedMethodException A forwarded call targets a method that exists nowhere on the decorated object.

Breaking change. These types previously surfaced as the SPL exceptions LogicException (missing decorated object) and BadMethodCallException (undefined method). They now extend \Exception via CacheDecoratorException and are not instances of those SPL classes — update any catch (LogicException ...) / catch (BadMethodCallException ...) blocks that relied on the old types.

Using with repositories

For repository-flavored use cases the package ships RepositoryCacheDecorator. It behaves exactly like CacheDecorator but reads its config from the repository_cache.* namespace instead of cache_decorator.*, so repository caches can be tuned independently of other decorators.

Install

Install with composer:

Publish whichever config you need (or both):

Environment variables:

Config Env var Default
cache_decorator.enabled CACHE_DECORATOR_ENABLED true
cache_decorator.ttl CACHE_DECORATOR_TTL 300
cache_decorator.use_tags CACHE_DECORATOR_TAGS true
repository_cache.enabled REPOSITORY_CACHE true
repository_cache.ttl REPOSITORY_CACHE_TTL 300
repository_cache.use_tags REPOSITORY_CACHE_TAGS true

Upgrading

From the previous 0.x line

A few breaking changes tightened the public contract:

From the repository-only version

The base class is now generic and the repository-specific glue has been removed in favor of the generic hooks:

From Laravel 5.x versions

This release targets Laravel 12 and 13 on PHP 8.2+. A few breaking changes:

Tested with Laravel 12 and Laravel 13.


All versions of cache-decorator with dependencies

PHP Build Version
Package Version
Requires illuminate/cache Version ~5.0
illuminate/config Version ~5.0
illuminate/support Version ~5.0
illuminate/log Version ~5.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 trm42/cache-decorator contains the following files

Loading the files please wait ...