@@ -28,9 +28,15 @@ Cache::set('my_cache_key_2', 'my second value', 3600);
28
28
Cache::set('my_cache_key_3', ['obj' => 'value']);
29
29
30
30
// Getting data from cache
31
- // Definition: get($key, $default = null)
31
+ // Definition: get($key, $fallback = null)
32
32
Cache::get('my_cache_key');
33
- Cache::get('my_non_existing_key', 'default/fallback value');
33
+ Cache::get('my_non_existing_key', 'fallback value');
34
+ // The $fallback parameter also allows for callables
35
+ Cache::get('my_non_existing_key', 'MyClass::myCallbackMethod');
36
+ Cache::get('my_non_existing_key', ['MyClass', 'myCallbackMethod']);
37
+ Cache::get('my_non_existing_key', function(){
38
+ return 'fallback value';
39
+ });
34
40
35
41
// Manually removing data from cache
36
42
// Definition: remove($key)
@@ -45,17 +51,14 @@ Cache::prune();
45
51
// Here's an example:
46
52
function getRecordFromDatabase($id)
47
53
{
48
- if ($cachedValue = Cache::magicGet()) {
49
- return $cachedValue;
50
- }
51
-
52
- $value = fetchValueFromDatabase($id); // replace this with your own data fetching/processing code
53
-
54
- return Cache::magicSet($value);
54
+ return Cache::magicGet([], function() use ($id) {
55
+ $value = fetchValueFromDatabase($id); // replace this with your own data fetching/processing code
56
+ return Cache::magicSet($value);
57
+ });
55
58
}
56
59
57
60
// The definition of both methods goes as follows:
58
- // magicGet($suffixes = [] , $default = null )
61
+ // magicGet($fallback = null , $suffixes = [] )
59
62
// magicSet($value, $expiresAfter = 0, $suffixes = [])
60
63
```
61
64
0 commit comments