Concrete5属性计数



我们用Concrete5建立了一个最初在Joomla开发的站点。我们的工作是把所有的东西都搬过来并具体化。这个网站的主要部分是大约1200个音频教学,每个教学都有不同的属性,如主题、作者、程序、位置等。

有些教义可能指定了多个属性,比如多个关键词或主题。

我想统计一下所有的属性,这样访问者就可以一眼看到某个作者的教学内容,或者某个特定主题的教学内容

  • 伦理(20)
  • 恐惧(42)
  • 感恩(55)

我的原始代码被发现有太多被偷听到的内容,对于这么多的教导和这么多的属性来说是不实用的。基本上,我遍历了每个属性,并根据PageList计数查找总计数。我们讨论的是每加载一页要进行数百次查找。打开缓存在这里似乎没有帮助。

有没有其他策略被证明可以成功地在大量页面上聚合属性计数?

以下是可供参考的网站:http://everydayzen.org/teachings/

我通常说"不要直接访问数据库;使用API",但我认为这里应该使用DB。

查看[Collection|File]SearchIndexAttributes表格。(我不确定教学是文件还是页面。如果是页面,你需要通过仪表板中的作业定期重新索引它们。)查看索引表比加入属性值表中的最新版本要容易得多。一旦看到该表,就可以在SQL中进行一些简单的GROUPing。

如果你想使用API,你可以像今天一样批量使用,进行适当的计算,然后缓存它

缓存没有理由不起作用,但第一次命中(当缓存处于冷态时)当然会占用全部时间。您应该缓存我的IndexAttributes想法(完整的表读取和循环并不是微不足道的),但至少使用一个冷缓存,它应该只需要几分之一秒,而数百个页面列表调用可能需要10秒或更长的时间。

我在Concrete5的工作网站上做了类似的事情,显示了工作所在的每个部门的计数。

即HR(32)、Sales(12)等

这是从帮助程序中获取的代码(这只是包含的相关函数):

<?php
class JobHelper {
/**
* GetDepartmentJobsCount
* Returns array of Department names with job count based on input Pages
* @param Array(Pages) - Result of a PageList->getPages
* @return Array
*/
public function getDepartmentJobsCount($pages) {
    $depts = $this->getDepartments();
    $cj = $this->setCounts($depts);        
    $cj = $this->setAttributeCounts($cj, $pages,'job_department');
    return $cj;
}
/**
* GetDepartments
* Return all available Departments
* @return Array(Page)
*/
public function getDepartmentPages(){
    $pld = new PageList();
    $pld->filterByPath('/working-lv'); //the path that your Teachings all sit under
    $pld->setItemsPerPage(0);
    $res = $this->getPage();
    $depts = array();
    foreach($res as $jp){
        $depts[$jp->getCollectionName()] = $jp;
    }
    ksort($depts);
    return $depts;
}
/**
* PopulateCounts
* Returns array of page names and counts
* @param Array - Array to feed from
* @return Array
*/
public function setCounts($v){
    foreach($v as $w){
        $a[$w]['count'] = 0;
    }
    return $a;
} 
/**
* PopulateCounts
* Returns array of page names, with counts added from attribute, and paths
* @param Array - Array to add counts and paths in to
* @param Array(Pages) - Pages to run through
* @param String - Attribute to also add to counts
* @param String - Optional - Job Search parameter, leave blank to get Page URL
* @return Array
*/
public function setAttributeCounts($cj, $pages, $attr){
    foreach($pages as $p) {
        $pLoc = explode('|',$p->getAttribute($attr));  // Our pages could have multiple departments pipe separated
        foreach($pLoc as $locName){
            $cj[$locName]['count']++;
        }           
    }
    return $cj;
}

然后,您可以从PageList模板中执行以下操作

$jh = Loader::helper('job');
$deptCounts = $jh->getDepartmentJobsCount($pages);
foreach($deptCounts as $dept => $data) { 
    echo $dept . '(' . $data['count] . ')';
}

最新更新