The browser sends a total of download files 2 second request, how to "download" only 1 times to record?

Source: Internet
Author: User

Recently, the personal official website realizes the PDF downloading function, for the statistic consideration. Added "Download Count" download_count this field.



But. I suddenly found out today. Each download Download_count is directly + 2. Assuming that the server happens, and it is possible that I download the Local on my own, how can be downloaded 2 times.

Then, the debug mode of log4j is turned on. Sure enough, 2 update requests were run.

@RequestMapping (value = "/download/pdf") public void Downloadpdf (@RequestParam Integer ID, httpservletrequest req, HttpServletResponse res) {map<string, object> Article = articleservice.get (ID); Pdfdownload.dodownloadpdfpostwithshuiyin (req, res, article);                Articleservice.plusdownloadcount (ID);      }



So, want to go through threadlocal<integer> local, such way to record. Suppose there is a value in local, which indicates that the current thread has been downloaded and does not need to update the download count again.

But it turns out that the above practice is not correct, the actual phenomenon is that the number of times added is not "stable", sometimes +1. Sometimes +2.

Later, I put the value in the local. Store the IP, infer whether it is null, or whether the 2 request IP is the same. The results are still not "stable" enough.
--------------------------------------------------------
in the course of practice, some of my "misconceptions":
1. Download the file, the browser will only send 1 requests. The actual is 2 times, no doubt.
2. Send 2 requests, is the same thread response. I take it for granted that all 2 requests are for "one download".
It turns out that I was too naïve.
3. Because it feels like 2 requests. I think using threadlocal to store a value indicating that the current thread has been downloaded.
It turns out that. This is unscientific.

2 requests. 2 different thread responses.
4. "2 requests, 2 different thread responses.

"Theoretically so."
But we had a similar problem when our team was doing the company project. The boss later thought that Tomcat's thread was implemented using a "thread pool".


Multiple requests may be the same thread or multiple.
This point. is consistent with the actual "number of updates" that occur.
5. Download article A, update the number of times.

Download articles B, C, D, the number of times are no longer updated.
This does not accord with my idea, the reason is: Download no comparison of the article ID, the number of downloads of different articles should be independent of each other.

--------------------------------------------------------
Now there are 2 questions:
1. I want to realize my original idea.


The browser sends 2 requests, and the number of downloads has been updated 2 times. Can you just let it update once. To accurately reflect the number of downloads!
I haven't thought of a good method at the moment.

2. Change the demand, a user in a certain period of time, download an article, no matter how many times. are only counted once.


Download different articles, should and should only be added 1 times.

My thoughts are:
Set up a queue that has already been downloaded, the user's IP and the ID of the article together as key.
The user downloads once, the user IP and the article ID together composed of key, stored in the queue.
When a new download request comes in. Find out from the queue if the key already exists. Assumption does not exist. Only +1. otherwise. Number of times not updated.


Original starting: http://fansunion.cn/article/detail/141.html

The browser sends a total of download files 2 second request, how to "download" only 1 times to record?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.