Hit counter persistent variables

Manlio Perillo manlio_perillo at libero.it
Wed Aug 20 20:31:25 MSD 2008


Chris Savery ha scritto:
> Thanks for the suggestion. I don't really want to go to that extent and 
> I'd wonder if updating a database would just slow the static files down. 

Yes, it will slow down.

> What I had in mind is just incrementing a variable. I suppose I could 
> write a simple module that maintains a few in memory varibles. Just 
> wondered if there was an easier way.
> 

But do you need to keep a global counter, or a counter for each file?
I was suggesting a database assuming you want to know how many time each 
  single thumbnail was server.

> Maybe write a log format that just adds a single char for each hit. Then 
> later I can find hit count by checking the length of that log file? And 
> a cron once an hour that stores the file length in another file and 
> deletes the log so it repeats. 

In this case I think it is better to use shared memory.
You open a file (whose name is in configration), make sure its size is 
sizeof(atomic_t), mmap it,  cast the memory address to atomic_t* and 
then use atomic_fetch_add(*v, 1) to increment the counter.

Each time you want to know the hit count, you just have to do the same 
operation, reading the atomic_t* variable from mapped memory (this 
should be an atomic operation).

This solution should be very fast, but you need to write a custom module 
and a custom tool for reading data.


 > [...]


Manlio Perillo





More information about the nginx mailing list