Completed
Push — stash_transaction_race ( 55e5bd )
by André
29:46 queued 14:41
created

TransactionItem::setClearCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 1
rs 10
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
8
namespace eZ\Publish\Core\Persistence\Cache\Adapter;
9
10
use Stash\Item;
11
12
/**
13
 * Class TransactionItem.
14
 *
15
 * Custom Item class for use during transactions, routes calls to save() and clear() to clear callback.
16
 *
17
 * @internal
18
 */
19
class TransactionItem extends Item
20
{
21
    private $clearFn;
22
    private $isItemDeferedClearedFn;
23
24
    public function setClearCallback(callable $clear)
25
    {
26
        $this->clearFn = $clear;
27
    }
28
29
    public function setIsClearedCallback(callable $isItemDeferedCleared)
30
    {
31
        $this->isItemDeferedClearedFn = $isItemDeferedCleared;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function isMiss()
38
    {
39
        // Mark any cache item which has been scheduled to be cleared as a miss.
40
        // We do this using callback since isHit property is private, & it will be reset on get()
41
        $fn = $this->isItemDeferedClearedFn;
42
        if ($fn($this->keyString)) {
43
            return true;
44
        }
45
46
        return parent::isMiss();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function save()
53
    {
54
        // We don't save cache during transaction (as cache is shared), & given the use of cache items within eZ
55
        // Platform kernel is not used across transaction boundaries items (these) given by pool can safely be assumed
56
        // to still be within transaction when save() is called.
57
        // ...
58
        // We do need to tell Pool that it should delete the item tough (which is done on commit if in transaction)
59
        // so this save will be done on-demand when needed.
60
        $clear = $this->clearFn;
61
        $clear($this->keyString);
62
63
        return false;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function clear()
70
    {
71
        $clear = $this->clearFn;
72
        $clear($this->keyString);
73
74
        return true;
75
    }
76
}
77