1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Exception; |
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
|
|
|
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)); |
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: