From 96b9286fe2ecd0feae84cb221da64ae4d3b6fe41 Mon Sep 17 00:00:00 2001 From: gidrocyfal <53264501+gidrocyfal@users.noreply.github.com> Date: Wed, 24 Jul 2019 17:02:02 +0300 Subject: [PATCH 1/2] Create GetListCache.php --- GetListCache.php | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 GetListCache.php diff --git a/GetListCache.php b/GetListCache.php new file mode 100644 index 0000000..8d3281b --- /dev/null +++ b/GetListCache.php @@ -0,0 +1,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; + } + +} From db0d25e87308088310499ce0e01bf76c287756b2 Mon Sep 17 00:00:00 2001 From: gidrocyfal <53264501+gidrocyfal@users.noreply.github.com> Date: Wed, 24 Jul 2019 17:21:20 +0300 Subject: [PATCH 2/2] Create getLastNews.php --- getLastNews.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 getLastNews.php diff --git a/getLastNews.php b/getLastNews.php new file mode 100644 index 0000000..7de378e --- /dev/null +++ b/getLastNews.php @@ -0,0 +1,30 @@ +class GetLastNews{ + + public function GetNews($count=5,$json=false){ + + if(!$json){ + $cn=0; + $xml=simplexml_load_file("https://lenta.ru/rss", null, LIBXML_NOCDATA); + foreach($xml->channel->item as $item){ + + if($cn<$count){ + $txt.= '
'.$item->title.'
'.$item->description.'
'; + } + $cn++; + } + return $txt; + }else{ + $cn=0; + $xml=simplexml_load_file("https://lenta.ru/rss", null, LIBXML_NOCDATA); + foreach($xml->channel->item as $item){ + + if($cn<$count){ + $txt[]=array("link"=>$item->link,"title"=>$item->title,"description"=>$item->description); + } + $cn++; + } + return json_encode($txt); + } + } + +}