私は私のニュースチャンネルでBen CrokerのEntry
Countプラグインを使用していますが、巨大なパフォーマンスヒットに気付きました。私はテンプレートにエントリをロードする方法を知っています。
それが使用されているニュースチャンネルには何万というエントリがあります。
このクエリをハイライト表示したサーバーログは、完了までに3分かかる https://pastebin.com/02uF0Mat
CPUを最大にしてサイトをクラッシュさせる。
私のテンプレートコード:
{% cache globally using key "trending-news" %}
{% set countedEntries = craft.entryCount.entries.section('news').limit(4) %}
{% for entry in countedEntries %}
{{ entry.postDate.format('jS F Y') }}
{{ entry.title }}
{% endfor %}
{% endcache %}
だから、私はこのコードが何千ものニュースエントリを読み込んでいると思って、それに4個しか表示していないと思うのですが、トップ4だけをロードできる方法はありますか?
事前に大変感謝しています!
ベストアンサー
This is untested since I don’t have time to load the plugin, so
please comment if there is a syntax error but changing this
// get all records from DB ordered by count descending
$entryCountRecords = EntryCountRecord::model()->findAll(array(
'order'=>'count desc'
));
次のようなものにはうまくいくはずです
public function getEntries($limit = null)
{
$where = [
'order' => 'count desc'
];
if($limit !== null){
$where['limit'] = $limit;
}
//get all records from DB ordered by count descending
$entryCountRecords = EntryCountRecord::model()->findAll($where);
....
そしてあなたのテンプレート
{% set countedEntries = craft.entryCount.entries(4).section('news').limit(4) %}
説明: ElementCriteriaModel
のように同じ量を読み込むすべてのエントリを取得するクエリの制限を含めます。
編集:多分あなたはこれを変更する必要がありますへ
public function getEntries($limit = null)
{
return craft()->entryCount->getEntries($limit);
}