linslin / laravel-coffee-cache
基于文件的Laravel缓存工具。这个缓存钩子会在composer自动加载和Laravel启动之前执行。这将使您的应用程序达到光速。
Requires
- ext-mbstring: *
- ext-zlib: *
- mobiledetect/mobiledetectlib: ^2.8
- dev-master
- 1.24.3
- 1.24.2
- 1.24.1
- 1.24.0
- 1.23.1
- 1.23.0
- 1.22.2
- 1.22.1
- 1.22.0
- 1.21.2
- 1.21.1
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.3
- 1.15.2
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-07 11:57:13 UTC
README
为Laravel 4.x、5.x、6.x、7.x、8.x、9.x和10.x提供基于商店的视图缓存。这个缓存钩子会在composer自动加载和Laravel启动之前执行。这将使您的应用程序达到光速。默认情况下,所有GET请求都将被缓存。
这是一个咖啡缓存。您可以用更多的咖啡来代替优化应用程序或服务器环境的时间。Mokka Mokka!
为什么和什么时候应该使用laravel-coffee-cache?
多年来,Laravel一直在不断壮大。今天,Laravel是一个非常棒的框架,它以最佳方式帮助您加快软件开发和编程。另一方面,Laravel在处理请求时较慢,每个请求都会消耗大量内存,即使您使用了视图或数据库缓存。Laravel的启动也需要一些时间,并且消耗大量内存。例如,如果您想渲染一个没有动态数据的“关于我们/免责声明”页面。那么,为什么您要启动Laravel及其所有依赖关系,只是为了返回一个简单的HTML页面呢?
☕ laravel-coffee-cache 允许您在为特定路由(请求URI)生成缓存文件后,完全利用Laravel和composer自动加载。这样,每个请求都会消耗大量硬件资源(CPU、RAM、硬盘)。这将使您的应用程序达到光速。
与现有的Laravel缓存系统的区别是:您不需要基于memcached的DB缓存,甚至不需要在Laravel中间件中放置基于视图文件的缓存。 提示:将您的DB缓存与 laravel-coffee-cache 结合使用是个不错的选择。即使 laravel-coffee-cache 在前台运行,您也可以使用您的DB缓存。
您将能够创建高流量的Web应用程序,并节省大量硬件资源(这也会节省金钱),使用 ☕ laravel-coffee-cache。如果您的网站运行速度太慢或服务器/服务器容量(CPU、内存)已满载,它可以帮助您优化应用程序。试试看吧。
安装
composer require --prefer-dist linslin/laravel-coffee-cache "*"
注册外观
如果您想使用外观来处理缓存文件,请将以下内容添加到您的 config/app.php
中的外观数组:
'CoffeeCache' => linslin\CoffeeCache\Facades\CoffeeCache::class,
示例 ./config/coffeeCache.php
配置文件
return [
/**
* Cache driver: 'file' or 'redis'
*/
'driver' => 'file',
/*
* Redis connection
*/
'redis' => [
'host' => 'localhost',
'port' => 6000,
'password' => '', //leave empty if no password is given
'timeout' => 0.5
],
];
API 文档
初始化实例
应放置在您的 app/public/index.php
文件中。
$coffeeCache = new CoffeeCache(__DIR__);
配置启用缓存的主机 [可选]
匹配应缓存的主机。默认:缓存所有域名
$coffeeCache->enabledHosts = [
'www.production.com',
'subdomain.production.com',
];
配置启用会话缓存的主机 [可选]
匹配仅当设置cookie cached=1时才缓存的主机。默认:缓存所有域名
$coffeeCache->enabledCacheHostsWithSession = [
'www.production.com',
'subdomain.production.com',
];
配置要排除的查询参数 [可选]
匹配的查询参数将从请求URI中排除。具有排除查询参数的请求URI将被视为未设置参数。默认:不排除任何查询参数
$coffeeCache->excludeQueryParam = [
'aQueryParameter',
];
启用/禁用整个缓存 [可选|默认:true]
标志以轻松禁用缓存。
$coffeeCache->cacheEnabled = true;
配置缓存驱动程序(文件、redis) [可选|默认:‘文件’|‘redis’]
$coffeeCache->cacheDriver = 'redis';
$coffeeCache->redisConnection = [
'host' => 'localhost',
'port' => 6000,
'password' => '',
'timeout' => 0.5
];
配置要缓存的HTTP状态码 [可选]
要缓存的HTTP状态码列表。默认:仅缓存“200”。
$coffeeCache->enabledHttpStatusCodes = [
'200',
'202',
];
排除要缓存的URL模式 [可选]
要排除的URL的URL模式。此示例将排除URL中包含“/admin”的URL。
$coffeeCache->excludeUrls = [
'/admin',
];
启用压缩缓存数据[可选]
删除标签后的空白字符(除了空格),删除标签前的空白字符(除了空格)。缩短多个空白字符序列。移除HTML注释
$coffeeCache->minifyCacheFile = true;
启用cookie处理缓存[可选]
只有当存在名为"cached"的cookie并且其值为"1"时,缓存才会工作。这是在运行coffeeCache时处理用户会话。这允许您为登录用户启用/禁用缓存。如果用户未登录,创建cached=1的cookie。如果用户已登录,创建cached=0的cookie。
$coffeeCache->cookieHandledCacheEnabled = true;
启用压缩[可选]
启用gzip压缩缓存数据。默认值是false
。
$coffeeCache->gzipEnabled = true;
过滤要压缩的内容类型。[可选]
将被忽略且不压缩的响应内容类型。
$coffeeCache->minifyIgnoreContentTypes = [
'image/png',
'image/gif',
'image/jpg',
'image/jpeg',
];
全局替换[可选]
将替换我们缓存文件中的某些字符串标记。这允许您全局操作缓存数据。您可以解析字符串或文件路径。文件内容将替换标记字符串。
$coffeeCache->globalReplacements = [
[
'type' => 'string',
'marker' => '###marker1####',
'value' => 'hallo welt'
], [
'type' => 'file',
'marker' => '###marker2####',
'filePath' => __DIR__.'/../public/test.txt'
], [
'type' => 'file',
'marker' => '###start_marker####',
'markerEnd' => '###end_marker####',
'filePath' => __DIR__.'/../public/test.txt'
]
];
外观API文档
删除所有缓存文件
CoffeeCache::clearCache();
手动删除缓存文件
CoffeeCache::clearCacheFile(route('route.name', [], false));
检查缓存文件是否存在
CoffeeCache::cacheFileExists(route('route.name', [], false));
获取创建日期(仅文件驱动程序)
CoffeeCache::getCacheFileCreatedDate(route('route.name', [], false));
示例:手动删除特定文件
例如,在控制器内部 - 示例
<?php
namespace App\Http\Controllers\Admin\Shop;
use App\Models\Shop;
use Illuminate\Http\Request;
use linslin\CoffeeCache\Facades\CoffeeCache;
/**
* Class EntryController
* @package App\Http\Controllers\Admin
*/
class EntryController extends ShopBaseController
{
/**
* @param Request $request
* @param Shop $shop
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function update(Request $request, Shop $shop)
{
//manually delete cache file for a route (if exists)
CoffeeCache::clearCacheFile(route('shop.show', ['shop' => $shop->slug], false));
return view('admin.shop.form',[
'shop' => $shop,
]);
}
}
设置和使用
-
在
app/storage/
中创建一个名为coffeeCache
的缓存文件夹。因此您创建了这个文件夹:/storage/coffeeCache
。 -
在
/storage/coffeeCache
中添加一个.gitignore
文件,并将以下内容放入其中。 -
编辑您的
app/public/index.php
并在PHP脚本顶部添加以下行require_once './../vendor/linslin/laravel-coffee-cache/CoffeeCache.php'; $coffeeCache = new CoffeeCache(__DIR__); $coffeeCache->cacheTime = 60 * 60 * 24 * 1; //Default is one day. 60 * 60 * 24 * 1 = 1 day $coffeeCache->minifyCacheFile = true; $coffeeCache->enabledHosts = [ 'www.production.com', 'subdomain.production.com', ]; // optional, leave this array empty if you want to cache all domains. $coffeeCache->enabledHttpStatusCodes = [ '200', '202', ]; // list of HTTP-Status codes which should be cached. $coffeeCache->excludeUrls = [ '/admin', ]; // URL pattern of URLs which should not be cache. This example will exclude URLS which have "/admin" somewhere in the URL. $coffeeCache->handle();
将
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
下的所有代码行替换为以下行/** @var Illuminate\Http\Response $response */ $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); if ($coffeeCache->isCacheAble()) { $coffeeCache->httpStatusCode = $response->status(); $coffeeCache->contentType = $response->headers->get('content-type'); $response->sendHeaders(); echo $response->content(); } else { $response->send(); } $kernel->terminate($request, $response); $coffeeCache->finalize();
您也可以将您的编辑与这个index.php示例进行比较。
最终,您的
app/public/index.php
应该看起来像这样<?php require_once './../vendor/linslin/laravel-coffee-cache/CoffeeCache.php'; $coffeeCache = new CoffeeCache(__DIR__); $coffeeCache->cacheTime = 60 * 60 * 24 * 1; //Default is one day. 60 * 60 * 24 * 1 = 1 day $coffeeCache->minifyCacheFile = true; $coffeeCache->enabledHosts = [ 'www.production.com', 'subdomain.production.com', ]; // optional, leave this array empty if you want to cache all domains. $coffeeCache->enabledHttpStatusCodes = [ '200', '202', ]; // list of HTTP-Status codes which should be cached. $coffeeCache->excludeUrls = [ '/admin', ]; // URL pattern of URLs which should not be cache. This example will exclude URLS which have "/admin" somewhere in the URL. $coffeeCache->handle(); require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; $kernel = $app->make('Illuminate\Contracts\Http\Kernel'); /** @var Illuminate\Http\Response $response */ $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); if ($coffeeCache->isCacheAble()) { $coffeeCache->httpStatusCode = $response->status(); $coffeeCache->contentType = $response->headers->get('content-type'); $response->sendHeaders(); echo $response->content(); } else { $response->send(); } $kernel->terminate($request, $response); $coffeeCache->finalize();
-
laravel-coffee-cache现在应该开始缓存您的GET请求并在
app/storage/coffeeCache
中创建缓存文件。 -
在
./config/coffeeCache.php
中创建一个配置文件。此存储库中可以找到示例配置。
变更日志
1.24.3
SpaceLeftOnDevice
现在按预期工作。
1.24.2
SpaceLeftOnDevice
语句已移至写入磁盘部分。
1.24.1
- 修复了空间剩余设备语句。
1.24.0
- 将允许使用的最大磁盘空间从95%缩减到90%。
1.23.1
- 修复了
hostDisabled[]
处理。
1.23.0
- 添加了
hostDisabled[]
配置,以防止特定域名缓存。
1.22.2
- 修复了
excludeQueryParam()
未执行其工作的问题。
1.22.1
- 修复了压缩空白字符问题。
1.22.0
- 添加了选项以在压缩的缓存文件版本中保留HTML注释。
<!--noremove some comment -->
。
1.21.2
- 对
globalReplacements
的第二次小修复,这将保留替换后的模板中的标记。
1.21.1
- 对
globalReplacements
的小修复,这将保留替换后的模板中的标记。
1.21.0
- 使
globalReplacements
标记可选,与结束和开始标记一起工作。
1.20.0
- 添加了
globalReplacements
选项。
1.19.0
- 添加了redis连接关闭。
1.18.0
- 添加了用于检测移动设备的移动检测库。
1.17.0
- 修复了平板设备的移动检测。
- 在redis驱动程序中添加了过期时间递增。每次击中键时,过期时间将增加
$coffeeCache->cacheTime
。
1.16.0
- 将检测到的用户代理添加到响应头状态中。
1.15.3
- 修复了用于移动缓存数据的清除缓存外观函数。
1.15.2
- 只有在将缓存驱动程序设置为"file"时,才会执行
spaceLeftOnDevice
函数。
1.15.1
- 修复了
spaceLeftOnDevice
权限问题。
1.15.0
- 添加了
gzipEnabled
选项。
1.14.0
- 添加了
enabledCacheHostsWithSession
选项。 - 增加了
excludeQueryParam
选项。 - 非常感谢 Marcos (@delacruzsippel)
1.13.1
- 修复了移动/桌面分割的 Facade。
1.13.0
- 增加了移动/桌面检测和缓存文件分割。
- 0 字节/空响应将不再被缓存。空响应体将被忽略。
- 修复了 minifyCacheFile 上的正则表达式
'/(\s)+/s'
以处理大量空白字符。
1.12.2
- 现在:对一些全局变量添加了 isset 验证。
1.12.1
- 对一些全局变量添加了 isset 验证。
1.12.0
- 向 Laravel Facade 添加了一个文件和 Redis 驱动。
- 添加了
$coffeeCache->cookieHandledCacheEnabled
选项,通过 cookies 来处理缓存。这可以用来禁用用户会话的缓存。
1.11.1
- 修复了 minify 的内容类型忽略问题。
1.11.0
- 添加了 "redis" 驱动。CoffeeCache 现在可以通过 'file' 和 'redis' 驱动工作。
- 添加了 "cacheEnabled",以便轻松启用和禁用 coffeeCache。
1.10.0
- 为 minify 添加了选项以忽略要压缩的特殊内容类型。
1.9.0
- 为缓存文件添加了 minify 选项。
1.8.3
- 语法修复
1.8.2
- 确保
spaceLeftOnDevice()
使用路由体积来测量设备上的剩余空间。 - 清除或删除缓存功能现在也将删除空缓存目录。
1.8.1
- 修复了 Facade 函数。
1.8.0
- 添加了
$coffeeCache->cacheEnabled
标志,通过软件开关禁用整个缓存。默认值为true
。 - 添加了
$coffeeCache->diskSpaceAllowedToUse
磁盘空间百分比,用于浮点数,当 coffeeCache 写入缓存文件时将停止。默认值为95.00
。 - 为缓存文件添加了文件夹处理。
1.7.2
- 修复了在文件系统上写入空文件的问题。
1.7.1
- 当缓存文件不可写时捕获异常。
1.7.0
- 添加了 cookie 处理器来在请求中禁用缓存:
setcookie("disable-cache", "1", time() + (3600*24), "/", $request->getHost());
。如果 cookie 中存在disable-cache
,则请求将不会响应缓存文件,并且不会为此请求创建缓存文件。
1.6.0
- 添加了删除所有缓存文件的辅助函数(清除缓存)。
1.5.0
- 向 Facade 添加了更多辅助函数。
1.4.0
- 添加了 Facade 以控制 Laravel 应用程序内的缓存文件。例如,删除缓存文件。
1.3.0
- 添加了排除 URL 模式从缓存中的选项。例如,包含 "/admin" 的 URL。
1.2.0
- 添加了启用特定 HTTP-Status 代码缓存的选项。默认为 "200 Ok"。
1.1.0
- 添加了启用特定域名缓存的选项。如果设置了 "HTTP_X_FORWARDED_HOST",则这也适用于反向代理。
1.0.0
- 第一个稳定版本
致谢
感谢 robre21 和 delacruzsippel!