Skip to content
Easily cache items specific to your Eloquent models without having to append the ID.
Branch: master
Clone or download
Latest commit f364a12 May 10, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Change to better method name May 9, 2019
tests First commit May 9, 2019
.editorconfig First commit May 9, 2019
.gitattributes First commit May 9, 2019
.gitignore First commit May 9, 2019
README.md Update README.md May 10, 2019
composer.json Update version May 9, 2019
phpunit.xml.dist First commit May 9, 2019

README.md

Model specific cache items made easy

This package adds a trait that provides a cache method for interacting with your app's cache, but scoped to your Eloquent models. This removes the need to constantly append the model ID to your cache keys. Just access your cache like this instead $user->cache('Avatar').

Under the hood it will prefix your cache keys with a generated model-specific key, in the format Model_ID:your-key. You can completely override this format by implementing the getScopedCacheKey method on your model.

Installation

You can install the package via composer:

composer require jameslkingsley/laravel-scoped-cache

Usage

Import the trait and use it on any Eloquent model. You can also use it on non-eloquent classes, providing they have a getKey method that returns their unique reference.

use Kingsley\ScopedCache\ScopedCache;

class User extends Model
{
    use ScopedCache;
}

Now you can use the cache how you would normally. You can set/get items directly from the cache method, or call any other cache method by leaving the argument list empty.

$user->cache(['name' => $user->name], 5);
$user->cache('name', 'default name');

$user->cache()->remember('name', 5, function () {
    // $this refers to the model this cache instance is scoped to
    return $this->name;
});

Keep in mind that this package is expecting the first argument of proxied calls to the cache to be the cache key. There might be some cases where custom macro's break this format.

You can’t perform that action at this time.