Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cache.php
1<?php
2
4
13
20{
22 protected $pool;
24 protected $cache;
26 protected $idMap = [];
28 protected $externalIdMap = [];
30 protected $isItemChanged = false;
31
32 public function __construct(Pool $pool, int $ttl, string $cacheId, \Bitrix\Main\Data\Cache $cache, EventManager $eventManager)
33 {
34 $this->pool = $pool;
35 parent::__construct($ttl, $cacheId, $cache, $eventManager);
36 }
37
41 public function isScopeSatisfy(int $scope): bool
42 {
43 return $scope === LOCATION_SEARCH_SCOPE_ALL || $scope === LOCATION_SEARCH_SCOPE_INTERNAL;
44 }
45
46 protected function loadDataFromCache(): void
47 {
48 $items = $this->cache->getVars()['items'];
49
50 if(!is_array($items))
51 {
52 return;
53 }
54
55 $this->pool->setItems($items);
56 $this->idMap = $this->cache->getVars()['idMap'];
57 $this->externalIdMap = $this->cache->getVars()['externalIdMap'];
58 }
59
60 public function saveDataToCache(): void
61 {
62 if($this->isItemChanged)
63 {
64 $this->cache->forceRewriting(true);
65 $this->cache->startDataCache();
66 $this->cache->endDataCache([
67 'items' => $this->pool->getItems(),
68 'idMap' => $this->idMap,
69 'externalIdMap' => $this->externalIdMap
70 ]);
71 }
72 }
73
79 protected function createIdIndex(int $locationId, string $languageId): string
80 {
81 return (string)$locationId.'_'.$languageId;
82 }
83
84 protected function createExternalIdIndex(string $externalId, string $sourceCode, string $languageId): string
85 {
86 return $externalId.'_'.$sourceCode.'_'.$languageId;
87 }
88
90 public function findById(int $locationId, string $languageId)
91 {
92 $result = null;
93 $externalIndex = $this->createIdIndex($locationId, $languageId);
94
95 if(isset($this->idMap[$externalIndex]))
96 {
97 $result = $this->pool->getItem($this->idMap[$externalIndex]);
98 }
99
100 return $result;
101 }
102
104 public function findByExternalId(string $externalId, string $sourceCode, string $languageId)
105 {
106 $result = null;
107
108 $externalIndex = $this->createExternalIdIndex($externalId, $sourceCode, $languageId);
109
110 if(isset($this->externalIdMap[$externalIndex]))
111 {
112 $result = $this->pool->getItem($this->externalIdMap[$externalIndex]);
113 }
114
115 return $result;
116 }
117
119 public function save(Location $location): Result
120 {
121 $index = $this->pool->getItemsCount();
122 $this->pool->addItem($index, $location);
123 $languageId = $location->getLanguageId();
124
125 if($locationId = $location->getId())
126 {
127 $tmpIndex = $this->createIdIndex($locationId, $languageId);
128 $this->idMap[$tmpIndex] = $index;
129 }
130
131 if($externalId = $location->getExternalId())
132 {
133 $sourceCode = $location->getSourceCode();
134 $tmpIndex = $this->createExternalIdIndex($externalId, $sourceCode, $languageId);
135 $this->externalIdMap[$tmpIndex] = $index;
136 }
137
138 $this->isItemChanged = true;
139 return new Result();
140 }
141
146 public function delete(Location $location): Result
147 {
148 $index = null;
149 $languageId = $location->getLanguageId();
150
151 if($locationId = $location->getId())
152 {
153 $tmpIndex = $this->createIdIndex($locationId, $languageId);
154
155 if(isset($this->idMap[$tmpIndex]))
156 {
157 $index = $this->idMap[$tmpIndex];
158 unset($this->idMap[$tmpIndex]);
159 }
160 }
161
162 if($externalId = $location->getExternalId())
163 {
164 $sourceCode = $location->getSourceCode();
165 $tmpIndex = $this->createExternalIdIndex($externalId, $sourceCode, $languageId);
166
167 if(isset($this->externalIdMap[$tmpIndex]))
168 {
169 if($index === null)
170 {
171 $index = $this->externalIdMap[$tmpIndex];
172 }
173
174 unset($this->externalIdMap[$tmpIndex]);
175 }
176 }
177
178 if($index)
179 {
180 $this->pool->deleteItem($index);
181 }
182
183 $this->isItemChanged = true;
184 return new Result();
185 }
186}
findById(int $locationId, string $languageId)
Definition cache.php:90
__construct(Pool $pool, int $ttl, string $cacheId, \Bitrix\Main\Data\Cache $cache, EventManager $eventManager)
Definition cache.php:32
createExternalIdIndex(string $externalId, string $sourceCode, string $languageId)
Definition cache.php:84
findByExternalId(string $externalId, string $sourceCode, string $languageId)
Definition cache.php:104
createIdIndex(int $locationId, string $languageId)
Definition cache.php:79