KodeInfo | Learning resources for web and mobile development

CACHING VIEWS

September 4th, 2014 23:05:31 by Imran Iqbal Comments(0) - Views(6223)

We have heard about caching queries , caching views but what if we want to cache a part of our view , a section or a div .  Lets create a view we will use a template for our view to view click here .

content.blade.php

<html>
 
<head>
 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
 
 <!-- Optional theme -->
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
 
 <!-- Latest compiled and minified JavaScript -->
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
 <div class="col-md-12">
 <h1>Alice in Wonderland, part dos</h1>
 
 <p>'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
 both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
 the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
 'Yes, we went to school in the sea, though you mayn't believe it'
 'I never said I didn't!' interrupted Alice.
 'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
 both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
 the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
 'Yes, we went to school in the sea, though you mayn't believe it'
 'I never said I didn't!' interrupted Alice.
 'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
 both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
 the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
 'Yes, we went to school in the sea, though you mayn't believe it'
 'I never said I didn't!' interrupted Alice.
 'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
 both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
 the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
 'Yes, we went to school in the sea, though you mayn't believe it'
 'I never said I didn't!' interrupted Alice.
 'You did,' said the Mock Turtle.</p>
 
 <div>
 <span class="badge">Posted 2012-08-02 20:47:04</span>
 
 <div class="pull-right"><span class="label label-default">alice</span> <span class="label label-primary">story</span>
 <span class="label label-success">blog</span> <span class="label label-info">personal</span> <span
 class="label label-warning">Warning</span>
 <span class="label label-danger">Danger</span></div>
 </div>
 
 </div>
</div>
</body>
</html>

 

Above code shows static content lets make it dynamic . I have create required migrations and seeding and inserted that data into table lets fetch and display that

routes.php

Route::get('/content/{id}', function($id){
 $content=Content::find($id);
 $content->tags=explode(',',$content->tags);
 
 return View::make('content',compact('content'));
});

 

Content.php

/**
 * Content
 *
 */
class Content extends Eloquent{
 
 protected $table = 'content';
 
}

After changing our static content to dynamic it will look like below

<html>
 
<head>
 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
 
 <!-- Optional theme -->
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
 
 <!-- Latest compiled and minified JavaScript -->
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
 <div class="col-md-12">
 <h1>{{$content->header}}</h1>
 
 <p>{{$content->content}}</p>
 
 <div>
 <span class="badge">Posted {{$content->created_at}}</span>
 
 @foreach($content->tags as $tag)
 <div class="pull-right">
 <span class="label label-warning">{{$tag}}</span>
 </div>
 @endforeach
 </div>
 
 </div>
</div>
</body>
</html>

Lets create our Cache class now . Create a new folder inside your app and name it Cache

Cache/PageCache.php

namespace Cache;
 
use URL;
use Cache;
use Closure;
 
class PageCache
{
 public static function cache($key, $expires = 1, Closure $closure)
 {
 $key = $key . URL::full();
 
 if ( Cache::has( $key ) )
 {
 $content = Cache::get( $key );
 }
 else
 {
 ob_start();
 $closure();
 $content = ob_get_contents();
 ob_end_clean();
 Cache::put( $key, $content, $expires );
 }
 
 echo $content;
 }
}

I dont like calling Cache\PageCache in my view so lets add an alias to our aliases array in app.php

'PageCache' => 'Cache\PageCache'

We have to tell composer to load our namespace so add app\Cache\ to autoload classmap

"app/Cache",

Last thing is to use our new class in our view don’t forgot to pass variable to function use as parameters

<?php PageCache::cache('content',5,function() use ( $content ) { ?>
<div class="col-md-12">
<h1>{{$content->header}}</h1>
 
<p>{{$content->content}}</p>
 
<div>
<span class="badge">Posted {{$content->created_at}}</span>
 
@foreach($content->tags as $tag)
<div class="pull-right">
<span class="label label-warning">{{$tag}}</span>
</div>
@endforeach
</div>
</div>
<?php }); ?>

Now our above section of view is cached for 5 mins try changing data in db and refresh page it wont change the values since our section of that view is cached . Enjoy .

Credits to Vincent for showing how to use ob_start , ob_get_contents and ob_end_clean .

Thanks

 

KodeInfo

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus