Jump to content


Photo

Page view counter and cache


  • Please log in to reply
4 replies to this topic

#1 mangopo

mangopo

    Newbie

  • Members
  • Pip
  • 5 posts
  • 6

Posted 09 August 2012 - 06:43 PM

My site's articles use a page view counter that makes it possible to list top 5 most viewed articles. After enabling cache for all pages it no longer counts views. Is there any way to fix this?

#2 MadeMyDay

MadeMyDay

    Sr. Member

  • Members
  • PipPipPipPip
  • 138 posts
  • 125

Posted 10 August 2012 - 03:51 AM

Depends on how this page view counter works. You could either work with a ajax request to an uncached page to update the counter or you use a (then rather complicated) construct with markup cache.

#3 mangopo

mangopo

    Newbie

  • Members
  • Pip
  • 5 posts
  • 6

Posted 10 August 2012 - 04:38 AM

This is the code in the blog article template:

$page->setOutputFormatting(false);
$page->views = $page->views+1;
$pages->saveField($page, 'views');
$page->setOutputFormatting(true);


#4 MadeMyDay

MadeMyDay

    Sr. Member

  • Members
  • PipPipPipPip
  • 138 posts
  • 125

Posted 10 August 2012 - 04:45 AM

Well, as suggested: I would put this code in a page with a non cached template:

<?php
$id = $sanitizer->text($input->get->id);
$whichPage = $pages->get($id);
$whichPage->views = $whichPage->views+1;
$whichPage->saveField($whichPage, 'views');

And in your pages / other templates you place something like this (jQuery assumed):

<script>
$.get("/mycounter/", {id: <?= $page->id ?> } );
</script>

"mycounter" would be your counter page with the counter template.

[totally untested]

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3116

  • LocationAtlanta, GA

Posted 10 August 2012 - 07:13 AM

I like MadeMyDay's solution. A couple other possibilities are:

1. Use MarkupCache rather than template caching. That way you can cache just the parts of the output that you want to, and leave the things like your counter uncached.

2. Create an autoload module. Add a ready() function to it and have that function increment the counter of $this->page (using the same method you are already doing).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users