Completed
Pull Request — master (#30)
by Alexander
01:22
created

WinCache::delete()   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 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
eloc 1
nc 1
nop 1
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace Yiisoft\Cache;
4
5
use DateInterval;
6
use DateTime;
7
use Exception;
8
use Psr\SimpleCache\CacheInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Cache\CacheInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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://sourceforge.net/projects/wincache/)
14
 * must be loaded. Also note that "wincache.ucenabled" should be set to "1" in your php.ini file.
15
 *
16
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by WinCache.
17
 */
18
class WinCache implements CacheInterface
19
{
20
    private const TTL_INFINITY = 0;
21
    private const TTL_EXPIRED = -1;
22
23
    public function get($key, $default = null)
24
    {
25
        $value = \wincache_ucache_get($key, $success);
26
        return $success ? $value : $default;
27
    }
28
29
    public function set($key, $value, $ttl = null)
30
    {
31
        $ttl = $this->normalizeTtl($ttl);
32
        if ($ttl < 0) {
33
            return $this->delete($key);
34
        }
35
        return \wincache_ucache_set($key, $value, $ttl);
36
    }
37
38
    public function delete($key)
39
    {
40
        return \wincache_ucache_delete($key);
41
    }
42
43
    public function clear()
44
    {
45
        return \wincache_ucache_clear();
46
    }
47
48
    public function getMultiple($keys, $default = null)
49
    {
50
        $defaultValues = array_fill_keys($this->iterableToArray($keys), $default);
51
        return array_merge($defaultValues, \wincache_ucache_get($this->iterableToArray($keys)));
52
    }
53
54
    public function setMultiple($values, $ttl = null)
55
    {
56
        return \wincache_ucache_set($this->iterableToArray($values), null, $this->normalizeTtl($ttl)) === [];
57
    }
58
59
    public function deleteMultiple($keys)
60
    {
61
        $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

61
        $deleted = array_flip(/** @scrutinizer ignore-type */ \wincache_ucache_delete($keys));
Loading history...
62
        foreach ($keys as $expectedKey) {
63
            if (!isset($deleted[$expectedKey])) {
64
                return false;
65
            }
66
        }
67
        return true;
68
    }
69
70
    public function has($key)
71
    {
72
        return \wincache_ucache_exists($key);
73
    }
74
75
    /**
76
     * Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
77
     * @param int|DateInterval|null $ttl raw TTL.
78
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
79
     */
80
    private function normalizeTtl($ttl): ?int
81
    {
82
        $normalizedTtl = $ttl;
83
        if ($ttl instanceof DateInterval) {
84
            try {
85
                $normalizedTtl = (new DateTime('@0'))->add($ttl)->getTimestamp();
86
            } catch (Exception $e) {
87
                $normalizedTtl = self::TTL_EXPIRED;
88
            }
89
        }
90
91
        return $normalizedTtl ?? static::TTL_INFINITY;
92
    }
93
94
    /**
95
     * Converts iterable to array
96
     * @param iterable $iterable
97
     * @return array
98
     */
99
    private function iterableToArray(iterable $iterable): array
100
    {
101
        return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable;
102
    }
103
}
104