15 private int $flagCounted = 0;
16 private int $flagCleared = 0;
19 private const CACHE_PREFIX =
'sonet_scorer_cache_';
20 private const CACHE_TTL = 10 * 60;
21 private const CACHE_DIR =
'/sonet/counterstate';
23 private const FLAGS = [
30 $this->userId = $userId;
31 $this->fetchCounters();
36 return in_array($type, self::FLAGS,
true);
46 return count($this->rows);
51 return (
bool) $this->flagCounted;
56 return $this->flagCleared;
61 $cache = Cache::createInstance();
62 $cache->clean($this->getCacheTag(), $this->getCacheDir());
65 private function getCacheDir(): string
67 return self::CACHE_DIR .
'/' . substr(md5($this->userId),2,2) .
'/';
70 private function getCacheTag(): string
72 return self::CACHE_PREFIX . $this->userId;
75 private function fetchCounters(): void
80 $this->rows = $this->getFlags();
84 $query = CounterTable::query()
91 ->where(
'USER_ID', $this->userId);
96 $rowsFlag = $this->getFlags();
97 $query->setLimit($limit);
100 $this->rows = $query->exec()->fetchAll();
101 if (!is_null($rowsFlag))
103 $this->rows = array_merge($this->rows, $rowsFlag);
107 private function getFlags(): array
110 $cache = Cache::createInstance();
112 if ($cache->initCache(self::CACHE_TTL, $this->getCacheTag(), $this->getCacheDir()))
114 $rows = $cache->getVars();
118 $rows = CounterTable::query()
125 ->where(
'USER_ID', $this->userId)
126 ->whereIn(
'TYPE', self::FLAGS)
133 $taggedCache->StartTagCache($this->getCacheDir());
134 $taggedCache->RegisterTag($this->getCacheTag());
136 $cache->startDataCache();
137 $cache->endDataCache($rows);
138 $taggedCache->EndTagCache();
142 foreach ($rows as $row)
144 switch ($row[
'TYPE'])
147 $this->flagCounted = (int) $row[
'VALUE'];
150 $this->flagCleared = (int) $row[
'VALUE'];