├── .gitignore
├── LICENSE
├── README.md
├── composer.json
└── src
├── Gchaincl
└── LaravelFragmentCaching
│ ├── Factory.php
│ └── ViewServiceProvider.php
└── helper.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 | composer.lock
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Gustavo Chaín
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Not supported [](http://unmaintained.tech/)
2 | =============
3 |
4 | This project is not supported anymore, you can try [matryoshka](https://github.com/laracasts/matryoshka) instead.
5 |
6 | laravel-fragment-caching
7 | ========================
8 |
9 | Add a Fragment caching support helper. [Blog post](http://gustaf.espontanea.io/blog/2014/02/09/laravel-fragment-caching)
10 |
11 | Installation
12 | ============
13 |
14 | Run: `composer require gchaincl/laravel-fragment-caching:dev-master`
15 | or
16 | * add: `"require": { "gchaincl/laravel-fragment-caching": "dev-master" }, `to composer.json
17 | * run: `composer install`
18 | * add: The following to your `app/config/app.php`
19 | ```php
20 | $providers => array(
21 | ...
22 | 'Gchaincl\LaravelFragmentCaching\ViewServiceProvider',
23 | )
24 | ```
25 |
26 |
27 | Usage
28 | =====
29 |
30 | In your view:
31 | ```php
32 |
33 | @foreach ($posts as $post)
34 |
35 | @cache("post" . $post->id)
36 | - {{ link_to_route('post.show', $post->title, $post->id) }} ({{ $post->user->username }})
37 | @endcache
38 |
39 | @endforeach
40 |
41 | ```
42 |
43 | First time we load that view, Framework will run 3 queries:
44 | ```sql
45 | select * from "posts"
46 | select * from "users" where "users"."id" = '5' limit 1
47 | select * from "users" where "users"."id" = '5' limit 1
48 | ```
49 |
50 | Second time, as fragments are already cached, there will be just one query:
51 | ```sql
52 | select * from "posts"
53 | ```
54 |
55 | Conditional caching
56 | ===================
57 |
58 | In situations where you don't always want to cache a block you can use `@cacheif($condition, $cacheId)`
59 |
60 | ```php
61 | {{-- Only use the cache for guests, admins will always get content rendered from the template --}}
62 | @cacheif( Auth::guest(), "post" . $post->id)
63 | {{ link_to_route('post.show', $post->title, $post->id) }} (@if (Auth::guest()) {{ $post->user->username }} @else {{ $post->user->email }} @endif)
64 | @endcacheif
65 | ```
66 |
67 | Tip
68 | ---
69 |
70 | To update view rendering on model changes, you should expire your fragments:
71 |
72 | ```php
73 | // app/model/Post.php
74 |
75 | class Post extends Eloquent {
76 |
77 | public static function boot() {
78 | parent::boot();
79 | static::updated(function($model) {
80 | Cache::forget("post" . $model->id);
81 | });
82 | }
83 | }
84 | ```
85 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "gchaincl/laravel-fragment-caching",
3 | "description": "Fragment Caching support helper",
4 | "license": "MIT",
5 | "keywords": ["cache", "fragment", "helper", "laravel"],
6 | "authors": [
7 | {
8 | "name": "Gustaf Shin",
9 | "email": "gustaf@espontanea.io"
10 | }
11 | ],
12 | "require": {
13 | "illuminate/cache":"~4.2.0"
14 | },
15 | "autoload": {
16 | "psr-0": {
17 | "Gchaincl\\LaravelFragmentCaching": "src/"
18 | }
19 | },
20 |
21 | "minimum-stability": "dev"
22 | }
23 |
--------------------------------------------------------------------------------
/src/Gchaincl/LaravelFragmentCaching/Factory.php:
--------------------------------------------------------------------------------
1 | getContainer()['cache'];
11 | $log = $this->getContainer()['log'];
12 |
13 | $content = $cache->get($key);
14 | if ( ! $content ) {
15 | ob_start();
16 |
17 | $closure();
18 | $content = ob_get_contents();
19 | ob_end_clean();
20 | $cache->forever($key, $content);
21 | $log->debug('writing cache', [$key]);
22 | } else {
23 | $log->debug('reading cache', [$key]);
24 | }
25 |
26 | return $content;
27 | }
28 |
29 | public function cache($key, \Closure $closure)
30 | {
31 | return $this->cacheif(true, $key, $closure);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/Gchaincl/LaravelFragmentCaching/ViewServiceProvider.php:
--------------------------------------------------------------------------------
1 | registerEnvironment();
11 | $this->registerBladeExtensions();
12 | }
13 |
14 | public function registerEnvironment()
15 | {
16 | $this->app->bindShared('view', function($app) {
17 | $resolver = $app['view.engine.resolver'];
18 | $finder = $app['view.finder'];
19 | $env = new Factory($resolver, $finder, $app['events']);
20 |
21 | $env->setContainer($app);
22 | $env->share('app', $app);
23 |
24 | return $env;
25 | });
26 | }
27 |
28 | protected function registerBladeExtensions()
29 | {
30 | $blade = $this->app['view']
31 | ->getEngineResolver()
32 | ->resolve('blade')
33 | ->getCompiler();
34 |
35 | $blade->extend(function($view, $compiler) {
36 | $pattern = $compiler->createMatcher('cache');
37 | return preg_replace($pattern, '$1' . $this->cacheTemplate(), $view);
38 | });
39 |
40 | $blade->extend(function($view, $compiler) {
41 | $pattern = $compiler->createOpenMatcher('cacheif');
42 | return preg_replace($pattern, '$1' . $this->cacheIfTemplate(), $view);
43 | });
44 |
45 | $blade->extend(function($view, $compiler) {
46 | $pattern = $compiler->createPlainMatcher('endcache(if)?');
47 | return preg_replace($pattern, '$1', $view);
48 | });
49 | }
50 |
51 | private function cacheTemplate()
52 | {
53 | return <<<'EOF'
54 | cache($2, function() use($__fc_vars) {
57 | extract($__fc_vars);
58 |
59 | // Cached Content goes below this
60 |
61 | ?>
62 | EOF;
63 | }
64 |
65 | private function cacheIfTemplate()
66 | {
67 | return <<<'EOF'
68 | cacheif$2, function() use($__fc_vars) {
71 | extract($__fc_vars);
72 |
73 | // Cached Content goes below this
74 |
75 | ?>
76 | EOF;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/helper.php:
--------------------------------------------------------------------------------
1 |