Ideas for implementing a drop-down refresh function like news app (backend)

Source: Internet
Author: User
begins

First of all, this article is not to teach you how to pull down the Android/ios client implementation, but from the back-end perspective to achieve similar to today's headlines such as the drop-down of the news client's effect. For the moment, regardless of data statistics, accurate recommendation of those, only to talk about my personal realization of ideas. Demand

Product Manager to my side of the demand is probably the case: there are a number of short video, data volume of more than 1w, a total of 10 categories, the average of about 1000 data per classification. Each drop-down on the first page of data is not duplicated (the first 20 times), and the pull-up load is randomly scrambled on the current page and is not allowed to repeat. The front end does not do logical processing, only is responsible for the interface to take data. Back-end to do the implementation. Analysis

Then here are a few questions:
1. Each time the front-end pull down, the request interface parameters Pagenum are 1, according to common sense all return the first page of data, how to ensure that the data given each time is different.
2. Pull up load, pass the Pagenum is 2,3,4 ..., according to the normal paging to the database can not get it. But the demand clearly points to be scrambled to be random, if each time with Collections.shuffle (list), to disturb and then paged to fetch, that may appear and other pages duplicate data.

Is there a solution that ensures that the pull-down is not duplicated 20 times and that each page of data is not duplicated when the pull-up is taken into account? Ideas

Imagine: If this pull down 20 times, each time give a virtual paging number (1-20), this page number on each request on the first page (that is, pagenum==1) is accumulated and put into the cache, when the greater than 20 is reset to 1, then this creates a closed loop, Each drop-down is actually switching back and forth between pages 1th through 20th. (PS: Do not forget to use the user ID and short video type as part of the cache key when slowing down.)

Since there are a lot of fields for each short video bean, we can't put 1000 or so data in the corresponding type into the memcached of a key, we need to put each short video corresponding to a key into the cache. Then each type to generate 20 data pools, each pool number is the user pull down the virtual paging page number, such as the 1th pool, the first page is the first page of the drop, the second page began to temporarily create a templist, AddAll the first page of data and do RemoveAll on the original list, and then write a loop that uses the new Random or Math.random take the stochastic as the subscript, get the corresponding elements add into the templist, and remove the original list of this element, until the original list.size () for 0 to jump out of the loop, Putting this temporary templist into the cache is a pool. Other virtual paging ideas and so on.

After 20 pools of data order, according to the front-end request Pagenum and the user's corresponding virtual paging, you can determine exactly which pool data, only need to be based on paging parameters to intercept. Implement

The implementation steps are as follows:
1. Traverse each type of short video, each put a copy of the cache, subscript as key, easy to follow the subscript to the cache of short video;
2. Put the short video under each type in 20 pool, each pool in addition to the first page of data is not the same, the rest are disrupted;

The code is as follows:

Public list<integer> getidlist (list<integer> idlist, int index, int pageSize) {if (collectionutils.is
        Empty (idlist)) {return null;
        } list<integer> idlisttmp = idlist;
        list<integer> tmp = new arraylist<integer> (); if (Index * pageSize < Idlisttmp.size ()) {for (int i = (index-1) * pageSize; i < index * PAGESIZE; i              
            + +) {Tmp.add (Idlisttmp.get (i));               
        } idlisttmp.removeall (TMP);
        } Boolean hasnext = true;
        Random random = new Random (System.currenttimemillis ());
                while (Hasnext) {if (Idlisttmp.size () < 1) {Hasnext = false;
            Break
                } else {int indextmp = Random.nextint (Idlisttmp.size ());
                int id = idlisttmp.get (indextmp);
                Tmp.add (ID);
           Idlisttmp.remove (INDEXTMP); }} return tmp; }

3. According to the user virtual paging to the pool number, get that pool cache data, and then according to the front-end request paging parameters to return data.

    list<video> videolist = new arraylist<video> ();
Return by paging
        pageutils<integer> pages = new pageutils<integer> (idlist, Request.getpagenum (), Request.getpagesize ());
        if (Collectionutils.isnotempty (Pages.getresult ())) {for
            (int index:pages.getResult ()) {
                Object obj = Cache.get (type + index);
                if (null! = obj) {
                    videolist.add ((videobean) obj);}}}
        

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.