MarkAHershberger has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250006

Change subject: Reduce cache time if fewer than 100 views
......................................................................

Reduce cache time if fewer than 100 views

There are some reports about HC not working on new pages.  In my
testing, it works but caching hides the display.

Proposed solution implemented here reduces the caching time for pages
with less than 100 hits from 1 day to 1 minute.

Added debugging statements to help with all this.

Change-Id: I900bcfd198e935e4371a8ddd43305ba781010b9b
---
M HitCounters.body.php
M HitCounters.hooks.php
M ViewCountUpdate.php
3 files changed, 18 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/HitCounters 
refs/changes/06/250006/1

diff --git a/HitCounters.body.php b/HitCounters.body.php
index 1c67b98..7b95a1f 100644
--- a/HitCounters.body.php
+++ b/HitCounters.body.php
@@ -30,6 +30,9 @@
                $cache = wfGetCache( CACHE_ANYTHING );
                $key = wfMemcKey( 'viewcount', $title->getDBkey() );
                $views = $cache->get( $key );
+               wfDebugLog( "HitCounters", "Got viewcount=" . var_export( 
$views, true ) .
+                       " from cache" );
+
                if ( !$views ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $row = $dbr->select(
@@ -37,11 +40,20 @@
                                array( 'hits' => 'page_counter' ),
                                array( 'page_id' => $title->getArticleID() ),
                                __METHOD__ );
+                       wfDebugLog( "HitCounters", "Got result=" . var_export( 
$row, true ) .
+                               " from DB and setting cache." );
 
                        if ( $row !== false && $current = $row->current() ) {
                                $views = $current->hits;
-                               /* update only once a day */
-                               $cache->set( $key, $views, 24 * 3600 );
+                               wfDebugLog( "HitCounters", "Got result=" . 
var_export( $current, true ) .
+                                       " from DB and setting cache." );
+                               if ( $views < 100 ) {
+                                       // Only cache for a minute
+                                       $cache->set( $key, $views, 60 );
+                               } else {
+                                       /* update only once a day */
+                                       $cache->set( $key, $views, 24 * 3600 );
+                               }
                        }
                }
 
diff --git a/HitCounters.hooks.php b/HitCounters.hooks.php
index b8834a2..9cf35ea 100644
--- a/HitCounters.hooks.php
+++ b/HitCounters.hooks.php
@@ -121,6 +121,7 @@
 
                        $viewcount = HitCounters::getCount( $skin->getTitle());
                        if ( $viewcount ) {
+                               wfDebugLog( "HitCounters", "Got 
viewcount=$viewcount and putting in page" );
                                $tpl->set( 'viewcount', $skin->msg( 'viewcount' 
)->numParams( $viewcount )->parse() );
                        }
                }
diff --git a/ViewCountUpdate.php b/ViewCountUpdate.php
index b2079cc..5b930eb 100644
--- a/ViewCountUpdate.php
+++ b/ViewCountUpdate.php
@@ -47,11 +47,13 @@
                global $wgHitcounterUpdateFreq;
                $dbw = wfGetDB( DB_MASTER );
 
+               wfDebugLog ( "HitCounter", "update freq set to: 
$wgHitcounterUpdateFreq;" );
                if ( $wgHitcounterUpdateFreq <= 1 || $dbw->getType() == 
'sqlite' ) {
                        $pageId = $this->pageId;
                        $method = __METHOD__;
                        $dbw->onTransactionIdle( function () use ( $dbw, 
$pageId, $method ) {
                                try {
+                                       wfDebugLog( "HitCounter", "About to 
update $pageId" );
                                        $dbw->upsert( 'hit_counter',
                                                // Perform this INSERT if 
page_id not found
                                                array( 'page_id' => $pageId, 
'page_counter' => 1 ),
@@ -61,6 +63,7 @@
                                                $method
                                        );
                                } catch ( DBError $e ) {
+                                       wfDebugLog("HitCounter", "Got an 
exception: " . $e->getMessage() );
                                        MWExceptionHandler::logException( $e );
                                }
                        } );

-- 
To view, visit https://gerrit.wikimedia.org/r/250006
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I900bcfd198e935e4371a8ddd43305ba781010b9b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/HitCounters
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to