Keyword

Caching and hit counter

  • Uzi
  • Uzi's Avatar Topic Author
  • Offline
  • Premium Member
More
3 weeks 1 day ago - 3 weeks 1 day ago #170399 by Uzi
Uzi created the topic: Caching and hit counter
Hi all,

I have a website which I enabled the cache module to speed things up a bit. Which works wonders, but the only thing that bothers me is that the hit counter is not working properly anymore.

I use Conservative caching - Gzip and Jotcache.

I can imagine other people use the cache as well, and use the hit counter as well, so is there a solution to this?

EDIT:

Ok so there as a topic here on this forum regarding this issue. They provided me this:
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (isset($_GET['view']) && strtolower($_GET['view']) == 'item') {
 $articleId = (int) $_GET['id'];
 
 $db = JFactory::getDBO();
 $db->setQuery(
                        'UPDATE #__k2_items' .
                        ' SET hits = hits + 1' .
                        ' WHERE id = '.(int) $articleId
                );
 $db->query();
}

?>

To put this in the K2 Tools module and parse the PHP code. Unfortunately this does not work when caching is enabled. It seems to me that the actual code does not gets fired because the hits are not added. Also when I disable caching alltogether and refresh the page the hits should be doubled (the normal hit counter + this module) but unfortunately again, this does not happen. I think there is something wrong with this code. Could someone help me out?
Last Edit: 3 weeks 1 day ago by Uzi.

Please Log in or Create an account to join the conversation.

  • Uzi
  • Uzi's Avatar Topic Author
  • Offline
  • Premium Member
More
3 weeks 1 day ago #170401 by Uzi
Uzi replied the topic: Caching and hit counter
Found the solution:
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (isset($_GET['view']) && strtolower($_GET['view']) == 'item') {
 $articleId = JRequest::getInt('id');
 $db = JFactory::getDBO();
 $db->setQuery(
                        'UPDATE #__k2_items' .
                        ' SET hits = hits + 1' .
                        ' WHERE id = '.(int) $articleId
                );
 $db->query();
}

?>

The article getting was not properly in the code. I changed that to above:
$articleId = JRequest::getInt('id');

This works!

Only jotcache gives problems because it also caches modules, so I need to figure that out, but with Native joomla caching + exluding the modules you don't want cached, this works.

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
3 weeks 1 day ago #170411 by Fotis
Fotis replied the topic: Caching and hit counter
This is something that will surely be addressed in K2 in 2019. The right way is actually to do this using JS so you can have real-time hit counting, bypassing CDN, server-side caching (Varnish/Nginx in proxy mode), Joomla's caching, browser (client-side) caching etc.

So basicallythe process is to load a JS file with a client-side generated timestamp (to bypass any caching, server or client side) on the "item" view in K2 only and then send an async/delayed POST request to trigger an extra hit in the database for that item. Now since that process may cause load on popular sites (sites with more than 100,00 monthly visitors or a few million pageviews per month), the plan is to move the "hits" records to a separate table so writing to that table is blazing fast.


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.

Moderators: william white