Completed
Push — master ( 2abc91...e91e9a )
by Alexander
01:35
created

WinCache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Yiisoft\Cache;
3
4
/**
5
 * WinCache provides Windows Cache caching in terms of an application component.
6
 *
7
 * To use this application component, the [WinCache PHP extension](https://sourceforge.net/projects/wincache/)
8
 * must be loaded. Also note that "wincache.ucenabled" should be set to "1" in your php.ini file.
9
 *
10
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by WinCache.
11
 */
12
final class WinCache extends SimpleCache
13
{
14
    private const TTL_INFINITY = 0;
15
16
    protected function hasValue(string $key): bool
17
    {
18
        return \wincache_ucache_exists($key);
19
    }
20
21
    protected function getValue(string $key, $default = null)
22
    {
23
        $value = \wincache_ucache_get($key, $success);
24
        return $success ? $value : $default;
25
    }
26
27
    protected function getValues(iterable $keys, $default = null): iterable
28
    {
29
        $defaultValues = array_fill_keys($keys, $default);
0 ignored issues
show
Bug introduced by
$keys of type iterable is incompatible with the type array expected by parameter $keys of array_fill_keys(). ( Ignorable by Annotation )

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

29
        $defaultValues = array_fill_keys(/** @scrutinizer ignore-type */ $keys, $default);
Loading history...
30
        return array_merge($defaultValues, \wincache_ucache_get($keys));
31
    }
32
33
    protected function setValue(string $key, $value, ?int $ttl): bool
34
    {
35
        return \wincache_ucache_set($key, $value, $ttl ?? self::TTL_INFINITY);
36
    }
37
38
    protected function setValues(iterable $values, ?int $ttl): bool
39
    {
40
        return \wincache_ucache_set($values, null, $ttl ?? self::TTL_INFINITY) === [];
41
    }
42
43
    protected function deleteValue(string $key): bool
44
    {
45
        return \wincache_ucache_delete($key);
46
    }
47
48
    public function clear(): bool
49
    {
50
        return \wincache_ucache_clear();
51
    }
52
53
    public function deleteValues(iterable $keys): bool
54
    {
55
        $deleted = array_flip(\wincache_ucache_delete($keys));
0 ignored issues
show
Bug introduced by
wincache_ucache_delete($keys) of type boolean is incompatible with the type array expected by parameter $array of array_flip(). ( Ignorable by Annotation )

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

55
        $deleted = array_flip(/** @scrutinizer ignore-type */ \wincache_ucache_delete($keys));
Loading history...
56
        foreach ($keys as $expectedKey) {
57
            if (!isset($deleted[$expectedKey])) {
58
                return false;
59
            }
60
        }
61
        return true;
62
    }
63
}
64