The class functions as a global cache container where you can store data
that is reused by the application. The cache content is not persisted, so
once the process is completed, the data is gone.
Usage:
public function getResult()
{
if (!Cache::has('result'))
{
Cache::set('result') = $this->complexMethod();
}
return Cache::get('result');
}
/** * Get the details of a page including inherited parameters * * @param mixed $intId A page ID or a Model object * * @return \PageModel The page model or null * * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0. * Use PageModel::findWithDetails() or PageModel->loadDetails() instead. */ public static function getPageDetails($intId) { trigger_error('Using Controller::getPageDetails() has been deprecated and will no longer work in Contao 5.0. Use PageModel::findWithDetails() or PageModel->loadDetails() instead.', E_USER_DEPRECATED); if ($intId instanceof \PageModel) { return $intId->loadDetails(); } elseif ($intId instanceof \Model\Collection) { /** @var \PageModel $objPage */ $objPage = $intId->current(); return $objPage->loadDetails(); } elseif (is_object($intId)) { $strKey = __METHOD__ . '-' . $intId->id; // Try to load from cache if (\Cache::has($strKey)) { return \Cache::get($strKey); } // Create a model from the database result $objPage = new \PageModel(); $objPage->setRow($intId->row()); $objPage->loadDetails(); \Cache::set($strKey, $objPage); return $objPage; } else { // Invalid ID if (!strlen($intId) || $intId < 1) { return null; } $strKey = __METHOD__ . '-' . $intId; // Try to load from cache if (\Cache::has($strKey)) { return \Cache::get($strKey); } $objPage = \PageModel::findWithDetails($intId); \Cache::set($strKey, $objPage); return $objPage; } }
/** * Return calculated price for this shipping method * @param IsotopeProductCollection * @return float */ protected function getLiveRateQuote(IsotopeProductCollection $objCollection) { $fltPrice = 0.0; //get a hash for the cache $strHash = static::makeHash($objCollection, array($this->ups_enabledService)); if (!Cache::has($strHash)) { //Build shipment $Shipment = $this->buildShipmentFromCollection($objCollection); //Get Iso Config $Config = Isotope::getConfig(); //UPS Rate Object $UPS = new Ups_Rate($Config->UpsAccessKey, $Config->UpsUsername, $Config->UpsPassword, $Config->UpsMode == 'test' ? true : false); try { $objResponse = $UPS->getRate($Shipment); $fltPrice = (double) $objResponse->RatedShipment[0]->TotalCharges->MonetaryValue; } catch (\Exception $e) { //@!TODO post error message } Cache::set($strHash, $fltPrice); } return Cache::get($strHash); }
/** * Return calculated price for this shipping method * @param IsotopeProductCollection * @return float */ protected function getLiveRateQuote(IsotopeProductCollection $objCollection) { $fltPrice = 0.0; //get a hash for the cache $strService = $this->fedex_enabledService ?: 'FEDEX_GROUND'; $strPackagingType = $this->fedex_PackingService ?: 'YOUR_PACKAGING'; $strHash = static::makeHash($objCollection, array($strService, $strPackagingType)); if (!Cache::has($strHash)) { $arrReturn = $this->buildShipment($objCollection); if (empty($arrReturn) || count($arrReturn) != 3) { Cache::set($strHash, $fltPrice); return Cache::get($strHash); } list($arrOrigin, $arrDestination, $arrShipment) = $arrReturn; //Cache the request so we don't have to run it again as the API is slow $strRequestHash = md5(implode('.', $arrDestination) . $arrShipment['service'] . $arrShipment['weight'] . implode('.', $this->Shipment['productids'])); // Construct FEDEX Object: For now, Origin is assumed to be the same for origin and shipping info $objFEDEXAPI = new FedExAPIRatesAndService($arrShipment, $arrOrigin, $arrOrigin, $arrDestination); $strRequestXML = $objFEDEXAPI->buildRequest('RatingServiceSelectionRequest'); // What the...? unset($_SESSION['CHECKOUT_DATA']['FEDEX'][$strRequestHash]); if ($_SESSION['CHECKOUT_DATA']['FEDEX'][$strRequestHash]) { $arrResponse = $_SESSION['CHECKOUT_DATA']['FEDEX'][$strRequestHash]; } else { $arrResponse = $objFEDEXAPI->sendRequest($strRequestXML); $_SESSION['CHECKOUT_DATA']['FEDEX'][$strRequestHash] = $arrResponse; } if ($arrResponse['RateReply']['Notifications']['Severity'] == 'SUCCESS') { $fltPrice = floatval($arrResponse['RateReply']['RateReplyDetails']['RatedShipmentDetails'][0]['ShipmentRateDetail']['TotalNetCharge']['Amount']); } else { $strLogMessage = sprintf('Error in shipping digest: %s - %s', $arrResponse['RatingServiceSelectionResponse']["Response"]["ResponseStatusDescription"], $arrResponse['RatingServiceSelectionResponse']["Response"]["Error"]["ErrorDescription"]); $strMessage = sprintf('%s - %s', $arrResponse['RatingServiceSelectionResponse']["Response"]["ResponseStatusDescription"], $arrResponse['RatingServiceSelectionResponse']["Response"]["Error"]["ErrorDescription"]); log_message($strLogMessage, 'error.log'); //$_SESSION['ISO_ERROR'][] = $strMessage; //\System::log($strLogMessage, __METHOD__, TL_ERROR); } Cache::set($strHash, $fltPrice); } return Cache::get($strHash); }
/** * * {{fmField::fm_tablename::8::title}} * * @param $arrSplit * @return string */ private function generateField($arrSplit) { if ($arrSplit[1] && $arrSplit[2] && $arrSplit[3]) { $tablename = $arrSplit[1]; $tableData = $tablename . '_data'; $id = $arrSplit[2]; $field = $arrSplit[3]; $cacheID = md5($arrSplit[1] . $arrSplit[2]); // check if table exist if (!$this->Database->tableExists($tableData)) { return 'table does not exist'; } // get field from cache $cachedItem = Cache::get($cacheID); if ($cachedItem) { return $this->getField($cachedItem, $field); } // get data $qProtectedStr = 'AND published = "1"'; // check for preview mode if (HelperModel::previewMode()) { $qProtectedStr = ''; } // q $itemDB = $this->Database->prepare('SELECT * FROM ' . $tableData . ' WHERE id = ? OR alias = ?' . $qProtectedStr . ' LIMIT 1')->execute((int) $id, $id); if (!$itemDB->count()) { return 'no item found'; } $item = $this->parseItem($itemDB, $tablename); // find and set map $moduleDB = $this->Database->prepare('SELECT tl_fmodules.id AS moduleID, tl_fmodules.*, tl_fmodules_filters.* FROM tl_fmodules LEFT JOIN tl_fmodules_filters ON tl_fmodules.id = tl_fmodules_filters.pid WHERE tablename = ? ORDER BY tl_fmodules_filters.sorting')->execute($tablename); $maps = array(); $widgets = array(); // while while ($moduleDB->next()) { $moduleInputFields = $moduleDB->row(); // get map if ($moduleInputFields['type'] == 'map_field') { $maps[] = $this->findMapAndSet($moduleInputFields); } if ($moduleInputFields['type'] == 'widget') { $widgets[] = $this->findWidgetAndSet($moduleInputFields); } // has options if ($moduleInputFields['type'] == 'simple_choice' || $moduleInputFields['type'] == 'multi_choice') { $dcaHelper = new DCAHelper(); // später durch statische methode austauschen! $arrCleanOptions[$moduleInputFields['fieldID']] = $dcaHelper->getOptions($moduleInputFields, $tablename, $item['pid']); } } // parse map if (!empty($maps)) { foreach ($maps as $map) { $objMapTemplate = new FrontendTemplate($map['template']); $item['mapSettings'] = $map; $objMapTemplate->setData($item); $item[$map['fieldID']] = $objMapTemplate->parse(); } } // field if (!empty($widgets)) { $arrayAsValue = array('list.blank', 'list.keyValue', 'table.blank'); foreach ($widgets as $widget) { $id = $widget['fieldID']; $tplName = $widget['widgetTemplate']; $type = $widget['widgetType']; $value = $item[$id]; if (in_array($type, $arrayAsValue)) { $value = deserialize($value); } // replace insertTags in array if (is_array($value)) { $value = HelperModel::replaceInsertTagsInArray($value); } // replace insertTags in string if (is_string($value)) { $value = $this->replaceInsertTags($value); } $objFieldTemplate = new FrontendTemplate($tplName); $objFieldTemplate->setData(array('value' => $value, 'type' => $type, 'item' => $item)); $item[$id . 'AsTemplate'] = $objFieldTemplate->parse(); } } // set clean options if (!empty($arrCleanOptions)) { // overwrite clean options foreach ($arrCleanOptions as $fieldID => $options) { if ($item[$fieldID] && is_string($item[$fieldID])) { $arrValues = explode(',', $item[$fieldID]); $tempValue = array(); if (is_array($arrValues)) { foreach ($arrValues as $val) { $tempValue[] = $options[$val]; } } $item[$fieldID . 'ShowLabels'] = implode(', ', $tempValue); } } } Cache::set($cacheID, $item); return $this->getField($item, $field); } return 'no valid arguments'; }