Jump to content

$logs as temporary page counters


Sergio
 Share

Recommended Posts

Folks, I'm wondering if I could have problems with the following setup, any ideas welcome!

The problem:

Setup a page counter with Procache

The project I'm working on has page views in the DB since 2007. I'm migrating from Wordpress. For some reasons not discussed here, these pages views are different than the number of views from Google Analytics. So, I have two fields on each page, page_views and ga_views.

Since I'm using Procache, I created an Ajax page that handles the increment of page_views. Works like a charm, but of course, the page is cleared from the cache as soon as the field gets updated. 

I was planning to use Redis for this, store the pages views in it and create a Cron job to retrieve then each night. But I was reading the PW's $log docs today and decided to try and store the page views as a log entry (using Text => $page->id).

So, I created a template to get the entries, sum them by page id and increment the page_views field. After that, I delete the log.

Currently, the website (still running in WP) has about 1500-2000 page views per day.

Do you guys think that I can have problems with this setup regarding disk I/O on the server, for instance?

I'm still thinking about using Redis in the future, but I have to learn it a little bit more and the project is on a tight schedule. So I thought about this as a preliminary solution. 

Link to comment
Share on other sites

The pages views IMHO aren't really in a critical range if you have halfway decent hardware, though if you're really trying to squeeze milliseconds out of the system and be prepared for spikes in page hits, a (carefully tuned) redis approach might be the best option. As an intermediate solution, you may also consider storing hits in a (non-pw-field) InnoDB table (entry id with auto increment, page_id, timestamp, plus arbitrary non-key columns) to avoid file level locks and repeated seek()s to the end of a potentially large log file. I'm using the InnoDB approach for our intranet with > 50.000 page views / day. In a nightly cron job, I switch the stats table for a new one (create table stats_new like stats; rename table stats to stats_[timestamp], stats_new to stats) and process the data from the then idle table.

  • Like 2
Link to comment
Share on other sites

Another alternative would be using what you (probably) already have: it's more than likely that you've got logging enabled for Apache, and the Apache log file will no doubt include the URL of each viewed page. See where I'm going with this? :)

The benefit of this approach would be that you reap all the benefits of ProCache: the bulk of the requests are served static files, and PHP or MySQL (or Redis or whatever) won't need to be triggered at all. The obvious downside is that parsing the logs requires a bit more work than simply fetching the data from a table.

  • Like 2
Link to comment
Share on other sites

That's a cool approach too, @teppo, although requires a bit more work and, as you already said, there's a downside on the flexibility of having a custom table.

I already implemented @BitPoet suggestion. I created a module with a couple of methods and a custom table. :) 

Now I'm working on using PWCron module to run the nightly job. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...