1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\ORM\Cache\Persister\Collection; |
||
6 | |||
7 | use Doctrine\ORM\Cache\CollectionCacheKey; |
||
8 | use Doctrine\ORM\Cache\ConcurrentRegion; |
||
9 | use Doctrine\ORM\EntityManagerInterface; |
||
10 | use Doctrine\ORM\Mapping\AssociationMetadata; |
||
11 | use Doctrine\ORM\PersistentCollection; |
||
12 | use Doctrine\ORM\Persisters\Collection\CollectionPersister; |
||
13 | |||
14 | class ReadWriteCachedCollectionPersister extends AbstractCollectionPersister |
||
15 | { |
||
16 | /** |
||
17 | * @param CollectionPersister $persister The collection persister that will be cached. |
||
18 | * @param ConcurrentRegion $region The collection region. |
||
19 | * @param EntityManagerInterface $em The entity manager. |
||
20 | * @param AssociationMetadata $association The association mapping. |
||
21 | */ |
||
22 | 20 | public function __construct( |
|
23 | CollectionPersister $persister, |
||
24 | ConcurrentRegion $region, |
||
25 | EntityManagerInterface $em, |
||
26 | AssociationMetadata $association |
||
27 | ) { |
||
28 | 20 | parent::__construct($persister, $region, $em, $association); |
|
29 | 20 | } |
|
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 2 | public function afterTransactionComplete() |
|
35 | { |
||
36 | 2 | if (isset($this->queuedCache['update'])) { |
|
37 | 1 | foreach ($this->queuedCache['update'] as $item) { |
|
38 | 1 | $this->region->evict($item['key']); |
|
39 | } |
||
40 | } |
||
41 | |||
42 | 2 | if (isset($this->queuedCache['delete'])) { |
|
43 | 1 | foreach ($this->queuedCache['delete'] as $item) { |
|
44 | 1 | $this->region->evict($item['key']); |
|
45 | } |
||
46 | } |
||
47 | |||
48 | 2 | $this->queuedCache = []; |
|
49 | 2 | } |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 4 | public function afterTransactionRolledBack() |
|
55 | { |
||
56 | 4 | if (isset($this->queuedCache['update'])) { |
|
57 | 2 | foreach ($this->queuedCache['update'] as $item) { |
|
58 | 2 | $this->region->evict($item['key']); |
|
59 | } |
||
60 | } |
||
61 | |||
62 | 4 | if (isset($this->queuedCache['delete'])) { |
|
63 | 2 | foreach ($this->queuedCache['delete'] as $item) { |
|
64 | 2 | $this->region->evict($item['key']); |
|
65 | } |
||
66 | } |
||
67 | |||
68 | 4 | $this->queuedCache = []; |
|
69 | 4 | } |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 6 | public function delete(PersistentCollection $collection) |
|
75 | { |
||
76 | 6 | $ownerId = $this->uow->getEntityIdentifier($collection->getOwner()); |
|
77 | 6 | $key = new CollectionCacheKey($this->sourceEntity->getRootClassName(), $this->association->getName(), $ownerId); |
|
78 | 6 | $lock = $this->region->lock($key); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
79 | |||
80 | 6 | $this->persister->delete($collection); |
|
81 | |||
82 | 6 | if ($lock === null) { |
|
83 | 2 | return; |
|
84 | } |
||
85 | |||
86 | 4 | $this->queuedCache['delete'][spl_object_id($collection)] = [ |
|
87 | 4 | 'key' => $key, |
|
88 | 4 | 'lock' => $lock, |
|
89 | ]; |
||
90 | 4 | } |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 6 | public function update(PersistentCollection $collection) |
|
96 | { |
||
97 | 6 | $isInitialized = $collection->isInitialized(); |
|
98 | 6 | $isDirty = $collection->isDirty(); |
|
99 | |||
100 | 6 | if (! $isInitialized && ! $isDirty) { |
|
101 | return; |
||
102 | } |
||
103 | |||
104 | 6 | $this->persister->update($collection); |
|
105 | |||
106 | 6 | $ownerId = $this->uow->getEntityIdentifier($collection->getOwner()); |
|
107 | 6 | $key = new CollectionCacheKey($this->sourceEntity->getRootClassName(), $this->association->getName(), $ownerId); |
|
108 | 6 | $lock = $this->region->lock($key); |
|
109 | |||
110 | 6 | if ($lock === null) { |
|
111 | 2 | return; |
|
112 | } |
||
113 | |||
114 | 4 | $this->queuedCache['update'][spl_object_id($collection)] = [ |
|
115 | 4 | 'key' => $key, |
|
116 | 4 | 'lock' => $lock, |
|
117 | ]; |
||
118 | 4 | } |
|
119 | } |
||
120 |