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