gchaincl / laravel-fragment-caching
此包已被废弃,不再维护。未建议替代包。
片段缓存支持助手
v0.0.1
2014-05-11 12:45 UTC
Requires
- illuminate/cache: 4.x
This package is not auto-updated.
Last update: 2021-02-05 21:02:17 UTC
README
该项目不再受支持,您可以尝试使用 matryoshka。
laravel-fragment-caching
添加片段缓存支持助手。 博客文章
安装
运行: composer require gchaincl/laravel-fragment-caching:dev-master 或
- 添加:
"require": { "gchaincl/laravel-fragment-caching": "dev-master" },到 composer.json - 运行:
composer install - 添加以下内容到您的
app/config/app.php
$providers => array( ... 'Gchaincl\LaravelFragmentCaching\ViewServiceProvider', )
使用
在您的视图中
<ul> @foreach ($posts as $post) @cache("post" . $post->id) <li> {{ link_to_route('post.show', $post->title, $post->id) }} ({{ $post->user->username }})</li> @endcache @endforeach </ul>
第一次加载该视图时,框架将运行3次查询
select * from "posts" select * from "users" where "users"."id" = '5' limit 1 select * from "users" where "users"."id" = '5' limit 1
第二次,由于片段已被缓存,因此只需查询一次
select * from "posts"
条件缓存
在您不总是想缓存一个块的情况下,可以使用 @cacheif($condition, $cacheId)
{{-- Only use the cache for guests, admins will always get content rendered from the template --}}
@cacheif( Auth::guest(), "post" . $post->id)
<li> {{ link_to_route('post.show', $post->title, $post->id) }} (@if (Auth::guest()) {{ $post->user->username }} @else {{ $post->user->email }} @endif)</li>
@endcacheif
提示
要更新模型更改后的视图渲染,您应该使您的片段过期
// app/model/Post.php class Post extends Eloquent { public static function boot() { parent::boot(); static::updated(function($model) { Cache::forget("post" . $model->id); }); } }