1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
cache.php
См. документацию.
1<?
2namespace Sale\Handlers\Delivery\Additional;
3
4use \Bitrix\Main\Application;
5use Bitrix\Main\ArgumentOutOfRangeException;
6use Bitrix\Main\Config\Option;
7
13class Cache
14{
15 protected $ttl = 0;
16 protected $cacheIdBase = '';
17
19 protected static $cacheManager = null;
20
26 public function __construct($type, $ttl)
27 {
28 if(static::$cacheManager === null)
29 static::$cacheManager = Application::getInstance()->getManagedCache();
30
31 $this->cacheIdBase = self::createCacheId($type);
32 $this->ttl = $ttl;
33 }
34
39 public function get(array $ids = array())
40 {
41 $result = false;
42 $cacheId = $this->getCacheId($ids);
43
44 if(static::$cacheManager->read($this->ttl, $this->cacheIdBase))
45 {
46 $res = static::$cacheManager->get($this->cacheIdBase);
47
48 if(!empty($res[$cacheId]))
49 $result = $res[$cacheId];
50 }
51
52 return $result;
53 }
54
55 public function getAll()
56 {
57 $result = array();
58
59 if(static::$cacheManager->read($this->ttl, $this->cacheIdBase))
60 $result = static::$cacheManager->get($this->cacheIdBase);
61
62 return $result;
63 }
64
69 public function set($value, array $ids)
70 {
71 $cached = false;
72
73 if(static::$cacheManager->read($this->ttl, $this->cacheIdBase))
74 $cached = static::$cacheManager->get($this->cacheIdBase);
75
76 if(!is_array($cached))
77 $cached = array();
78
79 $cacheId = $this->getCacheId($ids);
80 $cached[$cacheId] = $value;
81
82 static::$cacheManager->set($this->cacheIdBase, $cached);
83 }
84
88 public function clean()
89 {
90 static::$cacheManager->clean($this->cacheIdBase);
91 }
92
97 protected function getCacheId(array $ids = array())
98 {
99 $result = "cachePrefixIdx";
100
101 if (!empty($ids))
102 {
103 foreach (array_keys($ids) as $index)
104 {
105 if (is_array($ids[$index]))
106 {
107 $ids[$index] = serialize($ids[$index]);
108 }
109 }
110
111 $result .= implode('_', $ids);
112 }
113
114 return $result;
115 }
116
121 protected static function createCacheId($type)
122 {
123 return 'SALE_HNDL_DELV_ADD_'.$type;
124 }
125}
126
134class CacheSession extends Cache
135{
136 public function __construct($type, $ttl)
137 {
138 $this->cacheIdBase = self::createCacheId($type);
139 }
140
145 public function set($value, array $ids)
146 {
147 $cacheId = $this->getCacheId($ids);
148
149 if (!isset($_SESSION[$this->cacheIdBase]))
150 {
151 $_SESSION[$this->cacheIdBase] = [];
152 }
153
154 $_SESSION[$this->cacheIdBase][$cacheId] = $value;
155 }
156
161 public function get(array $ids = array())
162 {
163 $result = false;
164 $cacheId = $this->getCacheId($ids);
165
166 if(isset($_SESSION[$this->cacheIdBase][$cacheId]))
167 $result = $_SESSION[$this->cacheIdBase][$cacheId];
168
169 return $result;
170 }
171
175 public function clean(array $ids = array())
176 {
177 $cacheId = $this->getCacheId($ids);
178 unset($_SESSION[$this->cacheIdBase][$cacheId]);
179 }
180}
181
186class CacheManager
187{
188 protected static $items = array();
189
190 const TYPE_NONE = 0;
191 const TYPE_PROFILES_LIST = 1;
192 const TYPE_DELIVERY_FIELDS = 2;
193 const TYPE_DELIVERY_PRICE = 3;
194 const TYPE_PROFILE_FIELDS = 4;
195 const TYPE_DELIVERY_LIST = 5;
196 const TYPE_PROFILE_CONFIG = 6;
197 const TYPE_EXTRA_SERVICES = 7;
198
199 // types of cache
200 const LOC_CACHE = 1;
201 const LOC_SESSION = 2;
202
203 private const DISABLE_CACHE_OPTION = 'hndl_dlv_add_cache_disable';
204
205 //Possible cache types & some params
206 protected static $types = array(
207 self::TYPE_PROFILES_LIST => array('TTL' => 2419200, 'LOC' => self::LOC_CACHE), // month cache
208 self::TYPE_DELIVERY_FIELDS => array('TTL' => 2419200, 'LOC' => self::LOC_CACHE), // month cache
209 self::TYPE_DELIVERY_PRICE => array('TTL' => 0, 'LOC' => self::LOC_SESSION), // session
210 self::TYPE_PROFILE_FIELDS => array('TTL' => 2419200, 'LOC' => self::LOC_CACHE), // month cache
211 self::TYPE_DELIVERY_LIST => array('TTL' => 2419200, 'LOC' => self::LOC_CACHE), // month cache
212 self::TYPE_PROFILE_CONFIG => array('TTL' => 0, 'LOC' => self::LOC_SESSION), // session
213 self::TYPE_EXTRA_SERVICES => array('TTL' => 2419200, 'LOC' => self::LOC_CACHE), // month cache
214 );
215
221 public static function getItem($type)
222 {
223 if($type == self::TYPE_NONE)
224 return null;
225
226 if(empty(self::$types[$type]))
227 return null;
228
229 if (
230 defined('SALE_HNDL_DLV_ADD_CACHE_DISABLE')
231 || (int)Option::get('sale', self::DISABLE_CACHE_OPTION, 0) == 1
232 )
233 {
234 return null;
235 }
236
237 if(empty(self::$items[$type]))
238 {
239 if(self::$types[$type]['LOC'] == self::LOC_CACHE)
240 self::$items[$type] = new Cache($type, self::$types[$type]['TTL']);
241 elseif(self::$types[$type]['LOC'] == self::LOC_SESSION)
242 self::$items[$type] = new CacheSession($type, self::$types[$type]['TTL']);
243 }
244
245 return self::$items[$type];
246 }
247
248 public static function cleanAll()
249 {
250 foreach(self::$types as $typeId => $params)
251 {
252 $cache = self::getItem($typeId);
253 $cache->clean();
254 }
255 }
256
257 public static function getAll()
258 {
259 $result = array();
260
261 foreach(self::$types as $typeId => $params)
262 {
263 $cache = self::getItem($typeId);
264 $result[$typeId] = $cache->getAll();
265 }
266
267 return $result;
268 }
269}
$type
Определения options.php:106
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$value
Определения Param.php:39
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799