| | |
| | | package com.oying.modules.pc.store.service.impl; |
| | | |
| | | import cn.hutool.core.collection.ListUtil; |
| | | import cn.hutool.core.util.NumberUtil; |
| | | import com.oying.modules.pc.store.domain.Store; |
| | | import com.oying.modules.pc.store.domain.dto.StoreQueryCriteria; |
| | |
| | | // 初始化缓存 |
| | | List<Long> ids = storeMapper.queryStoreIds(criteria); |
| | | for (Long id : ids) { |
| | | redisTemplate.opsForZSet().add(setKey, 1, id); |
| | | redisTemplate.opsForZSet().add(setKey, id, 1); |
| | | } |
| | | // 设置缓存过期时间 |
| | | redisTemplate.expire(setKey, 30, TimeUnit.MINUTES); |
| | |
| | | } |
| | | |
| | | // 2. 从ZSET获取分页ID |
| | | Long offset = NumberUtil.mul(criteria.getPage(), criteria.getSize()).longValue(); |
| | | long end = NumberUtil.add(offset, criteria.getSize()).longValue(); |
| | | int current = (criteria.getPage() == null || criteria.getPage() < 1) ? 1 : criteria.getPage(); |
| | | int offset = (current - 1) * criteria.getSize(); |
| | | int end = offset + criteria.getSize() - 1; |
| | | Set<Long> storeIds = Optional.ofNullable(redisTemplate.opsForZSet() |
| | | .range(setKey, offset, end - 1)).orElse(Collections.emptySet()) |
| | | .range(setKey, offset, end)) |
| | | .orElse(Collections.emptySet()) |
| | | .stream().map(i-> (Long) i).collect(Collectors.toSet()); |
| | | |
| | | // 3. 获取详情数据 |
| | | List<Store> stores = storeService.listByIds(storeIds); |
| | | List<Store> stores = Optional.ofNullable(storeService.listByIds(storeIds)).orElse(ListUtil.toList()); |
| | | |
| | | page.setContent(stores); |
| | | page.setTotalElements(total); |