Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Cache.php
1<?php
2
4
5use \Bitrix\Main\Data;
6
7class Cache
8{
9 public const CACHE_ID = 'socialnetwork-toolbar-composition-';
10 private const TTL = 86400;
11 private const DIRECTORY = 'socialnetwork';
12
13 private Data\Cache $cache;
14
15 public function __construct(private int $userId, private int $spaceId)
16 {
17 $this->init();
18 }
19
20 public function store(array $data): void
21 {
22 $this->cache->forceRewriting(true);
23 $this->cache->startDataCache();
24 $this->cache->endDataCache($data);
25 }
26
27 public function get(): array
28 {
29 if (!$this->initCache())
30 {
31 return [];
32 }
33 $variables = $this->cache->getVars();
34 return is_array($variables) ? $variables : [];
35 }
36
37 public function initCache(): bool
38 {
39 return $this->cache->initCache(static::TTL, $this->getCacheId(), static::DIRECTORY);
40 }
41
42 private function init(): void
43 {
44 $this->cache = Data\Cache::createInstance();
45 $this->initCache();
46 }
47
48 private function getCacheId(): string
49 {
50 return static::CACHE_ID . $this->userId . '-' . $this->spaceId;
51 }
52}
initCache($ttl, $uniqueString, $initDir=false, $baseDir='cache')
Definition cache.php:271
__construct(private int $userId, private int $spaceId)
Definition Cache.php:15