Completed
Push — master ( 70ddda...03e428 )
by Sergey
06:10
created

Cache::getOrSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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