Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different cacheKey for the same model #8

Open
hugo-lovighi opened this issue Mar 11, 2016 · 2 comments
Open

Different cacheKey for the same model #8

hugo-lovighi opened this issue Mar 11, 2016 · 2 comments

Comments

@hugo-lovighi
Copy link

In your package, i can see that we have a single cached view for a model ?!

It could be great to pass different "versions of card model key" and generate several keys like :

App\Card/preview-1-1457710078
App\Card/full-1-1457710078

used in blade: @cache($card, ['preview']))
$item->getCacheKey('preview-version')

used in blade: @cache($card, ['full']))
$item->getCacheKey('full-version')

I'm working on your code to use it in my project, because in my case it's impossible to use different design for a same model for the moment.

@SumonMSelim
Copy link

@kesm0 any update on this?

@hugo-lovighi
Copy link
Author

hugo-lovighi commented Jul 8, 2016

Not really, I add some "tricky" code lines to make it work.
I modified 2 files.

BladeDirective.php : (line 72 to 76)

// Otherwise we'll try to use the item to calculate
// the cache key, itself.
if (is_object($item) && method_exists($item, 'getCacheKey')) {

        $version = $key[0];   // Sets multiple cache versions

        return $item->getCacheKey($version);
}

Cacheable.php : (line 13)

/**
 * Calculate a unique cache key for the model instance.
 */
public function getCacheKey($version)
{
    return sprintf("%s-%s/%s-%s",
        $version, // Get the required cache version
        get_class($this),
        $this->getKey(),
        $this->updated_at->timestamp
    );
}

And I use it in blade like this :

views/posts/home.php :

@foreach ($posts as $post)
    @cache($post, ['posts.home'])
        <h2>{{ $post->title }} - {{ $post->created_at }} - {{ $post->author->name }}</h2>
       <p>{{ $post->content }}</p>
    @endcache
@endforeach

views/posts/menu.php :

@foreach ($posts as $post)
    @cache($post, ['posts.menu'])
        <div>{{ $post->title }} - {{ str_limit($post->content, 30) }}</div>
    @endcache
@endforeach

When you update your post, every views are refresh !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants