One solution is simply to count the moment the GET
of the image is accessed.
GET /user/manga/113
To know the number of views, you can have an endpoint for this:
GET /user/manga/113/views
Each access will generate a record in a table, with some visitor data or simply a simple record of the visit. To know how many times it has been visited, just make the count of the records in the table. This solution, if you save information from the user you viewed, allows you to do a total count of views and a count of views excluding multiple visits from the same user (this is the concept of "unique visitors").
You could also create a "counter" column in the table and increase each view as long as you make a UPDATE
in the direct counter in the database:
UPDATE manga SET views = views +1 WHERE id = ?
Letting the database itself deal with the writing competition issue if multiple people are accessing the same image at the same time.
If you opt, for whatever reason, to make a SELECT
in value, increment and then make UPDATE
, you will have writing concurrency problems of this value. For example, if two GET
calls occur simultaneously in an image with 5 views, both will get the current value of the bank, increment by 1, and save. So, instead of having 7 views you may have only recorded 6 in the database.