PHPackages                             hkp22/cache-laraview-fragments - 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. hkp22/cache-laraview-fragments

ActiveLibrary[Caching](/categories/caching)

hkp22/cache-laraview-fragments
==============================

Caching Laravel view's fragments.

1.0.0(7y ago)291MITPHPPHP ^5.6|^7.0

Since Sep 20Pushed 7y agoCompare

[ Source](https://github.com/hkp22/cache-laraview-fragments)[ Packagist](https://packagist.org/packages/hkp22/cache-laraview-fragments)[ RSS](/packages/hkp22-cache-laraview-fragments/feed)WikiDiscussions master Synced 2d ago

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

Cache Laraview Fragments
========================

[](#cache-laraview-fragments)

Laravel view fragment caching support.

Installation
------------

[](#installation)

### Composer

[](#composer)

Download package into the project using Composer.

```
composer require hkp22/cache-laraview-fragments
```

### Registering Service Provider

[](#registering-service-provider)

> Laravel 5.5 (or higher) uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

For Laravel 5.4 or earlier releases version include the service provider within `app/config/app.php`:

```
'providers' => [
    Hkp22\CacheLaraViewFragments\CacheLaraViewFragmentServiceProvider::class,
],
```

### Cache Driver

[](#cache-driver)

This package has used Laravel tags (like `Cache::tags('foo')`) to store cache. So, you must use the Laravel cache driver which supports tagging, such as `Memcached` and `Redis`.

Make sure required `CACHE_DRIVER` is used in `.env` file.

```
CACHE_DRIVER=redis

```

Usage
-----

[](#usage)

After package installation, you may use `@cache` Blade directive in your views.

```
@cache('my-cache-key')

        Hello World

@endcache
```

In production environment, this will cache the HTML fragment "forever." For local development, on the other hand, it will automatically flush the relevant cache for you each time you refresh the page.

#### Clear Cache

[](#clear-cache)

Now because your production server will cache the fragments forever, You should add a step to deployment process that clears the relevant cache.

```
Cache::tags('views')->flush();
```

### Caching Models

[](#caching-models)

Consider the following example:

```
@cache($post)

        {{ $post->title }}>
        Written By: {{ $post->author->username }}

        {{ $post->body }}

@endcache

```

In this above example, `$post` object is passed to the `@cache` directive rather than a string. It will look for a `getCacheKey()` method on the model and this method is defined in `Hkp22\CacheLaraViewFragments\Cacheable` trait. So, use this trait in the model.

```
use Hkp22\CacheLaraViewFragments\Cacheable;

class Post extends Eloquent
{
    use Cacheable;
}
```

The cache key for model will include the object's `id` and `updated_at` timestamp: `App\Post/1-1537459253`.

> Note: Because `updated_at` timestamp used into the cache key, So whenever post is updated, the cache key will change.

#### Touching

[](#touching)

Consider the following example: **resources/views/posts/\_post.blade.php**

```
@cache($post)

        {{ $post->title }}

            @foreach ($post->comments as $comment)
                @include ('posts/_comment')
            @endforeach

@endcache
```

**resources/views/posts/\_comment.blade.php**

```
@cache($post)
    {{ $post->body }}
@endcache
```

In this above example whenever a new comment is created or existing comment is updated. It will not change the view HTML because view HTML is fetched from cache.

To fix this need to add `$touches` to the child model (Comment model in this case). So, whenever a new comment is created or updated, it would update the parent's `updated_at` timestamp.

So, in this case `Comment` model should like this:

```
