Skip to content

Easily cache items specific to your Eloquent models without having to append the ID.

Notifications You must be signed in to change notification settings

jameslkingsley/laravel-scoped-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Easily cache items specific to your Eloquent models without having to append the ID.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages