2

I maintain an application where users are able to store images, and then share them. The system is powered by MongoDB at the back end. Most of the image depiction pages are cached as flat HTML files, but I can run some code just before loading the file.

I've decided to implement a view count for the system. I am wondering what is the best storage place for that. It should be like Memcached but it should save the viewcounts every hour or so, so even if our server has to be restarted we won't lose the view counts.

What is the best solution for that (preferably with a PHP extension as a client)?

hakre
  • 178,314
  • 47
  • 389
  • 754
Kristina Brooks
  • 14,589
  • 25
  • 101
  • 178

4 Answers4

2

You might look into Redis. Sort of a cross between NoSQL and memcached. You can have Redis save to disk though. Is memcached a dinosaur in comparison to Redis?

Community
  • 1
  • 1
Brent Baisley
  • 12,441
  • 2
  • 21
  • 39
0

It sounds like the view count is for each cached flat HTML file? Why not store them in some code at the start of the file. It can be taken out when the file is viewed...

Darren
  • 9,190
  • 7
  • 38
  • 58
  • The view count is prepended to the HTML files by PHP as a JS variable. Then Javascript puts it in the right place. And, what you're suggesting is far from efficient. – Kristina Brooks Dec 27 '10 at 21:01
0

I once solved this problem by storing the view count in memcached and then updating the database at random intervals based on the number of views. The probability of storing to the database was 1/sqrt(number of views). This had nice properties. If the number of views was small, like 4, then the probability of writing to the database was 1/2. If the number of views on an object was high, say 1,000,000, then the probability goes to 1/1000. It seemed to work well.

Zeki
  • 4,531
  • 1
  • 16
  • 26
0

Do thing simple ... if you does not require a real time report/statistic, you should just parse the daily apache log and store the number of matches into database or even in a flat file format.

Benefits?

  1. it does not harm your existing application
  2. it does not require additional memcache or so
  3. just need to parse the daily log apache (once a day)
  4. recoverable

Of course, this does not stop you from set-up a cronjob to periodically read/update database (for fresher view count)

ajreal
  • 44,929
  • 10
  • 81
  • 118