nodes cache

此包已被废弃,不再维护。没有推荐替代包。
此包最新版本(1.0.11)没有提供许可证信息。

简化Laravel缓存的控制和管理工作

1.0.11 2020-02-13 16:03 UTC

README

Laravel提供处理缓存的简单集成 - 以结构化方式。

缓存键通常会累积,最终成为字符串中的3个以上参数。或者对URL进行缓存会导致不同的查询参数顺序产生不同的缓存版本。查询参数通常被处理为哈希表或字典,它们没有固定的顺序。

最糟糕的是,你会在项目中到处都是碎片化的缓存设置。

此包将使您的生活变得更加容易

Total downloads Monthly downloads Latest release Open issues License Star repository on GitHub Watch repository on GitHub Fork repository on GitHub StyleCI

📝 简介

我们在Nodes非常缺少Laravel中的缓存管理结构。

我们找到了一种更灵活、更有结构的方式来管理缓存及其生命周期。我们还创建了一些辅助方法,使整个过程更加简单和出色。

📦 安装

要安装此包,您需要

  • 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团队开发并维护

Follow Nodes PHP on Twitter Tweet Nodes PHP

📄 许可证

此包是开源软件,许可协议为MIT许可证