forked from PWDDEV/developerTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetListCache.php
More file actions
87 lines (66 loc) · 2.28 KB
/
GetListCache.php
File metadata and controls
87 lines (66 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class GetListCache
{
public function GetList($select,$filter,$cachetime=6000){
if(empty($filter))
return array(
'result' => 'error',
'data' => 'Заполнить фильтр',
);
$arReturn = array(
'result' => 'error',
'data' => 'Неправильный запрос',
);
$code=md5(serialize(array_merge($select,$filter)));
$cache = new CPHPCache();
// Устанавливаем время, на которое нужно кешировать
$cache_time = $cachetime;
// Уникальный ID
$cache_id = 'sectCache'.$code;
// Папка для хранения кеша
$cache_path = '/sectCache/sicache/'.$code;
// Проверяем существование кеша
if ( $cache->InitCache($cache_time, $cache_id, $cache_path) )
{
// Кеш есть, достаем данные
$arCacheData = $cache->GetVars();
$arReturn['result']="success";
// Достаем по ключу, с которым положили
$arReturn['data'] = $arCacheData['sectCache'.$sCode];
}
else
{
// Кеша нет, нужно получить данные
// Хороший тон - проверить, подключен ли модуль
if (\CModule::IncludeModule("iblock") )
{
if(empty($select))
$select= array('ID', 'NAME', 'XML_ID');
// Массив элементов
$arItems = [];
// Получаем данные
$res = \CIBlockElement::GetList(["ID"=>"DESC"], $filter, false, false, $select);
while($ob = $res->GetNextElement()){
$arFields = $ob->GetFields();
$arProps = $ob->GetProperties();
$arFields["PROPERTIES"]=$arProps;
$arItems[ $arFields['ID'] ] = $arFields;
}
// Сохраняем данные в кеше
$cache->StartDataCache($cache_time, $cache_id, $cache_path);
$cache->EndDataCache(['sectCache'.$sCode => $arItems]);
$arReturn['result']="success";
$arReturn['data'] = $arItems;
}
else
{
$arReturn['data'] = "Модуль инфоблоков не подключен";
}
}
return $arReturn;
}
public function ClearCache(){
$obCache = new CPHPCache();
$obCache->CleanDir("/sectCache/sicache");
return true;
}
}