How to implement pagination with multiple Redis ordered collection combinations

Source: Internet
Author: User
Tags redis
how to implement pagination with multiple Redis ordered collection combinations
Requirements: Now put the different categories of data into different Redis ordered set key, the query does not distinguish between categories, to conduct a unified query.
Redis Paging entities:
Import java.io.Serializable; /** * Intermediate Filtration entity <br> *〈 Function Detailed description 〉* * @author wangmuming * @see [Related Classes/methods] (optional) * @since [Product/module version] (optional)/public CLA SS Redispaging implements Serializable {/** * * private static final long Serialversionuid = 802830321345270
    
    1294L;
    
    Private String key;
    
    Private long total;
    
    private int offset;

    private int limit;
    /** * @return The key */public String Getkey () {return key;
    }/** * @param key to set */public void Setkey (String key) {this.key = key;
    /** * @return The total/public long gettotal () {return total; 
    /** * @param Total "Total" to set */public void settotal (long total) {this.total = total;
    /** * @return The offset */public int getoffset () {return offset; /** * @param offset the offset to set * * *
    public void SetOffset (int offset) {this.offset = offset;
    }/** * @return the limit */public int getlimit () {return limit;
    }/** * @param limit the limit to set */public void setlimit (int limit) {this.limit = limit;
        }/* (non-javadoc) * @see java.lang.object#tostring () * */@Override public String toString () {
    Return "redispaging [key=" + key + ", total=" + Total + ", offset=" + offset + ", limit=" + limit + "]"; }
    
}


Content within the Query method:
 Returns the JSON map<string,object> Map = new linkedhashmap<string,object> () using Linkedhashmap as an ordered set;
        Map.put ("Start", page);
        Map.put ("Draw", draw);
            try {string[] keys = Key.split (",");
                if (keys.length = = 1) {//Get Redis statistics Long total = Redisutil.zcard (key); int offset = page; Paging starts at 0//long pagesize = (total% count) = 0?
                Total/count: (Total/count) +1;
                    if (offset <= total) {list<string> List = Redisutil.zrangebyscore (key, sort, offset, count);
                    List<orderlinkalert> objlist = new arraylist<orderlinkalert> ();
                    Orderlinkalert obj = null;
                    Long index = offset + 1l;
                        for (String string:list) {obj = new Orderlinkalert ();
                        Obj.setid (index); Obj.sEtorderitemid (string);
                        Double time = Redisutil.zscore (key, String);
                        Long efficatetime = 0l;
                        if (null!= time) {efficatetime = Time.longvalue ();
                        } obj.setexceedrange (Dateutil.formattime (efficatetime));
                        Objlist.add (obj);
                    index++;
                    } map.put ("Data", objlist);
                    Map.put ("Recordstotal", total);
                    Map.put ("recordsfiltered", total);
                    Map.put ("page", pagesize);
                Map.put ("Length", count);
                } else {//parallel path request//Get Redis statistics long total = 0l;
                Add multiple key values from store to list list<redispaging> keylist = new arraylist<redispaging> ();
       Redispaging paging = null;         for (string string:keys) {Long tmp = Redisutil.zcard (string);
                    Total = Total + tmp;
                    paging = new redispaging ();
                    Paging.setkey (string);
                    Paging.settotal (TMP);
                Keylist.add (paging); int offset = page; Paging starts at 0//long pagesize = (total% count) = 0?
                Total/count: (Total/count) +1; if (offset <= total) {//calculates what data Keylist = computeredispaging in each key (keylist,of
                  
                    Fset,count,total);
                    map<string,string> mapping = new linkedhashmap<string,string> (); Multiple key values exist in logical for (redispaging redispaging:keylist) {list<string> Li
                 st = Redisutil.zrangebyscore (Redispaging.getkey (), Sort, redispaging.getoffset (), Redispaging.getlimit ());       if (null!= list &&!list.isempty ()) {for (String string:list) {
                                Double time = Redisutil.zscore (Redispaging.getkey (), string);
                                Long efficatetime = 0l;
                                if (null!= time) {efficatetime = Time.longvalue ();
                            } mapping.put (String, Dateutil.formattime (Efficatetime)); }} list<orderlinkalert> objlist = new arraylist&
                    Lt;orderlinkalert> ();
                    Orderlinkalert obj = null;
                    Long index = offset + 1l;
                        For (map.entry<string,string> Entity:mapping.entrySet ()) {obj = new Orderlinkalert ();
                        Obj.setid (index); Obj.setorderitemid (enTity.getkey ());
                        Obj.setexceedrange (Entity.getvalue ());
                        Objlist.add (obj);
                    index++;
                    } map.put ("Data", objlist);
                    Map.put ("Recordstotal", total);
                    Map.put ("recordsfiltered", total);
                    Map.put ("page", pagesize);
                Map.put ("Length", count); Exception e) {logger.error (' Query Redis data err
            Or: ", e);
        throw new Exception (e);
         }


Redis multiple ordered set paging logic implementation:
 Private list<redispaging> computeredispaging (list<redispaging> keylist, int offset, int count, long total) {
        The number of records that are currently required to get long sum = 0;
        int indexstart = 0;
        int indexend = 0; Calculates a position of two values greater than or equal to offset + count+1 (int i=0;i<keylist.size (); i++) {sum = sum + key
            List.get (i). Gettotal ();
                if (Sum >= (offset)) {indexstart = i;
            Break
        } sum = 0;
            for (int i=0;i<keylist.size (); i++) {sum = sum + keylist.get (i). Gettotal ();
                if (i >= indexstart && sum >= (offset+count)) {indexend = i;
            Break else if (i >= indexstart && i = = (Keylist.size ()-1) && sum >= offset && sum <= (offset
                +count)) {indexend = i;
            Break }//System.ouT.println ("Indexstart:" +indexstart + "Indexend:" + indexend);
            If there is only one fetch all data if (Indexstart = = indexend) {redispaging paging = Keylist.get (Indexstart);
            Long T = 0l;
            for (int i=0;i<indexstart;i++) {t = t + keylist.get (i) gettotal ();
            } paging.setoffset (offset-(int) t);
            Paging.setlimit (count);
            Keylist.clear ();
        Keylist.add (paging); ///If there are only 2 separate data calculations first-offset from the second inside to fill the remaining data if ((indexend-indexstart) = = 1) {Red
            Ispaging PAGING0 = Keylist.get (indexstart);
            Long T = 0l;
            for (int i=0;i<indexstart;i++) {t = t + keylist.get (i) gettotal ();
            Long rest0 = offset-t;
            Paging0.setoffset ((int) rest0);
            Paging0.setlimit ((int) (Paging0.gettotal ()-rest0));
            Redispaging paging1 = Keylist.get (indexend); Paging1.setOffset (0);
            Paging1.setlimit ((int) (count-(Paging0.gettotal ()-rest0)));
            Keylist.clear ();
            if (paging0.gettotal () > 0) {keylist.add (PAGING0);
            } if (Paging1.gettotal () >0) {keylist.add (paging1);
            How many if ((Indexend-indexstart) >= 2) are computed at the beginning and end of the go all in the middle of greater than or equal to 3 {
            list<redispaging> tmp = new arraylist<redispaging> ();
            Tmp.addall (keylist);
            
            Keylist.clear ();
            Long T = 0l;
            for (int i=0;i<indexstart;i++) {t = t + tmp.get (i) gettotal ();
            
            Long rest0 = offset-t;
            Redispaging Pagingstart = Tmp.get (indexstart);
            Pagingstart.setoffset ((int) rest0);
            Pagingstart.setlimit ((int) (Pagingstart.gettotal ()-rest0));
        if (pagingstart.gettotal () > 0) {        Keylist.add (Pagingstart);
            } rest0 = Pagingstart.gettotal ()-rest0;
                for (int i= (indexstart+1); i<indexend;i++) {redispaging paging = Tmp.get (i);
                Paging.setoffset (0);
                Paging.setlimit ((int) tmp.get (i). Gettotal ());
                Rest0 = rest0 + tmp.get (i) gettotal ();
                if (paging.gettotal () > 0) {keylist.add (paging);
            } redispaging pagingtop = Tmp.get (indexend);
            Pagingtop.setoffset (0);
            Pagingtop.setlimit ((int) (COUNT-REST0));
            if (pagingtop.gettotal () > 0) {keylist.add (pagingtop);
    } return keylist; }


Related Article

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.