Skip to content

Commit 4e7e689

Browse files
authored
Update LaravelCache.php
1 parent a15c44a commit 4e7e689

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Utils/LaravelCache.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LaravelCache
1010
/**
1111
* 单位 秒
1212
*/
13-
const DEFAULT_CACHE_TIME = 3600;
13+
const DEFAULT_CACHE_TIME = [1800, 3600, 7200, 14400, 28800, 57600, 86400];
1414

1515
/**
1616
* 执行指定函数并缓存指定时长
@@ -26,7 +26,10 @@ public static function remember(string $cacheKey, callable|Carbon|null $cacheTim
2626
// 使用默认缓存时间
2727
if (is_callable($cacheTime)) {
2828
$callable = $cacheTime;
29-
$cacheTime = now()->addSeconds(LaravelCache::DEFAULT_CACHE_TIME);
29+
30+
// 防止缓存雪崩,对不同数据随机缓存时间。从半小时到 1天
31+
$cacheSeconds = rand(0, 100) % count(LaravelCache::DEFAULT_CACHE_TIME);
32+
$cacheTime = now()->addSeconds($cacheSeconds);
3033
}
3134

3235
if (!is_callable($callable)) {

0 commit comments

Comments
 (0)