Parallel map sorted by values ​​with fast increment operation

I have a web application where I need to track the “most popular” (most visited) articles. Most pages (including article pages) in this application display the “most popular” list in the sidebar, so this list will be retrieved very often. On the other hand, articles are often visited too often (approximately half of page visits go to article pages).

What is the best way to track visits and select the N most visited articles? As far as I understand, it should be a parallel map articleId-> visitCount, which is sorted by values ​​(visitCounts), and where I can quickly (and threadafely) increase visitCount and expect the map to be re-sorted.

+4
source share
3 answers

For a web application, the best storage location would be in the database. Create a database with a field for the article ID and a field for the number of visits. Index the table by the number of visits. Whenever an article is viewed, add a post or enlarge an existing post. When you need to see a list of the most popular, just go to the table.

Databases are often the best answer for storing data in a web application.

In this case, the database will index the table depending on the number of visits. This makes it a little slower to insert and update, but the databases are designed to do the job, so it won't be that bad. Getting this data will always be very fast due to the supported index.

+2
source

If you do not want to use the database, you can use SortedSet to store objects containing both the article identifier and the visit count. Comparison of facilities will be at the expense of visits. An implementation may include a TreeSet , which must be synchronized externally in a multi-threaded environment, and a ConcurrentSkipListSet , which must not be synchronized externally.

+1
source

Personally, I would not respond to this during the update. You are much more likely to update your structure every time you visit than you read.

When the time comes for reading, make a copy of each identifier, go to # record, and then select it for display. You will be surprised how cheap it is.

0
source

All Articles