Skip to content

Commit 7ffd233

Browse files
committed
Added callable support for fallback value, refactored variable to be named , and replaced the suffix stringification by a MD5 hash of the serialized suffix array
1 parent e0f7380 commit 7ffd233

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/Cache.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
abstract class Cache
88
{
9-
public static function magicGet($suffixes = [], $default = null)
9+
public static function magicGet($suffixes = [], $fallback = null)
1010
{
1111
$key = static::magicKey($suffixes);
12-
return static::get($key, $default);
12+
return static::get($key, $fallback);
1313
}
1414

1515
public static function magicSet($value, $expiresAfter = 0, $suffixes = [])
@@ -22,22 +22,26 @@ public static function magicKey($suffixes = [])
2222
{
2323
$origin = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3)[2];
2424
$suffixes = $suffixes ?: $origin['args'];
25-
$suffix = implode('.', $suffixes);
26-
$key = $origin['class'] . '.' . $origin['function'] . ($suffix ? '.' . $suffix : '');
25+
$suffix = md5(serialize($suffixes));
26+
$key = $origin['class'] . '.' . $origin['function'] . '.' . $suffix;
2727
$key = strtolower(str_replace('\\', '.', $key));
2828

2929
return $key;
3030
}
3131

32-
public static function get($key, $default = null)
32+
public static function get($key, $fallback = null)
3333
{
3434
$cache = static::getCache($key);
3535

3636
if ($cache->isHit()) {
3737
return $cache->get();
3838
}
3939

40-
return $default;
40+
if (is_callable($fallback)) {
41+
return call_user_func($fallback);
42+
}
43+
44+
return $fallback;
4145
}
4246

4347
public static function set($key, $value, $expiresAfter = 0)

0 commit comments

Comments
 (0)