티스토리 뷰

반응형

JAVA Spring Redis 검색 조회 및 페이징 기능 

public List<String> getSearchDataList(String searchData, int pageNum, int pageSize) {

        if("all".equals(searchData)){
            searchData = "*";
        } else {
            searchData = "*"+searchData+"*";
        }

        ScanOptions scanOptions = ScanOptions.scanOptions().match(searchData).build();
        Cursor<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().scan(scanOptions);

        List<String> keysDataList = new ArrayList<>();
        List<String> result = new ArrayList<>();

        while (keys.hasNext()) {
            keysDataList.add(new String(keys.next(), StandardCharsets.UTF_8));
        }

        if(keysDataList.size() > 0){
            int startIndex = (pageNum-1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, keysDataList.size());

            Collections.sort(keysDataList);

            if (keysDataList.size() >= pageNum * pageSize) {
                result = keysDataList.subList(startIndex, endIndex);
            }
        } else {
            System.out.println("데이터 없음");
        }

        return result;

    }

 

 

 

끗자디ㅓ34132$!@#$

반응형