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
|
|
|
* Class BaseCache is a base class for different cache backend implementations, contains common functionality |
12
|
|
|
* @package Yiisoft\Cache |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseCache implements CacheInterface |
15
|
|
|
{ |
16
|
|
|
public const EXPIRATION_INFINITY = 0; |
17
|
|
|
/** |
18
|
|
|
* @var int|null default TTL for a cache entry. null meaning infinity, negative or zero results in cache key deletion. |
19
|
|
|
* This value is used by {@see set()} and {@see setMultiple()}, if the duration is not explicitly given. |
20
|
|
|
*/ |
21
|
|
|
private $defaultTtl; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return int|null |
25
|
|
|
*/ |
26
|
|
|
public function getDefaultTtl(): ?int |
27
|
|
|
{ |
28
|
|
|
return $this->defaultTtl; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param int|DateInterval|null $defaultTtl |
33
|
|
|
*/ |
34
|
|
|
public function setDefaultTtl($defaultTtl): void |
35
|
|
|
{ |
36
|
|
|
$this->defaultTtl = $this->normalizeTtl($defaultTtl); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $ttl |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
|
|
protected function ttlToExpiration($ttl): int |
44
|
|
|
{ |
45
|
|
|
$ttl = $this->normalizeTtl($ttl); |
46
|
|
|
|
47
|
|
|
if ($ttl === null) { |
48
|
|
|
$expiration = static::EXPIRATION_INFINITY; |
49
|
|
|
} elseif ($ttl <= 0) { |
50
|
|
|
$expiration = -1; |
51
|
|
|
} else { |
52
|
|
|
$expiration = $ttl + time(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $expiration; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Normalizes cache TTL handling `null` value and {@see DateInterval} objects. |
60
|
|
|
* @param int|DateInterval|null $ttl raw TTL. |
61
|
|
|
* @return int|null TTL value as UNIX timestamp or null meaning infinity |
62
|
|
|
*/ |
63
|
|
|
protected function normalizeTtl($ttl): ?int |
64
|
|
|
{ |
65
|
|
|
if ($ttl === null) { |
66
|
|
|
return $this->defaultTtl; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($ttl instanceof DateInterval) { |
70
|
|
|
try { |
71
|
|
|
return (new DateTime('@0'))->add($ttl)->getTimestamp(); |
72
|
|
|
} catch (Exception $e) { |
73
|
|
|
return $this->defaultTtl; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $ttl; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function iterableToArray(iterable $iterable): array |
81
|
|
|
{ |
82
|
|
|
return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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: