For temporary solution, I have used this script:
Placed it in your templates/index.php
<?php if (isset($view) && strtolower($view) == 'article') : $articleId = JRequest::getVar('id'); ?>
<script type='text/javascript'>
jQuery(document).ready(function($){
jQuery.post('../hits_counter.php',
{
option:'com_content',
view:'article',
id:'<?php echo $articleId;?>'
});
});
</script>
<?php endif; ?>
In hits_counter.php file
<?php
define( '_JEXEC', 1 );
//chdir("../");
getcwd();
define('JPATH_BASE', getcwd() );
define('DS', DIRECTORY_SEPARATOR);
$id = $_POST['id'];
//JPATH_LIBRARIES . '/legacy/application/application.php'
require_once( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once( JPATH_BASE .DS.'includes'.DS.'framework.php' );
//require_once( JPATH_BASE .DS.'includes'.DS.'application.php' );
require_once( JPATH_BASE .DS.'libraries'.DS.'legacy'.DS.'application'.DS.'application.php' );//libraries\legacy\application\application.php
// Get a db connection.
$db = JFactory::getDbo();
$s = 'select hits from #__content where id='.$id;
$db->setQuery($s);
$currentHits = $db->loadResult();
$updateHits = $currentHits+1;
$update = 'update #__content set hits='.$updateHits.' where id='.$id;
$db->setQuery($update);$db->Query();
$s1 = 'select hits from #__content where id='.$id;
$db->setQuery($s1);
$NowHits = $db->loadResult();exit;
?>
In above code will store the hits by counting +1,
At the same time, if you want to show the hits in your front end sites without hanging in Cache.
Go to com_content/views/article/default.php ( you can overwrite this file and placed it in your template file,If you overwrite then your path will be your_template/html/com_content/article/default.php )
search line <span class="icon-eye-open"></span>
place this below code,
<span class="icon-eye-open"></span>
<script type='text/javascript'>
$(document).ready(function() {
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "../view_hits.php",
data: { id:"<?php echo $this->item->id; ?>" },
dataType: "html", //expect html to be returned
success: function(response){
$("#my_ArticleView_cnt").text(response);
//alert(response);
}
});
});
</script>
<?php echo JText::sprintf('COM_CONTENT_ARTICLE_HITS', '<span id="my_ArticleView_cnt"></span>'); ?>
In view_hits.php place this below code:
<?php
define( '_JEXEC', 1 );
//chdir("../");
getcwd();
define('JPATH_BASE', getcwd() );
define('DS', DIRECTORY_SEPARATOR);
@$id = $_POST['id'];
@$fetch = $_POST['fetch'];
//JPATH_LIBRARIES . '/legacy/application/application.php'
require_once( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once( JPATH_BASE .DS.'includes'.DS.'framework.php' );
//require_once( JPATH_BASE .DS.'includes'.DS.'application.php' );
require_once( JPATH_BASE .DS.'libraries'.DS.'legacy'.DS.'application'.DS.'application.php' );//libraries\legacy\application\application.php
// Get a db connection.
$db = JFactory::getDbo();
$s = 'select hits from #__content where id='.$id;
$db->setQuery($s);
echo $currentHits = $db->loadResult(); exit;
?>
Same code you can used it for intro text view or full text article view.
Now you will get the Hits count without hanging in Cache.