我们用Concrete5构建了一个最初在Joomla开发的站点。我们的工作是把所有的东西都带来,然后把它变成现实。这个网站的主要部分是大约1200个音频教学,每个教学都有不同的属性,如主题、作者、程序、位置等。
一些教导可能有多个属性分配,例如多个关键字或主题。
我想对所有的属性进行计数,以便访问者能够看到某一作者的教导有多少,或者有多少是关于某一特定主题的,即:
我原来的代码被无意中听到了太多,对这么多的教导和许多属性来说都是实用的。基本上,我遍历了每个属性,并根据PageList计数对总计数进行了查找。我们讨论的是每一页加载的数百个查找。在这里打开缓存似乎没什么帮助。
在大量页面上聚集属性计数方面,是否有其他策略被证明是成功的?
这里是供参考的站点:http://everydayzen.org/teachings/
发布于 2013-08-18 14:41:45
我通常会说“不要直接访问数据库;使用API",但我认为您应该在这里使用DB。
查看[Collection|File]SearchIndexAttributes表。(我不确定教诲是档案还是书页。如果页面,则需要通过仪表板中的作业定期重新索引它们。)查看索引表比加入属性值表中的最新版本要容易得多。一旦看到该表,就可以在SQL中执行一些简单的GROUPing。
如果您想使用这个API,您可以像今天这样作为一个批处理执行它,执行适当的计算,然后缓存它。
缓存没有理由不起作用,但是第一个命中(当缓存是冷的)当然会花费大量的时间。您应该缓存我的IndexAttributes想法(完整的表读取和循环并不简单),但至少在冷缓存中,可能需要一秒到10秒或更长的时间,数百个页面列表调用可能会占用这些时间。
发布于 2016-07-05 14:13:15
我在Concrete5的一个求职网站上做了一些类似的事情,通过显示每个部门的数量,工作减少了。
即人力资源(32)、销售(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] . ')';
}https://stackoverflow.com/questions/18283705
复制相似问题