Completed
Push — deferred_cache_invalidation ( b52909 )
by
unknown
20:31
created

TransactionalCacheAdapterDecorator::deleteItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Cache\Adapter;
10
11
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
12
use Psr\Cache\CacheItemInterface;
13
14
/**
15
 * Internal proxy adapter invalidating cache items on transaction commits/rollbacks.
16
 */
17
class TransactionalCacheAdapterDecorator implements TagAwareAdapterInterface
18
{
19
    /** @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface */
20
    protected $innerPool;
21
22
    /** @var int */
23
    protected $transactionNestingLevel;
24
25
    /** @var array */
26
    protected $deferredTagsInvalidation;
27
28
    /** @var array */
29
    protected $deferredItemsDeletion;
30
31
    /**
32
     * @param \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface $innerPool
33
     * @param int $transactionNestingLevel
34
     * @param array $deferredTagsInvalidation
35
     * @param array $deferredItemsDeletion
36
     */
37
    public function __construct(
38
        TagAwareAdapterInterface $innerPool,
39
        int $transactionNestingLevel = 0,
40
        array $deferredTagsInvalidation = [],
41
        array $deferredItemsDeletion = []
42
    ) {
43
        $this->innerPool = $innerPool;
44
        $this->transactionNestingLevel = $transactionNestingLevel;
45
        $this->deferredTagsInvalidation = $deferredTagsInvalidation;
46
        $this->deferredItemsDeletion = $deferredItemsDeletion;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getItem($key)
53
    {
54
        /** @var \Symfony\Component\Cache\CacheItem $newItem */
55
        $item = $this->innerPool->getItem($key);
56
57
        if ($this->transactionNestingLevel > 0) {
58
            $item->expiresAfter(0);
59
            $this->innerPool->save($item);
60
            return $this->innerPool->getItem($key);
61
        }
62
63
        return $item;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getItems(array $keys = [])
70
    {
71
        if ($this->transactionNestingLevel > 0) {
72
            // disable cache
73
        }
74
75
        return $this->innerPool->getItems($keys);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasItem($key)
82
    {
83
        if ($this->transactionNestingLevel > 0) {
84
            // disable cache ?
85
        }
86
87
        return $this->innerPool->hasItem($key);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function deleteItem($key)
94
    {
95
        if ($this->transactionNestingLevel > 0) {
96
            $this->deferredItemsDeletion[$this->transactionNestingLevel][] = $key;
97
98
            return true;
99
        }
100
101
        return $this->innerPool->deleteItem($key);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function deleteItems(array $keys)
108
    {
109
        if ($this->transactionNestingLevel > 0) {
110
            $this->deferredItemsDeletion[$this->transactionNestingLevel] += $keys;
111
112
            return true;
113
        }
114
115
        return $this->innerPool->deleteItems($keys);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function invalidateTags(array $tags)
122
    {
123
        if ($this->transactionNestingLevel > 0) {
124
            $this->deferredTagsInvalidation[$this->transactionNestingLevel] += $tags;
125
126
            return true;
127
        }
128
129
        return $this->innerPool->invalidateTags($tags);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function clear()
136
    {
137
        $this->deferredItemsDeletion = [];
138
        $this->deferredTagsInvalidation = [];
139
        --$this->transactionNestingLevel;
140
141
        return $this->innerPool->clear();
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function save(CacheItemInterface $item)
148
    {
149
        return $this->innerPool->save($item);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function saveDeferred(CacheItemInterface $item)
156
    {
157
        return $this->innerPool->saveDeferred($item);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function commit()
164
    {
165
        return $this->innerPool->commit();
166
    }
167
168
    public function enableTransactionMode(): void
169
    {
170
        ++$this->transactionNestingLevel;
171
        $this->deferredTagsInvalidation[$this->transactionNestingLevel] = [];
172
        $this->deferredItemsDeletion[$this->transactionNestingLevel] = [];
173
    }
174
175
    public function disableTransactionMode(): void
176
    {
177
        $this->invalidateDeferredTags();
178
        $this->deleteDeferredItems();
179
        --$this->transactionNestingLevel;
180
    }
181
182
    protected function invalidateDeferredTags(): void
183
    {
184
        $tags = $this->deferredTagsInvalidation[$this->transactionNestingLevel];
185
186
        $this->innerPool->invalidateTags(array_unique($tags));
187
    }
188
189
    protected function deleteDeferredItems(): void
190
    {
191
        $keys = $this->deferredItemsDeletion[$this->transactionNestingLevel];
192
193
        $this->innerPool->deleteItems(array_unique($keys));
194
    }
195
}
196