Completed
Pull Request — master (#30)
by Alexander
09:17 queued 07:44
created

ArrayCache::ttlToExpiration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
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
 * ArrayCache provides caching for the current request only by storing the values in an array.
12
 *
13
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ArrayCache supports.
14
 */
15
final class ArrayCache implements CacheInterface
16
{
17
    private const EXPIRATION_INFINITY = 0;
18
    private const EXPIRATION_EXPIRED = -1;
19
    private const TTL_EXPIRED = -1;
20
21
    private $cache = [];
22
23 136
    public function get($key, $default = null)
24
    {
25 136
        if (isset($this->cache[$key]) && !$this->isExpired($key)) {
26 114
            $value = $this->cache[$key][0];
27 114
            if (is_object($value)) {
28 20
                $value = clone $value;
29
            }
30
31 114
            return $value;
32
        }
33
34 57
        return $default;
35
    }
36
37 156
    public function set($key, $value, $ttl = null): bool
38
    {
39 156
        $expiration = $this->ttlToExpiration($ttl);
40 156
        if ($expiration < 0) {
41 2
            return $this->delete($key);
42
        }
43 156
        if (is_object($value)) {
44 46
            $value = clone $value;
45
        }
46 156
        $this->cache[$key] = [$value, $expiration];
47 156
        return true;
48
    }
49
50 24
    public function delete($key): bool
51
    {
52 24
        unset($this->cache[$key]);
53 24
        return true;
54
    }
55
56 158
    public function clear(): bool
57
    {
58 158
        $this->cache = [];
59 158
        return true;
60
    }
61
62 21
    public function getMultiple($keys, $default = null)
63
    {
64 21
        $results = [];
65 21
        foreach ($keys as $key) {
66 21
            $value = $this->get($key, $default);
67 21
            $results[$key] = $value;
68
        }
69 21
        return $results;
70
    }
71
72 27
    public function setMultiple($values, $ttl = null): bool
73
    {
74 27
        foreach ($values as $key => $value) {
75 27
            $this->set($key, $value, $ttl);
76
        }
77 27
        return true;
78
    }
79
80 2
    public function deleteMultiple($keys): bool
81
    {
82 2
        foreach ($keys as $key) {
83 2
            $this->delete($key);
84
        }
85 2
        return true;
86
    }
87
88 32
    public function has($key): bool
89
    {
90 32
        return isset($this->cache[$key]) && !$this->isExpired($key);
91
    }
92
93
    /**
94
     * Checks whether item is expired or not
95
     * @param $key
96
     * @return bool
97
     */
98 116
    private function isExpired($key): bool
99
    {
100 116
        return $this->cache[$key][1] !== 0 && $this->cache[$key][1] <= time();
101
    }
102
103
    /**
104
     * Converts TTL to expiration
105
     * @param $ttl
106
     * @return int
107
     */
108 162
    private function ttlToExpiration($ttl): int
109
    {
110 162
        $ttl = $this->normalizeTtl($ttl);
111
112 162
        if ($ttl === null) {
0 ignored issues
show
introduced by
The condition $ttl === null is always false.
Loading history...
113 154
            $expiration = static::EXPIRATION_INFINITY;
114 12
        } elseif ($ttl <= 0) {
115 4
            $expiration = static::EXPIRATION_EXPIRED;
116
        } else {
117 8
            $expiration = $ttl + time();
118
        }
119
120 162
        return $expiration;
121
    }
122
123
    /**
124
     * Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
125
     * @param int|DateInterval|null $ttl raw TTL.
126
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
127
     */
128 174
    private function normalizeTtl($ttl): ?int
129
    {
130 174
        if ($ttl instanceof DateInterval) {
131
            try {
132 5
                return (new DateTime('@0'))->add($ttl)->getTimestamp();
133
            } catch (Exception $e) {
134
                return static::TTL_EXPIRED;
135
            }
136
        }
137
138 170
        return $ttl;
139
    }
140
}
141