Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
gridstatestorage.php
1<?php
2
4
6
11{
12 private const FIELD_CURRENT_PAGE = 'currentPage';
13 private const FIELD_DEADLINE = 'deadline';
14
15 private const DEADLINE_SEC = 60;
16
25 private function getStorageKey(int $productId, string $gridId): string
26 {
27 return "{$productId}_{$gridId}";
28 }
29
38 public function load(int $productId, string $gridId): GridState
39 {
40 $state = new GridState($productId, $gridId);
41 $key = $this->getStorageKey($productId, $gridId);
42 $cache = Application::getInstance()->getLocalSession($key);
43
44 // postponed every minute so that the pages do not stick forever.
45 $deadline = (int)$cache->get(self::FIELD_DEADLINE);
46 if ($deadline < time())
47 {
48 $cache->set(self::FIELD_DEADLINE, time() + self::DEADLINE_SEC);
49 }
50 else
51 {
52 $currentPage = $cache->get(self::FIELD_CURRENT_PAGE);
53 if (isset($currentPage))
54 {
55 $state->setCurrentPage((int)$currentPage);
56 }
57 }
58
59 return $state;
60 }
61
69 public function save(GridState $state): void
70 {
71 $key = $this->getStorageKey($state->getProductId(), $state->getGridId());
72 $cache = Application::getInstance()->getLocalSession($key);
73 $cache->set(self::FIELD_CURRENT_PAGE, $state->getCurrentPage());
74 $cache->set(self::FIELD_DEADLINE, time() + self::DEADLINE_SEC);
75 }
76}