Issues (910)

framework/caching/WinCache.php (3 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\caching;
9
10
/**
11
 * WinCache provides Windows Cache caching in terms of an application component.
12
 *
13
 * To use this application component, the [WinCache PHP extension](https://www.iis.net/downloads/microsoft/wincache-extension)
14
 * must be loaded. Also note that "wincache.ucenabled" should be set to "On" in your php.ini file.
15
 *
16
 * See [[Cache]] manual for common cache operations that are supported by WinCache.
17
 *
18
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
19
 *
20
 * @author Qiang Xue <[email protected]>
21
 * @since 2.0
22
 */
23
class WinCache extends Cache
24
{
25
    /**
26
     * Checks whether a specified key exists in the cache.
27
     * This can be faster than getting the value from the cache if the data is big.
28
     * Note that this method does not check whether the dependency associated
29
     * with the cached data, if there is any, has changed. So a call to [[get]]
30
     * may return false while exists returns true.
31
     * @param mixed $key a key identifying the cached value. This can be a simple string or
32
     * a complex data structure consisting of factors representing the key.
33
     * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
34
     */
35
    public function exists($key)
36
    {
37
        $key = $this->buildKey($key);
38
39
        return wincache_ucache_exists($key);
40
    }
41
42
    /**
43
     * Retrieves a value from cache with a specified key.
44
     * This is the implementation of the method declared in the parent class.
45
     * @param string $key a unique key identifying the cached value
46
     * @return string|bool the value stored in cache, false if the value is not in the cache or expired.
47
     */
48
    protected function getValue($key)
49
    {
50
        return wincache_ucache_get($key);
51
    }
52
53
    /**
54
     * Retrieves multiple values from cache with the specified keys.
55
     * @param array $keys a list of keys identifying the cached values
56
     * @return array a list of cached values indexed by the keys
57
     */
58
    protected function getValues($keys)
59
    {
60
        return wincache_ucache_get($keys);
61
    }
62
63
    /**
64
     * Stores a value identified by a key in cache.
65
     * This is the implementation of the method declared in the parent class.
66
     *
67
     * @param string $key the key identifying the value to be cached
68
     * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
69
     * it could be something else.
70
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
71
     * @return bool true if the value is successfully stored into cache, false otherwise
72
     */
73
    protected function setValue($key, $value, $duration)
74
    {
75
        return wincache_ucache_set($key, $value, $duration);
76
    }
77
78
    /**
79
     * Stores multiple key-value pairs in cache.
80
     * @param array $data array where key corresponds to cache key while value is the value stored
81
     * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
82
     * @return array array of failed keys
83
     */
84
    protected function setValues($data, $duration)
85
    {
86
        return wincache_ucache_set($data, null, $duration);
0 ignored issues
show
Bug Best Practice introduced by
The expression return wincache_ucache_set($data, null, $duration) returns the type boolean which is incompatible with the documented return type array.
Loading history...
87
    }
88
89
    /**
90
     * Stores a value identified by a key into cache if the cache does not contain this key.
91
     * This is the implementation of the method declared in the parent class.
92
     *
93
     * @param string $key the key identifying the value to be cached
94
     * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
95
     * it could be something else.
96
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
97
     * @return bool true if the value is successfully stored into cache, false otherwise
98
     */
99
    protected function addValue($key, $value, $duration)
100
    {
101
        return wincache_ucache_add($key, $value, $duration);
102
    }
103
104
    /**
105
     * Adds multiple key-value pairs to cache.
106
     * The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
107
     * storage supports multiadd, this method should be overridden to exploit that feature.
108
     * @param array $data array where key corresponds to cache key while value is the value stored
109
     * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
110
     * @return array array of failed keys
111
     */
112
    protected function addValues($data, $duration)
113
    {
114
        return wincache_ucache_add($data, null, $duration);
0 ignored issues
show
Bug Best Practice introduced by
The expression return wincache_ucache_add($data, null, $duration) returns the type boolean which is incompatible with the documented return type array.
Loading history...
$data of type array is incompatible with the type string expected by parameter $key of wincache_ucache_add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        return wincache_ucache_add(/** @scrutinizer ignore-type */ $data, null, $duration);
Loading history...
115
    }
116
117
    /**
118
     * Deletes a value with the specified key from cache
119
     * This is the implementation of the method declared in the parent class.
120
     * @param string $key the key of the value to be deleted
121
     * @return bool if no error happens during deletion
122
     */
123
    protected function deleteValue($key)
124
    {
125
        return wincache_ucache_delete($key);
126
    }
127
128
    /**
129
     * Deletes all values from cache.
130
     * This is the implementation of the method declared in the parent class.
131
     * @return bool whether the flush operation was successful.
132
     */
133
    protected function flushValues()
134
    {
135
        return wincache_ucache_clear();
136
    }
137
}
138