Completed
Push — master ( be9660...707803 )
by Georges
16s queued 13s
created

SemiReplicationCluster::clear()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 21
rs 9.6111
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author  Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Cluster\Drivers\SemiReplication;
17
18
use Phpfastcache\Cluster\ClusterPoolAbstract;
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Exceptions\PhpfastcacheExceptionInterface;
21
use Phpfastcache\Exceptions\PhpfastcacheReplicationException;
22
use Psr\Cache\CacheItemInterface;
23
24
/**
25
 * Class FullReplicationCluster
26
 * @package Phpfastcache\Cluster\Drivers\FullReplication
27
 */
28
class SemiReplicationCluster extends ClusterPoolAbstract
29
{
30
    /**
31
     * @inheritDoc
32
     */
33
    public function getItem($key)
34
    {
35
        /** @var ExtendedCacheItemInterface $item */
36
        $item = null;
37
        $eCount = 0;
38
39
        foreach ($this->clusterPools as $driverPool) {
40
            try {
41
                $poolItem = $driverPool->getItem($key);
42
                if ($poolItem->isHit()) {
43
                    if (!$item) {
44
                        $item = $poolItem;
45
                        break;
46
                    }
47
                }
48
            } catch (PhpfastcacheExceptionInterface $e) {
49
                $eCount++;
50
            }
51
        }
52
53
        if (\count($this->clusterPools) <= $eCount) {
54
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
55
        }
56
57
        return $this->getStandardizedItem($item ?? new Item($this, $key), $this);
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function hasItem($key)
64
    {
65
        $eCount = 0;
66
        foreach ($this->clusterPools as $driverPool) {
67
            try {
68
                $poolItem = $driverPool->getItem($key);
69
                if ($poolItem->isHit()) {
70
                    return true;
71
                }
72
            } catch (PhpfastcacheExceptionInterface $e) {
73
                $eCount++;
74
            }
75
        }
76
77
        if (\count($this->clusterPools) <= $eCount) {
78
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function clear()
88
    {
89
        $hasClearedOnce = false;
90
        $eCount = 0;
91
92
        foreach ($this->clusterPools as $driverPool) {
93
            try {
94
                if ($result = $driverPool->clear()) {
95
                    $hasClearedOnce = $result;
96
                }
97
            } catch (PhpfastcacheExceptionInterface $e) {
98
                $eCount++;
99
            }
100
        }
101
102
        if (\count($this->clusterPools) <= $eCount) {
103
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
104
        }
105
106
        // Return true only if at least one backend confirmed the "clear" operation
107
        return $hasClearedOnce;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function deleteItem($key)
114
    {
115
        $hasDeletedOnce = false;
116
        $eCount = 0;
117
118
        foreach ($this->clusterPools as $driverPool) {
119
            try {
120
                if ($result = $driverPool->deleteItem($key)) {
121
                    $hasDeletedOnce = $result;
122
                }
123
            } catch (PhpfastcacheExceptionInterface $e) {
124
                $eCount++;
125
            }
126
        }
127
128
        if (\count($this->clusterPools) <= $eCount) {
129
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
130
        }
131
        // Return true only if at least one backend confirmed the "clear" operation
132
        return $hasDeletedOnce;
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function save(CacheItemInterface $item)
139
    {
140
        /** @var ExtendedCacheItemInterface $item */
141
        $hasSavedOnce = false;
142
        $eCount = 0;
143
144
        foreach ($this->clusterPools as $driverPool) {
145
            try {
146
                $poolItem = $this->getStandardizedItem($item, $driverPool);
147
                if ($result = $driverPool->save($poolItem)) {
148
                    $hasSavedOnce = $result;
149
                }
150
            } catch (PhpfastcacheExceptionInterface $e) {
151
                $eCount++;
152
            }
153
        }
154
155
        if (\count($this->clusterPools) <= $eCount) {
156
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
157
        }
158
        // Return true only if at least one backend confirmed the "commit" operation
159
        return $hasSavedOnce;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function saveDeferred(CacheItemInterface $item)
166
    {
167
        /** @var ExtendedCacheItemInterface $item */
168
        $hasSavedOnce = false;
169
        foreach ($this->clusterPools as $driverPool) {
170
            $poolItem = $this->getStandardizedItem($item, $driverPool);
171
            if ($result = $driverPool->saveDeferred($poolItem)) {
172
                $hasSavedOnce = $result;
173
            }
174
        }
175
        // Return true only if at least one backend confirmed the "commit" operation
176
        return $hasSavedOnce;
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function commit()
183
    {
184
        $hasCommitOnce = false;
185
        $eCount = 0;
186
187
        foreach ($this->clusterPools as $driverPool) {
188
            try {
189
                if ($result = $driverPool->commit()) {
190
                    $hasCommitOnce = $result;
191
                }
192
            } catch (PhpfastcacheExceptionInterface $e) {
193
                $eCount++;
194
            }
195
        }
196
197
        if (\count($this->clusterPools) <= $eCount) {
198
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
199
        }
200
        // Return true only if at least one backend confirmed the "commit" operation
201
        return $hasCommitOnce;
202
    }
203
}
204