Cache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 68
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacadeComponentId() 0 4 1
A cache() 0 17 3
A get() 0 12 3
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * Generated on Yii 2.0.12
6
 *
7
 * @see       https://github.com/sergeymakinen/yii2-facades
8
 * @copyright Copyright (c) 2016-2017 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 mixed getOrSet(mixed $key, callable|\Closure $callable, int $duration = null, \yii\caching\Dependency $dependency = null) Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key, or to store the result of $callable execution if there is no cache available for the $key.
60
 * @see \yii\caching\Cache::getOrSet
61
 *
62
 * @method static bool hasEventHandlers(string $name) Returns a value indicating whether there is any handler attached to the named event.
63
 * @see \yii\base\Component::hasEventHandlers
64
 *
65
 * @method static array madd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
66
 * @see \yii\caching\Cache::madd
67
 *
68
 * @method static array mget(string[] $keys) Retrieves multiple values from cache with the specified keys.
69
 * @see \yii\caching\Cache::mget
70
 *
71
 * @method static array mset(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
72
 * @see \yii\caching\Cache::mset
73
 *
74
 * @method static array multiAdd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
75
 * @see \yii\caching\Cache::multiAdd
76
 *
77
 * @method static array multiGet(string[] $keys) Retrieves multiple values from cache with the specified keys.
78
 * @see \yii\caching\Cache::multiGet
79
 *
80
 * @method static array multiSet(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
81
 * @see \yii\caching\Cache::multiSet
82
 *
83
 * @method static bool off(string $name, callable $handler = null) Detaches an existing event handler from this component.
84
 * @see \yii\base\Component::off
85
 *
86
 * @method static void on(string $name, callable $handler, mixed $data = null, bool $append = true) Attaches an event handler to an event.
87
 * @see \yii\base\Component::on
88
 *
89
 * @method static bool set(mixed $key, mixed $value, int $duration = null, \yii\caching\Dependency $dependency = null) Stores a value identified by a key into cache.
90
 * @see \yii\caching\Cache::set
91
 *
92
 * @method static void trigger(string $name, \yii\base\Event $event = null) Triggers an event.
93
 * @see \yii\base\Component::trigger
94
 *
95
 * Property accessors
96
 *
97
 * @method static int getDefaultDuration() Returns default duration in seconds before a cache entry will expire.
98
 * @see \yii\caching\Cache::defaultDuration
99
 *
100
 * @method static string getKeyPrefix() Returns 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 null|array|bool getSerializer() Returns the functions used to serialize and unserialize cached data.
104
 * @see \yii\caching\Cache::serializer
105
 *
106
 * @method static void setDefaultDuration(int $value) Sets default duration in seconds before a cache entry will expire.
107
 * @see \yii\caching\Cache::defaultDuration
108
 *
109
 * @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.
110
 * @see \yii\caching\Cache::keyPrefix
111
 *
112
 * @method static void setSerializer(null|array|bool $value) Sets the functions used to serialize and unserialize cached data.
113
 * @see \yii\caching\Cache::serializer
114
 */
115
class Cache extends Facade
116
{
117
    /**
118
     * @inheritDoc
119
     */
120 3
    public static function getFacadeComponentId()
121
    {
122 3
        return 'cache';
123
    }
124
125
    /**
126
     * Retrieves a value from cache (or stores it if it's not cached) with a specified key.
127
     * If the cache already contains such a key, it will be returned, else the default value
128
     * will be stored in the cache and returned.
129
     *
130
     * @param mixed $key a key identifying the cached value. This can be a simple string or
131
     * a complex data structure consisting of factors representing the key.
132
     * @param callable|mixed $default the value to be cached and retrieved if some value is not already in the cache. This can also be
133
     * any callable function that will recieve the key and should return a value.
134
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
135
     * @param \yii\caching\Dependency $dependency dependency of the cached item. If the dependency changes,
136
     * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
137
     * This parameter is ignored if [[serializer]] is false.
138
     *
139
     * @return bool whether the value is successfully stored into cache.
140
     */
141 1
    public static function cache($key, $default, $duration = 0, $dependency = null)
142
    {
143
        /**
144
         * @var \yii\caching\Cache $cache
145
         */
146 1
        $cache = static::getFacadeComponent();
147 1
        $value = $cache->get($key);
148 1
        if ($value === false) {
149 1
            if ($default instanceof \Closure) {
150 1
                $value = call_user_func($default, $key);
151 1
            } else {
152 1
                $value = $default;
153
            }
154 1
            $cache->set($key, $value, $duration, $dependency);
155 1
        }
156 1
        return $value;
157
    }
158
159
    /**
160
     * Retrieves a value from cache with a specified key.
161
     *
162
     * @param mixed $key a key identifying the cached value. This can be a simple string or
163
     * a complex data structure consisting of factors representing the key.
164
     * @param callable|mixed $default a default value. This can also be any callable function that
165
     * will recieve the key and should return a value.
166
     *
167
     * @return mixed the value stored in cache or the default value if the value is not in the cache, expired,
168
     * or the dependency associated with the cached data has changed.
169
     */
170 2
    public static function get($key, $default = false)
171
    {
172 2
        $value = static::getFacadeComponent()->get($key);
173 2
        if ($value === false) {
174 2
            if ($default instanceof \Closure) {
175 2
                $value = call_user_func($default, $key);
176 2
            } else {
177 2
                $value = $default;
178
            }
179 2
        }
180 2
        return $value;
181
    }
182
}
183