複数のredis秩序化集合の組合せがページ分割をどのように実現するか

9496 ワード

複数のredis秩序化集合の組合せがページ分割をどのように実現するか
需要:異なるカテゴリのデータを異なるredis秩序集合keyに格納し、クエリー時にカテゴリを区別せずに統一されたクエリーを行います.
redisページングエンティティ:
import java.io.Serializable;

/**
 *       
* 〈 〉 * * @author wangmuming * @see [ / ]( ) * @since [ / ] ( ) */ public class RedisPaging implements Serializable { /** */ private static final long serialVersionUID = 8028303213452701294L; private String key; private long total; private int offset; private int limit; /** * @return the key */ public String getKey() { return key; } /** * @param key the key to set */ public void setKey(String key) { this.key = key; } /** * @return the total */ public long getTotal() { return total; } /** * @param total the 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 + "]"; } }

クエリー・メソッドの内容:
 //  linkedhashmap         json
        Map map = new LinkedHashMap();
        map.put("start", page);
        map.put("draw", draw);
        try {
            
            String[] keys = key.split(",");
            if( keys.length == 1 ) {
                //   redis     
                long total = RedisUtil.zcard(key);
                int offset = page; //   0  
//                long pagesize = (total % count) == 0 ? total /count : (total /count)+1;
                if(offset <= total) {
                    List list = RedisUtil.zrangeByScore(key, sort, offset, count);
                    List objList = new ArrayList();
                    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 { //         
                
                //   redis     
                long total = 0l;
                //   key     list 
                List keyList = new ArrayList();
                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; //   0  
//                long pagesize = (total % count) == 0 ? total /count : (total /count)+1;
                if(offset <= total) {
                    //    key        
                    keyList = computeRedisPaging(keyList,offset,count,total);
                  
                    Map mapping = new LinkedHashMap();
                    //   key      
                    for (RedisPaging redisPaging : keyList) {
                        List list = 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 objList = new ArrayList();
                    OrderlinkAlert obj = null;
                    long index = offset + 1l;
                    for (Map.Entry 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);
                }
                
            }
          
        } catch (Exception e) {
            logger.error("query redis data error:", e);
            throw new Exception(e);
        }
        

redisマルチ秩序集合ページングロジックの実装:
 private List computeRedisPaging(List keyList, int offset, int count, long total) {
        //          
        long sum = 0;
        int indexStart = 0;
        int indexEnd = 0;
        //             offset       offset + count+1   
        for(int i=0;i= (offset) ) {
                indexStart = i;
                break;
            }
        }
        sum = 0;
        for(int i=0;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( indexStart == indexEnd ) {
            RedisPaging paging = keyList.get(indexStart);
            long t = 0l;
            for(int i=0;i 0 ) {
                keyList.add(paging0);
            }
            if( paging1.getTotal() >0 ) {
                keyList.add(paging1);
            }
            
        }
        
        //      3                      
        if( (indexEnd-indexStart) >= 2 ) {
            List tmp = new ArrayList();
            tmp.addAll(keyList);
            keyList.clear();
            
            long t = 0l;
            for(int i=0;i 0 ) {
                keyList.add(pagingStart);
            }
            rest0 = pagingStart.getTotal() - rest0 ;
            for(int i=(indexStart+1);i 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;
    }