PHPackages                             enlight/stash-view - 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. [Templating &amp; Views](/categories/templating)
4. /
5. enlight/stash-view

ActiveLibrary[Templating &amp; Views](/categories/templating)

enlight/stash-view
==================

Stash view is a composer package for Laravel which caches blade views using Russian Doll Caching methodology. Which drastically improves speed of the website and as a side effect reduces sql queries and solves n+1 problem.

0.0.4(4y ago)21133MITPHPPHP &gt;=7.2

Since Jul 25Pushed 4y ago1 watchersCompare

[ Source](https://github.com/bhushan/Stash-View)[ Packagist](https://packagist.org/packages/enlight/stash-view)[ GitHub Sponsors](https://github.com/bhushan)[ RSS](/packages/enlight-stash-view/feed)WikiDiscussions master Synced 1mo ago

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

Stash View
==========

[](#stash-view)

Stash view is a composer package for `Laravel` which caches views using Russian Doll Caching methodology.

> What is Russian Doll Caching ? It is really famous caching stratergy to cache your views into small chunks. It is quite famous in Rails community. If you are interested to know more checkout this [link](https://guides.rubyonrails.org/caching_with_rails.html).

In a nutshell, It caches your views into chunks. This article will give you more clear [idea](https://signalvnoise.com/posts/3112-how-basecamp-next-got-to-be-so-damn-fast-without-using-much-client-side-ui).

[![Russian Doll Caching PNG](https://camo.githubusercontent.com/09920391cbacec903bedc185618cd7dda7262c3cbd3915cae33a5e0378bae58e/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f33376173736574732f73766e2f3737372d7275737369616e2d646f6c6c2d63616368696e672d312e706e67)](https://camo.githubusercontent.com/09920391cbacec903bedc185618cd7dda7262c3cbd3915cae33a5e0378bae58e/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f33376173736574732f73766e2f3737372d7275737369616e2d646f6c6c2d63616368696e672d312e706e67)

Now enough about idea let's talk about how to use it
----------------------------------------------------

[](#now-enough-about-idea-lets-talk-about-how-to-use-it)

> You can also checkout example code from [here](https://github.com/bhushan/Stash-View-Example)

> Want video explaination of internals? check this [youtube](https://www.youtube.com/watch?v=iNWCCYs0qKs) link.

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

[](#installation)

### Step 1: Composer

[](#step-1-composer)

Firstly require this package using following command.

```
composer require enlight/stash-view
```

### Step 2: Service Provider (Optional)

[](#step-2-service-provider-optional)

This package supports auto discovery but if you are using `Laravel 5.4` or below you need to add `ServiceProvider` into `providers` array.

For your Laravel app, open `config/app.php` and, within the `providers` array, append:

```
Enlight\StashView\Providers\StashViewServiceProvider::class
```

This will bootstrap the package into Laravel.

### Step 3: Cache Driver

[](#step-3-cache-driver)

For this package to function properly, you must use a Laravel cache driver that supports tagging (like `Cache::tags('foo')`). Drivers such as `Memcached` and `Redis` support this feature.

Check your `.env` file, and ensure that your `CACHE_DRIVER` choice accomodates this requirement:

```
CACHE_DRIVER=redis
```

> NOTE: If your application is set to `local` environment then by default this package will use `array` caching driver to speed up development process so that you don't need to clear cache again and again while developing.

Have a look at [Laravel's cache configuration documentation](https://laravel.com/docs/7.x/cache#configuration), if you need any help.

Usage
-----

[](#usage)

### The Basics

[](#the-basics)

With the package now installed, you may use the provided `@cache` Blade directive anywhere in your views, like so:

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

        Hello World

@endcache
```

By surrounding this block of HTML with the `@cache` and `@endcache` directives, we're asking the package to cache the given HTML. Now this example is trivial, however, you can imagine a more complex view that includes various nested caches, as well as lazy-loaded relationship calls that trigger additional database queries. After the initial page load that caches the HTML fragment, each subsequent refresh will instead pull from the cache. As such, those additional database queries will never be executed. Really cool side effect of this package is it reduces you sql queries and solves n+1 problem out of the box.

Please keep in mind that, in production, this will cache the HTML fragment "forever". For local development, on the other hand, we are using `array` cache driver which stores the cache in memory and flush out when work done. That way, you may update your views and templates however you wish, without needing to worry about clearing the cache manually.

Now because your production server will cache the fragments forever, you'll want to add a step to your deployment process that clears the relevant cache.

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

### Caching Models

[](#caching-models)

While you're free to hard-code any string for the cache key, the true power of Russian-Doll caching comes into play when we use a timestamp-based approach.

Consider the following fragment:

```
@cache($post)

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

        {{ $post->body }}

@endcache
```

In this example, we're passing the `$post` object, itself, to the `@cache` directive - rather than a string. The package will then look for a `getCacheKey()` method on the model. We've already done that work for you; just have your Eloquent model use the `Enlight\StashView\Traits\Cacheable` trait, like so:

```
use Enlight\StashView\Traits\Cacheable;

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

Alternatively, you may use this trait on a parent class that each of your Eloquent models extend.

That should do it! Now, the cache key for this fragment will include the object's `Primary Key` ie `id` in most cases and `updated_at` timestamp: `App\Post/1-98765432101`.

> The key is that, because we factor the `updated_at` timestamp into the cache key, whenever you update the given post, the cache key will change. This will then, in effect, bust the cache!

#### Touching

[](#touching)

In order for this technique to work properly, it's vital that we have some mechanism to alert parent relationships (and subsequently bust parent caches) each time a model is updated. Here's a basic workflow:

1. Model is updated in the database.
2. Its `updated_at` timestamp is refreshed, triggering a new cache key for the instance.
3. The model "touches" (or pings) its parent.
4. The parent's `updated_at` timestamp, too, is updated, which busts its associated cache.
5. Only the affected fragments re-render. All other cached items remain untouched.

Luckily, Laravel offers this "touch" functionality out of the box. Consider a `Note` object that needs to alert its parent `Card` relationship each time an update occurs.

```
