
內容簡介
實現頁面計數器的方法有很多種,使用外部服務如Jetpack 或 Google Analytics 絕對是不錯的選擇,但在某些場合下,您可能希望自己處理此事並更多地掌控情況。
由於非常普遍的做法是利用某種緩存機制來提高網站性能,所以直接在 PHP 層級上實現計數器並不可靠,因為計數器增加將很少,這是由於快取的原因。更好的解決方案是使用 WordPress 觸發 AJAX 請求的方式,在加載任何時候增加頁面計數器,無論是從快取中提供還是不提供。
然而,當我們考慮更大的規模時,我們會發現傳統的 WP AJAX 實現也很消耗資源。因此,此外掛使用了更為複雜的方式來增加計數器,但很輕量級。總之,計數器存儲在 WordPress 生態系統 “外部” 的一些快速儲存區(例如 Memcached 或 Redis)中。然後有一個在 WordPress 層級上實現的定期檢查新計數器的定期任務,並將其值作為每個相應帖子的 postmeta 儲存。
此外掛使用了PhpFastCache 庫,允許將數據存儲到多種後端中。有關詳細信息,請參閱其文檔。此功能允許您根據您的託管提供商選擇任何後端。
此外掛預配置了基於文件的儲存方式,這應該在任何託管環境中運行,但對於高流量的網站,您可以考慮使用 MemcacheD 的內存儲存方式。
該原理和想法在下面的文章中有詳細描述,該文章發布在我的博客上。
外掛標籤
開發者團隊
原文外掛簡介
There are deffinitely different methods for implementing pageview counters. Considering some external service such as Jetpack or Google Analytics is surely a good option, but there may be scenarios where you may want to handle this just by yourself and have everything more under control.
Since it’s very common to utilize some caching mechanism to improve your site’s performance, implementing counter directly on PHP level wouldn’t work reliably as counter would be barely incremeneted due to the caching. Better solution would be to use WordPress way of triggering AJAX requests and increment pageview counters anytime page is loaded, no matter if served from cache or not.
When we start to think on bigger scale though, we find tradional WP AJAX implementation quite resources heavy as well. This plugin uses slightly more complicated method to increase counters, but it’s very lightweight. In a nuthsell, counters are stored ‘outside’ of WordPress ecosystem into some fast storage such as Memcached or Redis. Then there’s implemented re-occuring cron task on WordPress level, which regularly checks for new counters and store its values as postmeta for each respective post.
Plugin utilizes PhpFastCache library which allows to store data into many types of back-ends. See their documentation for more details. This feature allows you to choose any back-end depending on your hosting provider.
This plugin comes preconfigured with file-based storage, which should work on any hosting environment, but for higher traffic sites you may want to consider in-memory storage such as MemcacheD.
Whole principle and idea is described in following article published on my blog.
