Passed
Push — master ( 0ba014...6d80cc )
by Sergey
02:39
created

Cache::cache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 10
nc 3
nop 4
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-facades
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\facades;
11
12
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
13
/**
14
 * Cache facade.
15
 *
16
 * Facades Yii::$app->get('cache') component.
17
 *
18
 * @see \yii\caching\Cache
19
 * @method static bool add(mixed $key, mixed $value, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores a value identified by a key into cache if the cache does not contain this key.
20
 * @method static \yii\base\Behavior attachBehavior(string $name, string|array|\yii\base\Behavior $behavior) Attaches a behavior to this component.
21
 * @method static attachBehaviors(array $behaviors) Attaches a list of behaviors to the component.
22
 * @method static array behaviors() Returns a list of behaviors that this component should behave as.
23
 * @method static string buildKey(mixed $key) Builds a normalized cache key from a given key.
24
 * @method static bool delete(mixed $key) Deletes a value with the specified key from cache.
25
 * @method static null|\yii\base\Behavior detachBehavior(string $name) Detaches a behavior from the component.
26
 * @method static detachBehaviors() Detaches all behaviors from the component.
27
 * @method static ensureBehaviors() Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
28
 * @method static bool exists(mixed $key) Checks whether a specified key exists in the cache.
29
 * @method static bool flush() Deletes all values from cache.
30
 * @method static null|\yii\base\Behavior getBehavior(string $name) Returns the named behavior object.
31
 * @method static \yii\base\Behavior[] getBehaviors() Returns all behaviors attached to this component.
32
 * @method static string getKeyPrefix() Returns a string prefixed to every cache key so that it is unique globally in the whole cache storage.
33
 * @method static array|bool getSerializer() Returns the functions used to serialize and unserialize cached data.
34
 * @method static bool hasEventHandlers(string $name) Returns a value indicating whether there is any handler attached to the named event.
35
 * @method static bool madd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
36
 * @method static array mget(string[] $keys) Retrieves multiple values from cache with the specified keys.
37
 * @method static bool mset(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
38
 * @method static bool multiAdd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
39
 * @method static array multiGet(string[] $keys) Retrieves multiple values from cache with the specified keys.
40
 * @method static bool multiSet(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
41
 * @method static bool off(string $name, callable $handler = null) Detaches an existing event handler from this component.
42
 * @method static on(string $name, callable $handler, mixed $data = null, bool $append = true) Attaches an event handler to an event.
43
 * @method static bool set(mixed $key, mixed $value, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores a value identified by a key into cache.
44
 * @method static setKeyPrefix(string $value) Sets a string prefixed to every cache key so that it is unique globally in the whole cache storage.
45
 * @method static setSerializer(array|bool $value) Sets the functions used to serialize and unserialize cached data.
46
 * @method static trigger(string $name, \yii\base\Event $event = null) Triggers an event.
47
 */
48
class Cache extends Facade
49
{
50
    /**
51
     * @inheritdoc
52
     */
53
    public static function getFacadeComponentId()
54
    {
55
        return 'cache';
56
    }
57
58
    /**
59
     * Retrieves a value from cache (or stores it if it's not cached) with a specified key.
60
     * If the cache already contains such a key, it will be returned, else the default value
61
     * will be stored in the cache and returned.
62
     *
63
     * @param mixed $key a key identifying the cached value. This can be a simple string or
64
     * a complex data structure consisting of factors representing the key.
65
     * @param callable|mixed $default the value to be cached and retrieved if some value is not already in the cache. This can also be
66
     * any callable function that will recieve the key and should return a value.
67
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
68
     * @param \yii\caching\Dependency $dependency dependency of the cached item. If the dependency changes,
69
     * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
70
     * This parameter is ignored if [[serializer]] is false.
71
     *
72
     * @return bool whether the value is successfully stored into cache.
73
     */
74
    public static function cache($key, $default, $duration = 0, $dependency = null)
75
    {
76
        /**
77
         * @var \yii\caching\Cache $cache
78
         */
79
        $cache = static::getFacadeComponent();
80
        $value = $cache->get($key);
81
        if ($value === false) {
82
            if ($default instanceof \Closure) {
83
                $value = call_user_func($default, $key);
84
            } else {
85
                $value = $default;
86
            }
87
            $cache->set($key, $value, $duration, $dependency);
88
        }
89
        return $value;
90
    }
91
92
    /**
93
     * Retrieves a value from cache with a specified key.
94
     *
95
     * @param mixed $key a key identifying the cached value. This can be a simple string or
96
     * a complex data structure consisting of factors representing the key.
97
     * @param callable|mixed $default a default value. This can also be any callable function that
98
     * will recieve the key and should return a value.
99
     *
100
     * @return mixed the value stored in cache or the default value if the value is not in the cache, expired,
101
     * or the dependency associated with the cached data has changed.
102
     */
103
    public static function get($key, $default = false)
104
    {
105
        $value = static::getFacadeComponent()->get($key);
106
        if ($value === false) {
107
            if ($default instanceof \Closure) {
108
                $value = call_user_func($default, $key);
109
            } else {
110
                $value = $default;
111
            }
112
        }
113
        return $value;
114
    }
115
116
    /**
117
     * Retrieves a value from cache (or stores it if it's not cached) with a specified key.
118
     *
119
     * @see cache
120
     * @param mixed $key a key identifying the cached value.
121
     * @param callable|mixed $default the value to be cached and retrieved if some value is not already in the cache.
122
     * @param int $duration the number of seconds in which the cached value will expire.
123
     * @param \yii\caching\Dependency $dependency dependency of the cached item.
124
     *
125
     * @return bool whether the value is successfully stored into cache.
126
     */
127
    public static function getOrSet($key, $default, $duration = 0, $dependency = null)
128
    {
129
        return static::cache($key, $default, $duration, $dependency);
130
    }
131
}
132