Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CacheManager.php
1<?php
2
4
6
8{
9 public const GENERAL_PRESET = 'generalPreset';
10 public const NOTIFY_PRESET = 'notifyPreset';
11
12 private const CACHE_TTL = 31536000; //one year
13 private const BASE_CACHE_DIR = '/im/settings/';
14 private const USER_MODE = 'user';
15 private const PRESET_MODE = 'preset';
16
17 private static array $staticCache = [];
18
19
20 private string $mode;
21 private int $entityId;
22 private Cache $cache;
23
24 public static function getUserCache(?int $userId = null): self
25 {
26 return new static(self::USER_MODE, $userId);
27 }
28
29 public static function getPresetCache(?int $presetId = null): self
30 {
31 return new static(self::PRESET_MODE, $presetId);
32 }
33
38 private function __construct(string $mode, ?int $entityId = null)
39 {
40 $this->mode = $mode;
41
42 if ($entityId !== null)
43 {
44 $this->setEntityId($entityId);
45 }
46
47 $this->cache = Cache::createInstance();
48 }
49
50
51 public function setEntityId(int $entityId): self
52 {
53 $this->entityId = $entityId;
54
55 return $this;
56 }
57
68 public function getValue(): array
69 {
70 $result = [];
71 $cacheName = $this->getCacheName();
72 if (isset(static::$staticCache[$cacheName]))
73 {
74 return static::$staticCache[$cacheName];
75 }
76 if ($this->cache->initCache(self::CACHE_TTL, $cacheName, $this->getCacheDir()))
77 {
78 $result = $this->cache->getVars();
79 static::$staticCache[$cacheName] = $result;
80 }
81
82 return $result;
83 }
84
85 public function setValue(array $value): self
86 {
87 $cacheName = $this->getCacheName();
88
89 $this->cache->clean($cacheName, $this->getCacheDir());
90
91 $this->cache->initCache(self::CACHE_TTL, $cacheName, $this->getCacheDir());
92 $this->cache->startDataCache();
93 $this->cache->endDataCache($value);
94 static::$staticCache[$cacheName] = $value;
95
96 return $this;
97 }
98
99 public function clearCache(): self
100 {
101 $cacheName = $this->getCacheName();
102 $this->cache->clean($cacheName, $this->getCacheDir());
103 unset(static::$staticCache[$cacheName]);
104
105 return $this;
106 }
107
108 public function clearAll(): self
109 {
110 $this->cache->cleanDir($this->getCacheDir());
111 static::$staticCache = [];
112
113 return $this;
114 }
115
116 private function getCacheName(): string
117 {
118 return $this->mode . '_' . $this->entityId . '_v2';
119 }
120
121 private function getCacheDir(): string
122 {
123 return self::BASE_CACHE_DIR . $this->mode . '/';
124 }
125
126}
static getUserCache(?int $userId=null)
static getPresetCache(?int $presetId=null)