1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class SnCache |
5
|
|
|
* |
6
|
|
|
* Permanent cache for classSupernova |
7
|
|
|
*/ |
8
|
|
|
class SnCache { |
9
|
|
|
/** |
10
|
|
|
* Кэш данных - юзера, планеты, юниты, очередь, альянсы итд |
11
|
|
|
* |
12
|
|
|
* @var array $data |
13
|
|
|
*/ |
14
|
|
|
protected $data = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Кэширует соответствия между расположением объектов - в частности юнитов и очередей |
18
|
|
|
* |
19
|
|
|
* Массив $locator - хранит отношения между записями для быстрого доступа по тип_записи:тип_локации:ид_локации:внутренний_ид_записи=>информация |
20
|
|
|
* Для LOC_UNIT внутренний ИД - это SNID, а информация - это ссылка на запись `unit` |
21
|
|
|
* Для LOC_QUE внутренний ИД - это тип очереди, а информация - массив ссылок на `que` |
22
|
|
|
* |
23
|
|
|
* @var array $locator |
24
|
|
|
*/ |
25
|
|
|
protected $locator = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Кэш запросов |
29
|
|
|
* |
30
|
|
|
* @var array $queries |
31
|
|
|
*/ |
32
|
|
|
protected $queries = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Информация о блокировках |
36
|
|
|
* |
37
|
|
|
* @var array $locks |
38
|
|
|
*/ |
39
|
|
|
protected $locks = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var db_mysql $db |
43
|
|
|
*/ |
44
|
|
|
protected $db; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* SnDbCachedOperator constructor. |
48
|
|
|
* |
49
|
|
|
* @param \Common\GlobalContainer $gc |
50
|
|
|
* @param db_mysql $db |
51
|
|
|
*/ |
52
|
|
|
public function __construct($gc, $db) { |
|
|
|
|
53
|
|
|
$this->db = $db; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Repacking data for $location_type |
59
|
|
|
* |
60
|
|
|
* @param int $location_type |
61
|
|
|
* @param int $record_id |
62
|
|
|
*/ |
63
|
|
|
public function cache_repack($location_type, $record_id = 0) { |
64
|
|
|
// Если есть $user_id - проверяем, а надо ли перепаковывать? |
65
|
|
|
if ($record_id && isset($this->data[$location_type][$record_id]) && $this->data[$location_type][$record_id] !== null) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
HelperArray::array_repack($this->data[$location_type]); |
70
|
|
|
HelperArray::array_repack($this->locator[$location_type], 3); // TODO У каждого типа локации - своя глубина!!!! Но можно и глубже ??? |
71
|
|
|
HelperArray::array_repack($this->queries[$location_type], 1); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function cache_clear($location_type, $hard = true) { |
75
|
|
|
if ($hard && !empty($this->data[$location_type])) { |
76
|
|
|
// Здесь нельзя делать unset - надо записывать NULL, что бы это отразилось на зависимых записях |
77
|
|
|
// TODO - replace with setNull |
78
|
|
|
array_walk($this->data[$location_type], 'setNull'); |
79
|
|
|
} |
80
|
|
|
$this->locator[$location_type] = array(); |
81
|
|
|
$this->queries[$location_type] = array(); |
82
|
|
|
$this->cache_repack($location_type); // Перепаковываем внутренние структуры, если нужно |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// TODO - UNUSED ???????????? |
|
|
|
|
86
|
|
|
public function cache_clear_all($hard = true) { |
87
|
|
|
if ($hard) { |
88
|
|
|
$this->data = array(); |
89
|
|
|
static::cache_lock_unset_all(); |
90
|
|
|
} |
91
|
|
|
$this->locator = array(); |
92
|
|
|
$this->queries = array(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function cache_isset($location_type, $record_id) { |
96
|
|
|
return isset($this->data[$location_type][$record_id]) && $this->data[$location_type][$record_id] !== null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// TODO - UNUSED ???????????? |
|
|
|
|
100
|
|
|
public function cache_get($location_type, $record_id) { |
101
|
|
|
return isset($this->data[$location_type][$record_id]) ? $this->data[$location_type][$record_id] : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* Кэшируем запись в соответствующий кэш |
105
|
|
|
|
106
|
|
|
Писать в кэш: |
107
|
|
|
1. Если записи не существует в кэше |
108
|
|
|
2. Если стоит $force_overwrite |
109
|
|
|
3. Если во время транзакции существующая запись не заблокирована |
110
|
|
|
|
111
|
|
|
Блокировать запись: |
112
|
|
|
1. Если идет транзакция и запись не заблокирована |
113
|
|
|
2. Если не стоит скип-лок |
114
|
|
|
*/ |
115
|
|
|
public function cache_set($location_type, $record, $force_overwrite = false, $skip_lock = false) { |
116
|
|
|
// нет идентификатора - выход |
117
|
|
|
if (!($record_id = $record[SnDbCachedOperator::$location_info[$location_type][P_ID]])) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$in_transaction = $this->db->getTransaction()->check(false); |
122
|
|
|
if ( |
123
|
|
|
$force_overwrite |
124
|
|
|
|| |
125
|
|
|
// Не заменяются заблокированные записи во время транзакции |
126
|
|
|
($in_transaction && !$this->cache_lock_get($location_type, $record_id)) |
127
|
|
|
|| |
128
|
|
|
!$this->cache_isset($location_type, $record_id) |
129
|
|
|
) { |
130
|
|
|
$this->data[$location_type][$record_id] = $record; |
131
|
|
|
if ($in_transaction && !$skip_lock) { |
132
|
|
|
$this->cache_lock_set($location_type, $record_id); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function queryCacheSetByFilter($location_type, $filter, $record_id) { |
138
|
|
|
$this->queries[$location_type][$filter][$record_id] = &$this->getDataRefByLocationAndId($location_type, $record_id); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function cache_unset($cache_id, $safe_record_id) { |
142
|
|
|
// $record_id должен быть проверен заранее ! |
143
|
|
|
if (isset($this->data[$cache_id][$safe_record_id]) && $this->data[$cache_id][$safe_record_id] !== null) { |
144
|
|
|
// Выставляем запись в null |
145
|
|
|
$this->data[$cache_id][$safe_record_id] = null; |
146
|
|
|
// Очищаем кэш мягко - что бы удалить очистить связанные данные - кэш локаций и кэш запоросов и всё, что потребуется впредь |
147
|
|
|
$this->cache_clear($cache_id, false); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function cache_lock_get($location_type, $record_id) { |
152
|
|
|
return isset($this->locks[$location_type][$record_id]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function cache_lock_set($location_type, $record_id) { |
156
|
|
|
return $this->locks[$location_type][$record_id] = true; // Не всегда - от результата |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// TODO - UNUSED ???????????? |
|
|
|
|
160
|
|
|
public function cache_lock_unset($location_type, $record_id) { |
161
|
|
|
if (isset($this->locks[$location_type][$record_id])) { |
162
|
|
|
unset($this->locks[$location_type][$record_id]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return true; // Не всегда - от результата |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function cache_lock_unset_all() { |
169
|
|
|
// Когда будем работать с xcache - это понадобиться, что бы снимать в xcache блокировки с записей |
170
|
|
|
// Пройти по массиву - снять блокировки для кэшера в памяти |
171
|
|
|
$this->locks = array(); |
172
|
|
|
|
173
|
|
|
return true; // Не всегда - от результата |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getData($locationType = LOC_NONE) { |
177
|
|
|
return $locationType == LOC_NONE ? $this->data : $this->data[$locationType]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function cacheUnsetElement($locationType, $recordId) { |
181
|
|
|
$this->data[$locationType][$recordId] = null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function isArrayLocation($locationType) { |
185
|
|
|
return is_array($this->data[$locationType]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return reference to record in $data by locationType and recordId |
190
|
|
|
* |
191
|
|
|
* @param int $locationType |
192
|
|
|
* @param int $recordId |
193
|
|
|
* |
194
|
|
|
* @return &mixed |
|
|
|
|
195
|
|
|
*/ |
196
|
|
|
public function &getDataRefByLocationAndId($locationType, $recordId) { |
197
|
|
|
return $this->data[$locationType][$recordId]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// TODO UNUSED ???? |
|
|
|
|
201
|
|
|
public function setUnitLocator($unit, $unit_id) { |
202
|
|
|
if (is_array($unit)) { |
203
|
|
|
$this->locator[LOC_UNIT][$unit['unit_location_type']][$unit['unit_location_id']][$unit['unit_snid']] = &$this->getDataRefByLocationAndId(LOC_UNIT, $unit_id); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getUnitLocator($location_type, $location_id, $unit_snid) { |
208
|
|
|
return $unit_snid ? $this->locator[LOC_UNIT][$location_type][$location_id][$unit_snid] : $this->locator[LOC_UNIT][$location_type][$location_id]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getUnitLocatorByFullLocation($location_type, $location_id) { |
212
|
|
|
return is_array($this->locator[LOC_UNIT][$location_type][$location_id]) ? $this->locator[LOC_UNIT][$location_type][$location_id] : array(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data) { |
216
|
|
|
$this->locator[LOC_UNIT][$location_type][$location_id][$unit_data['unit_snid']] = &$this->data[LOC_UNIT][$unit_data['unit_id']]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function isUnitLocatorNotSet($location_type, $location_id) { |
220
|
|
|
return !isset($this->locator[LOC_UNIT][$location_type][$location_id]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function locatorReset() { |
224
|
|
|
$this->locator = array(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function queriesReset() { |
228
|
|
|
$this->queries = array(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getQueries() { |
232
|
|
|
return $this->queries; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getLocks() { |
236
|
|
|
return $this->locks; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getQueriesByLocationAndFilter($locationType, $filter) { |
240
|
|
|
return !empty($this->queries[$locationType][$filter]) && is_array($this->queries[$locationType][$filter]) ? $this->queries[$locationType][$filter] : array(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function isQueryCacheByLocationAndFilterEmpty($locationType, $filter) { |
244
|
|
|
return !isset($this->queries[$locationType][$filter]) || $this->queries[$locationType][$filter] === null; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function queryCacheResetByLocationAndFilter($locationType, $filter) { |
248
|
|
|
$this->queries[$locationType][$filter] = array(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.