nodes / cache
简化Laravel缓存的控制和管理工作
Requires
- laravel/framework: >=5.1.0 <6.15.0
- nodes/core: ^1.0
This package is auto-updated.
Last update: 2024-05-17 19:28:32 UTC
README
为Laravel提供处理缓存的简单集成 - 以结构化方式。
缓存键通常会累积,最终成为字符串中的3个以上参数。或者对URL进行缓存会导致不同的查询参数顺序产生不同的缓存版本。查询参数通常被处理为哈希表或字典,它们没有固定的顺序。
最糟糕的是,你会在项目中到处都是碎片化的缓存设置。
此包将使您的生活变得更加容易
📝 简介
我们找到了一种更灵活、更有结构的方式来管理缓存及其生命周期。我们还创建了一些辅助方法,使整个过程更加简单和出色。
📦 安装
要安装此包,您需要
- Laravel 5.1+
- PHP 5.5.9+
然后您必须修改您的composer.json
文件并运行composer update
,以便将包的最新版本包含到您的项目中。
"require": { "nodes/cache": "^1.0" }
或者您可以在终端中运行composer require命令。
composer require nodes/cache:^1.0
🔧 设置
在config/app.php
中设置服务提供者
Nodes\Cache\ServiceProvider::class
在config/app.php
中设置别名
'NodesCache' => Nodes\Cache\Support\Facades\Cache::class
发布配置文件
php artisan vendor:publish --provider="Nodes\Cache\ServiceProvider"
如果您想覆盖任何现有的配置文件,请使用--force
参数
php artisan vendor:publish --provider="Nodes\Cache\ServiceProvider" --force
⚙ 使用方法
全局方法
function cache_remember($cacheGroupKey, array $params = [], $data, $tags = null, \Closure $closure = null)
function cache_put($cacheGroupKey, array $params = [], $data, $tags = null)
function cache_get($cacheGroupKey, array $params = [], $tags = null)
function cache_forget($cacheGroupKey, array $params = [], $tags = null)
function cache_flush($tags)
function cache_wipe()
门面方法
\NodesCache::remember($cacheGroupKey, array $params = [], $data, $tags = null, \Closure $closure = null)
\NodesCache::put($cacheGroupKey, array $params = [], $data, $tags = null)
\NodesCache::get($cacheGroupKey, array $params = [], $tags = null)
\NodesCache::forget($cacheGroupKey, array $params = [], $tags = null)
\NodesCache::flush($tags)
\NodesCache::wipe()
示例
首先重要的事情是为您的新缓存组创建配置,配置应该在/config/nodes/cache.php中(否则您忘记执行vendor:publish)
'groups' => [
/*
|--------------------------------------------------------------------------
| Project
|--------------------------------------------------------------------------
|
| Cache settings used by your project.
|
*/
'geographic' => [
'continent.bySlug' => [
'active' => true,
'key' => 'geographic-continent-by-slug',
'lifetime' => 3600,
],
...
将给出一个$cacheGroup geographic.continent.bySlug
请记住使键唯一以避免冲突
记住
记住是同时获取和存储到缓存的方法,95%的情况下这是正确的选择
return cache_remember('geographic.continent.bySlug', ['slug' => $slug], $tags, function () use ($slug) {
// Look up in db
$continent = $this->where('slug', $slug)->first();
if (!$continent) {
throw new EntityNotFoundException(sprintf('Could not find continent with slug [%s]', $slug));
}
return $continent;
});
这种方式我们首先在缓存中查找,如果找到数据则返回。如果没有找到数据,则运行闭包以在数据库中查找数据并返回。然后在闭包中返回数据并将其缓存。
存储
只将数据存储在缓存中
// Look up in db
$continent = $this->where('slug', $slug)->first();
if (!$continent) {
throw new EntityNotFoundException(sprintf('Could not find continent with slug [%s]', $slug));
}
// Put in cache
$success = cache_put('geographic.continent.bySlug', ['slug' => $slug], $continent)
$success
是一个布尔值
获取
只从缓存中获取数据
// Get from cache
$continent cache_get('geographic.continent.bySlug', ['slug' => $slug])
$continent
是大陆或null
🏆 贡献者
此包由Nodes的PHP团队开发并维护
📄 许可证
此包是开源软件,许可协议为MIT许可证