Using Memcache as a counter for multiple objects

I have a website for photo hosting and I want to track photo views. Due to the large amount of traffic that I get, increasing the column in MySQL with each hit carries too much overhead.

I currently have a system implemented using Memcache, but this is pretty much just a hack.

Every time a photo is viewed, I expand my photo-hits_uuid file in Memcache. In addition, I am adding a string containing uuid to the invalidation array, also stored in Memcache. Each so often, I get an invalidation array, and then cycle through the lines in it, clicking on the photos hits in MySQL and decreasing their Memcache keys.

This approach works much faster than directly using MySQL, but is there a better way?

+4
source share
3 answers

I did some research, and it looks like Radish may be my solution. It seems like it is essentially Memcache with more functionality - for me the most valuable is listing, which pretty much solves my problem.

+2
source

There is a way that I use.

Method 1: (File Size) Each time someone hits the page, I add another byte to the file. Then after x seconds or so (I set 600), I will count how many bytes in my file, delete my file, and then upgrade it to the MySQL database. It will also scale if multiple servers are added to a small file on the cache server. Use fwrite to add to the file and you will never have to read this cache file.

Method 2: (The number stored in the file) Another method is to save the number in a text file that contains the number of hits, but I recommend using it, because if two processes are updated at the same time, the data may be disabled (maybe the same thing with method1).

I would use method 1, because although this is a larger file size, it is faster.

0
source

I assume that you store access logs on your server for this solution.

  • Keep track of the last time you checked your logs.
  • Every n seconds or so (where n is less than the time it takes for your logs to be rotated, if any), look at the last log file, ignoring each press, until you find the timestamp after your last check time.
  • Indicate the number of hits for each image.
  • Add each counter to the account stored in the database.
  • Save the timestamp of the last log entry that you processed the next time.
0
source

Source: https://habr.com/ru/post/1298947/


All Articles