esemve / vanillacache
适用于 Laravel 5 的强大缓存引擎
Requires
- php: >=5.6
This package is auto-updated.
Last update: 2024-09-25 23:00:07 UTC
README
Vanilla Cache 是 Laravel 5.X 的缓存系统。它是一个响应缓存系统,因此您可以缓存完整的 HTML 响应,并在下次 无需启动 Laravel 的情况下提供服务。
您可以在共享 Web 服务器中使用它。使用此缓存时的请求时间最多为 10-20 毫秒。
这是一个开发版本!请勿将其用于生产项目!
安装
composer require esemve/vanillacache –dev
添加到 providers
Esemve\VanillaCache\CacheServiceProvider::class
添加到 Facades
'Vanilla' => Esemve\VanillaCache\Facades\Vanilla::class
之后
composer dump-autoload php artisan vendor:publish
现在您可以使用 Laravel 创建缓存了!
下一步是设置 CacheServer。它将在 Laravel 加载之前提供缓存。将以下内容添加到 composer.json 的 "autoload" 部分:
"files": ["config/vanilla.php","vendor/esemve/vanillacache/vanilla/CacheServer.php"],
为什么 config/vanilla.php 是如此奇怪?
因为它在 Laravel 之前加载,所以您不能在此文件中使用任何 Laravel 指定的函数!
如何使用?
use \Vanilla; … public function index(RoomRepository $roomRepository) { $rooms = $roomRepository->getAll(); return Vanilla::Cache(View::make('cinema.index',[ 'rooms' => $rooms ]),10); }
Vanilla::Cache($view,$sec)
保存实际 URL 的视图内容(包括 GET 参数)10 秒。如果不到 10 秒有用户打开此 URL,CacheServer 将在无需启动 Laravel 的情况下提供缓存。
引擎
VanillaCache 包含默认的文件引擎。您可以创建一个引擎(例如:mysql,redis 等)。
如何创建一个引擎?
config/vanilla.php
'file' => [ 'laravel' => 'Esemve\\VanillaCache\\Engines\\FileEngine', 'vanilla' => __DIR__.'/../vendor/esemve/vanillacache/vanilla/Engines/FileEngine.php', 'config' => [ 'storage_folder' => realpath(__DIR__.'/../storage/').'/vanilla/' ] ]
“file”: 引擎名称
在“laravel” => 'xxx'部分 中为您的缓存引擎填充命名空间。此引擎在 Laravel 中是活动的。
在“vanilla” => 'xxx'部分 中可以设置在 Laravel 之前加载的引擎文件。它必须实现 Esemve\VanillaCache\Interfaces\EngineInterface。它将为 CacheServer 提供缓存内容。
“config”部分 将发送到您的引擎。
动态内容/块(类似于 SSI)
所有页面都包含动态或重复的元素,例如右侧、菜单等。您可以从这些元素生成 HTML,并将其包含到缓存中。
为缓存生成 HTML
Vanilla::storeHtml(“rightSide”,view(“_partials.rightSide”))
之后,您可以在页面中使用一个 <##html:##> 标签。
<html> ... <body> … <!-- <##html:rightSide##> --> ... </body> </html>
CacheServer 和 Laravel 将自动将此标签(如果有注释标签)替换为存储的 HTML 内容。