ipunkt / laravel-route-cache
为 Laravel 路由发送 304 和缓存静态内容的简单缓存
Requires
- php: >=5.4.0
- illuminate/support: 4.*
This package is not auto-updated.
Last update: 2024-09-24 01:20:50 UTC
README
本包简化了在 Laravel 项目中对静态页面的缓存。它发送 304 状态头并(如果可用)不运行控制器而发送静态 HTML。配置简单直观,使用单个路由过滤器。
安装
在 composer.json 中添加以下行
"require": {
"ipunkt/laravel-route-cache": "~1.0.0"
}
运行 php artisan config:publish ipunkt/laravel-route-cache
将 'Ipunkt\LaravelRouteCache\LaravelRouteCacheServiceProvider',
添加到 app/config/app.php
中的 providers
。
配置
编辑 app/config/packages/ipunkt/laravel-route-cache/config.php
以满足您的需求。
如何作为 Laravel 路由过滤器使用
将 'before' => 'cache.before'
添加到您想缓存的每个路由。 路由必须是规范化的!,因为过滤器通过从请求中提取 url()
来缓存。 (也许我会将其更改为 fullUrl()
)
要使缓存过时,只需触发事件 entity.modified
并附加修改的资源请求或 URL。您可以通过将配置中设置的 GET 参数附加到 URL 上作为 cachebuster
(默认为 "renew-cache")来在浏览器上强制执行此操作。
如果 infoheader
配置设置不是 false
,则库向响应添加一个标题,以显示内容是从缓存还是控制器中获取的。
其他用途
类 Ipunkt\LaravelRouteCache\RouteCache
不需要过滤器即可直接使用。您可以在控制器中使用它。
// get RouteCache-Instance
$routecache = \App::make('routecache');
$routecache->setEntityFromRequest($request);
// or
$routecache->setEntityFromUrl($url);
// to remove the cache
$routecache->removeCache()
// to check if the client has a valid (same ETag) Cache
$routecache->checkClientHasValidCache()
// to save a Response to the Cache
$routecache->setCacheFromResponse(Response $response)
// to save string-content to the Cache
$routecache->setCacheFromContent($content)
// to get a saved Response from the Cache
$routecache->getResponseFromCache()
// to get a saved string from the Cache
$routecache->getContentFromCache()
// you can get the ETag for the entity (but you don't need it)
$routecache->getETag()
// you can even set a custom ETag if you like
$routecache->getETag($value)
开放 TODO
请随意分支并推送更改
- 记住所有已缓存的实体,并允许通过命令清除它们
- 添加测试用例
- 缓存内容的 MIME 类型,并使用该类型进行响应
- 处理 POST、PUT、UPDATE 和 DELETE 请求的例程