douchen5971 2014-01-06 15:52
浏览 25
已采纳

ModX革命缓存动态生成的占位符

How can I cache (using ModX's cacheManager), the dynamic placeholders i am generating here:

// recursive function to generate our un-ordered list of menu items
if(!function_exists('GenerateMenu')){
    function GenerateMenu($level, $parent = 0, $wc, $wi, $we, $cs, $sc, $cl){
        try {
            $lvl = ++$level;
            global $modx;
            // Check for #1, should this be cached, #2 does it already exist in the cache
            $cached = $modx->cacheManager->get('Navigator');
            if($sc && isset($cached)){
                // need to get the placeholders from cache - here somehow!
                return $cached;
            }else{
                // get the site start
                $siteStartId = $modx->getOption('site_start');
                // Set our initial rows array
                $rows = array();
                // Run our query to get our menu items
                $sql = 'Select `id`, `menutitle`, `uri`, `longtitle`, `parent`, `link_attributes`, `class_key`, `content`, `alias`, `introtext`
                                    From `' . $modx->getOption(xPDO::OPT_TABLE_PREFIX) . 'site_content` 
                                    Where `deleted` = 0 AND `hidemenu` = 0 AND `published` = 1 AND `parent` = :parent
                                    Order by `parent`, `menuindex`';
                $query = new xPDOCriteria($modx, $sql, array(':parent' => $parent));
                if ($query->stmt && $query->stmt->execute()) {
                    $rows = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
                }
                // do some cleanup
                unset($query, $sql);
                // make sure we have some rows, and then build our html for the menu
                if($rows){
                    // grab a count of our results
                    $rCt = count($rows);
                    $cls = ($lvl > 1) ? 'sub-item-' . $lvl : 'main-item-' . $lvl;
                    $ret .= '   <ul class="' . $cls . '" id="' . $cls . '-' . $parent . '">' . "
";
                    for($i = 0; $i < $rCt; ++$i){
                        // if this resource is a WebLink, show the content in it, as the URL for the href tag, otherwise, use the resource's URI
                        $url = ($rows[$i]['class_key'] == 'modWebLink') ? $rows[$i]['content'] : '/' . $rows[$i]['uri'];
                        // Check for the site's start id, if true, show a "blank" link, otherwise show the $url
                        $showUrl = ($siteStartId == $rows[$i]['id']) ? '/' : $url;
                        $la = (strlen($rows[$i]['link_attributes']) > 0) ? ' ' . $rows[$i]['link_attributes'] : null;
                        // Set some dynamic placeholders, they can only be used ont he pages that contain this snippet
                        $modx->toPlaceholders(array('Title-' . $rows[$i]['id'] => $rows[$i]['longtitle'],
                                                    'MenuTitle-' . $rows[$i]['id'] => $rows[$i]['menutitle'],
                                                    'URL-' . $rows[$i]['id'] => $showUrl), 
                                                'link');
                        $ret .= '       <li class="' . $cls . '" id="' . $rows[$i]['alias'] . '">' . "
";
                        $ret .= '           <a href="' . $showUrl . '" title="' . $rows[$i]['longtitle'] . '"' . $la . '>' . $rows[$i]['menutitle'] . '</a>' . "
";
                        $ret .= GenerateMenu($lvl, $rows[$i]['id']);
                        // Check for a snippet, and render it
                        $it = $rows[$i]['introtext'];
                        if($cs && IsSnippet($it)){
                            // if we find a snippet in the Summary field, run it, and attach it to our output   
                            preg_match('/\[\[!?(.*)\]\]/', $it, $sm);
                            $ret .= $modx->runSnippet($sm[1]);
                            // clean up
                            unset($sm);
                        }
                        $ret .= '       </li>' . "
";
                    }
                    $ret .= '   </ul>' . "
";
                }
                // clean up
                unset($rows);           
                // Check to see if we should cache it, if so, set it to cache, and apply the length of time it should be cached for: defaults to 2 hours
                if($sc){
                    // NEED TO SET THE PLACEHOLDERS TO CACHE SOMEHOW
                    $modx->cacheManager->set('Navigator', $ret, $cl);
                }
                // return the menu
                return $ret;
            }
        } catch(Exception $e) {
            // If there was an error, make sure to write it out to the modX Error Log
            $modx->log(modX::LOG_LEVEL_ERROR, '[Navigator] Error: ' . $e->getMessage());
            return null;
        }
    }
}
  • 写回答

2条回答 默认 最新

  • donglizhan7848 2015-08-09 21:50
    关注

    The easiest solution may be pdoTools which allows you to establish caching at run time.

    http://www.shawnwilkerson.com/modx/tags/pdotools/

    Also, I do not believe resource placeholders are cached which is the best place to have your items cahced:

                case '+':
                    $tagName= substr($tagName, 1 + $tokenOffset);
                    $element= new modPlaceholderTag($this->modx);
                    $element->set('name', $tagName);
                    $element->setTag($outerTag);
                    $elementOutput= $element->process($tagPropString);
                    break;
    

    From lines 455-461 of https://github.com/modxcms/revolution/blob/master/core/model/modx/modparser.class.php#L455

    You may notice the other tag types have:

    $element->setCacheable($cacheable);
    

    I cover the parser in Appendix D of my book. I found some issues in it in 2011 which Jason corrected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大