PStamatiou
PStamatiou

Reputation: 151

displaying # views on a page without hitting database all the time

More and more sites are displaying the number of views (and clicks like on dzone.com) certain pages receive. What is the best practice for keeping track of view #'s without hitting the database every load?

I have a bunch of potential ideas on how to do this in my head but none of them seem viable.

Thanks, first time user.

Upvotes: 3

Views: 434

Answers (5)

Anon
Anon

Reputation: 326

One way is to use memcached as a counter. You could modify this rate limit implementation to instead act as general counter. The key could be in yyyymmddhhmm format with an expiration of 15 or 30 minutes (depending on what you consider to be concurrent visitors) and then simply get those keys when displaying the page.

Nice libraries for communicating with the memcache server are available in many languages.

Upvotes: 1

Tiberiu Ana
Tiberiu Ana

Reputation: 3663

One cheap trick would be to simply cache the value for a few minutes.

The exact number of views doesn't matter much anyway since, on a busy site, in the time a visitor goes through the page, a whole batch of new views is already in.

Upvotes: 2

alok
alok

Reputation: 1330

If you are using server side scripting, increment it in a variable. It's likely to get reset if you restart the services so not such a good idea if accuracy is needed.

Upvotes: -1

Chris
Chris

Reputation: 1400

I would try the database approach first - returning the value of an autoincrement counter should be a fairly cheap operation so you might be surprised. Even keeping a table of many items on which to record the hit count should be fairly performant.

But the question was how to avoid hitting the db every call. I'd suggest loading the table into the webapp and incrementing it there, only backing it up to the db periodically or on webapp shutdown.

Upvotes: 3

Buddy
Buddy

Reputation: 6713

You could set up a flat file that has the number of hits in it. This would have issues scaling, but it could work.

If you don't care about displaying the number of page views, you could use something like google analytics or piwik. Both make requests after the page is already loaded, so it won't impact load times. There might be a way to make a ajax request to the analytics server, but I don't know for sure. Piwik is opensource, so you can probably hack something together.

Upvotes: 0

Related Questions