2013-08-16 138 views
1

我們用Joomla最初開發的Concrete5構建了一個站點。我們的工作是把所有東西都帶回來,然後把它混合起來。這個網站的主要部分是約1200音頻的教導,每個教學具有不同的屬性,如主題,作者,節目,位置等。混凝土5屬性計數

一些教導可能有多個屬性分配,多個關鍵字或主題。

我想給計數的所有屬性,使訪問者可以看到有多少教導是由某個作者,或有多少是關於特定主題一目瞭然,即:

  • 道德(20)
  • 恐懼(42)
  • 感恩(55)

我原來的代碼變成了有太多的偶然聽到是這麼教導等許多屬性實用。基本上,我跑過去了,對於每個屬性,我都根據PageList數進行了總計數查找。我們正在討論每次頁面加載時的數百次查詢。打開緩存在這裏似乎沒有幫助。

是否有任何其他策略已證明成功地聚合了大量頁面上的屬性計數?

這裏是參考網站:http://everydayzen.org/teachings/

回答

1

我通常說「不直接訪問數據庫,使用API​​」,但我認爲你應該在這裏使用DB。

查看[Collection|File]SearchIndexAttributes表。 (我不確定教學是否是文件或頁面,如果是頁面,則需要通過儀表板中的工作定期重新編制索引。)查看索引表比加入最近期的要容易得多版本在屬性值表中。一旦你看到該表,你可以在SQL中做一些簡單的分組。

如果你想使用API​​,你可以像現在這樣做批處理,做適當的計算然後緩存它。

沒有理由緩存不應該工作,但第一個命中(當緩存很冷時)當然會佔用全部時間。你應該緩存我的IndexAttributes想法(一個完整的表讀和循環不是微不足道的),但至少有一個寒冷的緩存,應該需要幾秒鐘的時間比數十頁列表調用可能需要的10秒或更長時間。

1

我已經做了一個工作網站Concrete5類似的東西,通過表明就業落在各部門的罪名。

即HR(32),銷售(12)等

這是從acheives這個助手採取的代碼(這僅僅是相關的功能包括):

<?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; 
} 

然後,您可以從一個分頁模板,請執行下列操作

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