PHPackages                             trm42/cache-decorator - 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. trm42/cache-decorator

ActiveLibrary[Caching](/categories/caching)

trm42/cache-decorator
=====================

Magical (as in saves manual work quite a lot) Cache Decorator for Laravel 5. Designed for usage with repositories but easily usable for other uses. If there's enough interest, can be made framework agnostic.

0.9.3(10y ago)42461[1 issues](https://github.com/trm42/CacheDecorator/issues)GPL-2.0PHP

Since Jan 4Pushed 9y ago1 watchersCompare

[ Source](https://github.com/trm42/CacheDecorator)[ Packagist](https://packagist.org/packages/trm42/cache-decorator)[ RSS](/packages/trm42-cache-decorator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (5)Versions (4)Used By (0)

(Magical) Cache Decorator for Laravel Repositories
==================================================

[](#magical-cache-decorator-for-laravel-repositories)

Repositories are really, really nice thing that solves real-world issues and follows the idea of DRY (Don't Repeat Yourself), but making similar classes for repository caching and repeating yourself over and over again for code like this:

```
namespace something\nice;

class CachedUserRepository {

	protected $repository;
	protected $cache;

	public function __construct(UserRepository $users, Cache $cache) {
		$this->repository = $users;
		$this->cache = $cache;
	}

	function all()
	{
		if (!$this->cache->has('all')) {
			$results = $this->repository->all();
			$this->cache->save('all', $results);
		} else {
			$results = $this->cache->get('all');
		}

		return $results;

	}

	function findByX($x)
	{
		$key = 'find-' . $x;
		if (!$this->cache->has($key)) {
			$results = $this->repository->findByX($x);
			$this->cache->save($key, $results);
		} else {
			$results = $this->cache->get($key);
		}

		return $results;
	}
}
```

And repeat this for every repository class in your project. Lots of dull repetition. Also, it doesn't help to make the caching code as part of your repository base class as it violates the single responsibility principle.

Cue (Magical) Cache Decorator which handles these things automatically for you with just few lines of class declaration:

```
namespace My\Repositories;

use Trm42\CacheDecorator\CacheDecorator;

class CachedUserRepository extends CacheDecorator {

	protected $ttl = 5; // cache ttl in minutes
	protected $prefix_key = 'users';
	protected $excludes = ['all']; // these methods are not cached

	public function repository()
	{
		return UserRepository::class;
	}

}
```

Aand you're set! The Cache Decorator caches every method call not in the $excludes array.

*Please note this the current version doesn't support objects as part of the method call. It will be added to v1.0.0.*

If you need something really special handling for some methods you can always override them in the Cached Repository class like this (simple example):

```
public function findByX($x)
{

	$key = $this->generateCacheKey(__FUNCTION__, compact($x));

	$res = $this->getCache($key);

	if (!$res) {
		$results = $this->repository->findX($x);

		$this->putCache($key, $results);
	}

	return $res;

}
```

If you happen to use a cache driver that enables you to use cache tags, you can clear the cache automatically when the data changes:

```
// Additional properties to add to the earlier example
// with class decoration
protected $tag_cleaners = ['create'];
protected $tags = ['users'];
```

Aaand you're set!

Install
-------

[](#install)

Install with composer:

```
composer require trm42/cache-decorator
```

Copy the default configuration:

```
cp vendor/trm42/cache-decorator/config/repository_cache.php ./config/
```

*Tested with Laravel 5.1 and 5.2.*

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.3% 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 ~57 days

Total

3

Last Release

3661d ago

### Community

Maintainers

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

---

Top Contributors

[![trm42](https://avatars.githubusercontent.com/u/4980285?v=4)](https://github.com/trm42 "trm42 (36 commits)")[![theel0ja](https://avatars.githubusercontent.com/u/5832930?v=4)](https://github.com/theel0ja "theel0ja (1 commits)")

---

Tags

laravelcachelaravel 5repositorydecorator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trm42-cache-decorator/health.svg)

```
[![Health](https://phpackages.com/badges/trm42-cache-decorator/health.svg)](https://phpackages.com/packages/trm42-cache-decorator)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[anahkiasen/flatten

A package for the Illuminate framework that flattens pages to plain HTML

33313.0k](/packages/anahkiasen-flatten)[dragon-code/laravel-cache

An improved interface for working with cache

6844.8k9](/packages/dragon-code-laravel-cache)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)[swiggles/memcache

Memcache driver for Laravel 5

1449.9k1](/packages/swiggles-memcache)

PHPackages © 2026

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