简体   繁体   English

如何将 Laravel 视图转换为静态 HTML 并从缓存中提供它们?

[英]How to convert Laravel views to static HTML and serve them from cache?

I would like to setup functionality to transform the views in Laravel 5.3 to static HTML and serve it to visitors untill specified from a CMS call.我想设置功能以将 Laravel 5.3 中的视图转换为静态 HTML 并将其提供给访问者,直到从 CMS 调用中指定。 I also like to use HTTP headers to cache the pages on a visitors computer.我还喜欢使用 HTTP 标头来缓存访问者计算机上的页面。

  1. How can i get views to be served as static HTML with route caching enabled?如何在启用路由缓存的情况下将视图作为静态 HTML 提供?
  2. How can i serve the static HTML views from the users cache using HTTP headers?如何使用 HTTP 标头从用户缓存中提供静态 HTML 视图?
  3. Are there any other caveats or performance boosting tricks i can/should utilise?我可以/应该使用其他任何警告或性能提升技巧吗?

Here I will answer the File based cache with the code.在这里,我将用代码回答File based cache Which is not exactly the HTML cache, but performs well.这不完全是HTML缓存,但性能良好。

Look at my Laravel Installation /var/www/ea (This is my Laravel Installation directory)查看我的 Laravel 安装 /var/www/ea(这是我的 Laravel 安装目录)

在存储/框架/缓存中,您将找到缓存的文件

Step 1: How to create cache第 1 步:如何创建缓存

Step 2: This step comes before step 1, which is configure cache to file cache.第 2 步:此步骤在第 1 步之前,即configure缓存到file缓存。 Open the config/cache.php and do check two things打开config/cache.php并检查两件事

'default' => env('CACHE_DRIVER', 'file'), //It means your cache default driver is File based. 'default' => env('CACHE_DRIVER', 'file'), //这意味着您的缓存默认驱动程序是基于文件的。

Then然后

'path' => storage_path('framework/cache'), //This is the path My screenshot is showing above. 'path' => storage_path('framework/cache'), //这是我上面截图的路径。

Step 3: Lets write the code to create cache.第 3 步:让我们编写代码来创建缓存。 Open your Controller method and write following打开您的控制器方法并编写以下内容

\Cache::put( 'cachekey', 'Hello I am Abdul', 1 ); // 1- minute

laravel documentation for cache缓存的 Laravel 文档

Step 4: Check for any new directory created inside framework/cache/第 4 步:检查在framework/cache/创建的任何新目录

That's your cache file.那是你的缓存文件。

// Following code check if the cache key already exists before creating, 
// otherwise create cache file
if (Cache::has('key')){
    Cache::get('key');
} else {
    Cache::put('key', $values, 10);
}

There is a simple Package for that which you can do.有一个简单的包可以做。 It will easily cache responses as static files on disk for lightning fast page loads.它将轻松地将响应缓存为磁盘上的静态文件,以实现闪电般的快速页面加载。

Here is the Page Cache Package for Laravel这是Laravel页面缓存包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM