我的控制器中有以下代码:
$categories = Category::trans()->orderBy('lft')->get();
$categories = collect($categories)->keyBy('translation_of');
$categories = $subCategories = $categories->groupBy('parent_id');
if ($categories->has(0)) {
$categories = $categories->get(0)->take($maxItems);
$subCategories = $subCategories->forget(0);
$maxRowsPerCol = round($categories->count() / $numberOfCols, 0, PHP_ROUND_HALF_EVEN);
$maxRowsPerCol = ($maxRowsPerCol > 0) ? $maxRowsPerCol : 1;
$categories = $categories->chunk($maxRowsPerCol);
} else {
$categories = collect([]);
$subCategories = collect([]);
}
view()->share('categories', $categories);
view()->share('subCategories', $subCategories);
在我看来:
<ul>
@foreach ($categories as $key => $columns)
<div>
@foreach ($columns as $category)
<a href="...">{{ $category->name }}</a>
@foreach ($subCategories->get($category->tid) as $subCat)
<a href="{{ \App\Helpers\UrlGenerator::category($subCat, 1) }}">{{ $subCat->name }}</a>
@endforeach
@endforeach
</div>
@endforeach
</ul>
我的urlgenerator包含以下代码:
public static function category($entry, $level = 0, $locale = null, $countryCode = null)
{
if (is_array($entry)) {
$entry = ArrayHelper::toObject($entry);
}
if ($level == 1) {
if (isset($entry->parent) && isset($entry->parent->slug) && isset($entry->slug)) {
$attr = [
'countryCode' => $countryCode,
'catSlug' => $entry->parent->slug,
'subCatSlug' => $entry->slug,
];
$url = lurl(trans('routes.v-search-subCat', $attr, $locale), $attr, $locale);
} else {
$url = '#';
}
} else {
// Some other code not related to this question ;)
}
return $url;
}
我没有写这段代码,但我正在尝试实现新的功能来优化应用程序。问题是我正在尝试将所有类别和子类别保存到redis缓存中(我已经设置了它,其他示例也存储在那里)。
我什么都试过了,但是我不明白redis是如何缓存那些有很好关系的示例的。
如您所见,我有一个urlgenerator代码,它使用这些关系来生成url。如果我把所有的子类放到redis,我就得到 "#"
在url中,这意味着urlgenerator中的条件不符合要求。
基本上,我想做的就是将整个类别和子类别列表放到redis缓存中,以减少ttfb时间,但无法想象我如何管理与redis的关系:/
编辑:我开始考虑 Redis::set(...)
以及 Redis::get(...)
从刀锋的Angular 看,但我想这是一个不好的做法,不是吗?
暂无答案!
目前还没有任何答案,快来回答吧!