记得上下班打卡 | git大法好,push需谨慎

Commit 9b61cc87 authored by 姜秀龙's avatar 姜秀龙

LostFoundItem redis

parent c21f48ef
......@@ -7,8 +7,10 @@ import com.liquidnet.service.sweet.entity.SweetLostFoundItem;
import com.liquidnet.service.sweet.mapper.SweetLostFoundItemMapper;
import com.liquidnet.service.sweet.param.SweetLostFoundItemParam;
import com.liquidnet.service.sweet.service.ISweetLostFoundItemService;
import com.liquidnet.service.sweet.utils.LostFoundRedisUtils;
import com.liquidnet.service.sweet.vo.SweetLostFoundItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
......@@ -23,6 +25,9 @@ import java.util.stream.Collectors;
@Service
public class SweetLostFoundItemServiceImpl extends ServiceImpl<SweetLostFoundItemMapper, SweetLostFoundItem> implements ISweetLostFoundItemService {
@Autowired
private LostFoundRedisUtils lostFoundRedisUtils;
@Override
public boolean publishItem(SweetLostFoundItemParam item) {
if (item == null) {
......@@ -38,6 +43,8 @@ public class SweetLostFoundItemServiceImpl extends ServiceImpl<SweetLostFoundIte
lostFoundItem.setDescription(item.getDescription());
lostFoundItem.setItemImage(item.getItemImage());
lostFoundRedisUtils.deleteItemListCache(item.getPerformanceId());
return baseMapper.insert(lostFoundItem) > 0;
}
......@@ -57,6 +64,9 @@ public class SweetLostFoundItemServiceImpl extends ServiceImpl<SweetLostFoundIte
existingItem.setDescription(item.getDescription());
existingItem.setItemImage(item.getItemImage());
lostFoundRedisUtils.deleteItemListCache(item.getPerformanceId());
lostFoundRedisUtils.deleteItemDetailCache(item.getId());
return baseMapper.updateById(existingItem) > 0;
}
......@@ -73,6 +83,9 @@ public class SweetLostFoundItemServiceImpl extends ServiceImpl<SweetLostFoundIte
}
item.setIsDeleted(1);
lostFoundRedisUtils.deleteItemListCache(item.getPerformanceId());
return baseMapper.updateById(item) > 0;
}
......@@ -82,44 +95,54 @@ public class SweetLostFoundItemServiceImpl extends ServiceImpl<SweetLostFoundIte
return null;
}
SweetLostFoundItem item = baseMapper.selectById(id);
if (item == null || item.getIsDeleted() == 1) {
return null;
SweetLostFoundItemVo itemDetail = lostFoundRedisUtils.getItemDetail(id);
if (itemDetail == null) {
SweetLostFoundItem item = baseMapper.selectById(id);
if (item == null || item.getIsDeleted() == 1) {
return null;
}
SweetLostFoundItemVo vo = new SweetLostFoundItemVo();
BeanUtils.copyProperties(item, vo);
lostFoundRedisUtils.setItemDetail(id, vo);
return vo;
}
SweetLostFoundItemVo vo = new SweetLostFoundItemVo();
BeanUtils.copyProperties(item, vo);
return vo;
return itemDetail;
}
@Override
public List<SweetLostFoundItemVo> getItemList(Integer itemType, String performanceId) {
QueryWrapper<SweetLostFoundItem> queryWrapper = new QueryWrapper<>();
// 过滤逻辑删除的记录
queryWrapper.eq("is_deleted", 0);
// 根据演出ID查询
queryWrapper.eq("performance_id", performanceId.trim());
// 根据物品类型查询
if (itemType != null) {
queryWrapper.eq("item_type", itemType);
List<SweetLostFoundItemVo> itemList = lostFoundRedisUtils.getItemList(performanceId, itemType);
if (itemList == null || itemList.isEmpty()) {
QueryWrapper<SweetLostFoundItem> queryWrapper = new QueryWrapper<>();
// 过滤
queryWrapper.eq("is_deleted", 0);
queryWrapper.eq("performance_id", performanceId.trim());
// 排序
queryWrapper.orderByDesc("create_time");
// 执行查询
List<SweetLostFoundItem> items = baseMapper.selectList(queryWrapper);
// 转换为VO对象列表
List<SweetLostFoundItemVo> itemVos = items.stream()
.map(item -> {
SweetLostFoundItemVo vo = new SweetLostFoundItemVo();
BeanUtils.copyProperties(item, vo);
return vo;
})
.collect(Collectors.toList());
lostFoundRedisUtils.setItemList(performanceId, itemVos);
return itemVos.stream()
.filter(item -> itemType.equals(item.getItemType()))
.collect(Collectors.toList());
}
// 排序:按创建时间降序排列
queryWrapper.orderByDesc("create_time");
// 执行查询
List<SweetLostFoundItem> items = baseMapper.selectList(queryWrapper);
// 转换为VO对象列表
return items.stream()
.map(item -> {
SweetLostFoundItemVo vo = new SweetLostFoundItemVo();
BeanUtils.copyProperties(item, vo);
return vo;
})
.collect(Collectors.toList());
return itemList;
}
}
\ No newline at end of file
package com.liquidnet.service.sweet.utils;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.sweet.vo.SweetLostFoundItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
/**
* 失物招领Redis缓存工具类
*
* @author liquidnet
* @since 2025-01-18
*/
@Slf4j
@Component
public class LostFoundRedisUtils {
@Autowired
private RedisUtil redisUtil;
// Redis key前缀
private static final String LOST_FOUND_KEY_PREFIX = "sweet:lost_found:";
private static final String ITEM_LIST_KEY = LOST_FOUND_KEY_PREFIX + "item_list:";
private static final String ITEM_DETAIL_KEY = LOST_FOUND_KEY_PREFIX + "item_detail:";
private static final String ADMIN_LIST_KEY = LOST_FOUND_KEY_PREFIX + "admin_list:";
// 缓存过期时间(秒)
private static final long LIST_EXPIRE_TIME = 432000; // 5天
private static final long DETAIL_EXPIRE_TIME = 432000; // 5天
/**
* 获取失物信息列表缓存
*
* @param performanceId 演出ID
* @param itemType 物品类型(可选,null表示全部)
* @return 失物信息列表
*/
public List<SweetLostFoundItemVo> getItemList(String performanceId, Integer itemType) {
try {
String key = buildItemListKey(performanceId);
Object cachedValue = redisUtil.get(key);
if (cachedValue != null) {
List<SweetLostFoundItemVo> allItems = (List<SweetLostFoundItemVo>) cachedValue;
// 如果指定了itemType,进行筛选
if (itemType != null) {
return allItems.stream()
.filter(item -> itemType.equals(item.getItemType()))
.collect(Collectors.toList());
}
return allItems;
}
} catch (Exception e) {
log.error("获取失物信息列表缓存失败", e);
}
return null;
}
/**
* 设置失物信息列表缓存
*
* @param performanceId 演出ID
* @param itemList 失物信息列表
*/
public void setItemList(String performanceId, List<SweetLostFoundItemVo> itemList) {
try {
String key = buildItemListKey(performanceId);
redisUtil.set(key, itemList, LIST_EXPIRE_TIME);
} catch (Exception e) {
log.error("设置失物信息列表缓存失败", e);
}
}
/**
* 获取失物信息详情缓存
*
* @param itemId 物品ID
* @return 失物信息详情
*/
public SweetLostFoundItemVo getItemDetail(Long itemId) {
try {
String key = ITEM_DETAIL_KEY + itemId;
Object cachedValue = redisUtil.get(key);
if (cachedValue != null) {
return (SweetLostFoundItemVo) cachedValue;
}
} catch (Exception e) {
log.error("获取失物信息详情缓存失败", e);
}
return null;
}
/**
* 设置失物信息详情缓存
*
* @param itemId 物品ID
* @param itemDetail 失物信息详情
*/
public void setItemDetail(Long itemId, SweetLostFoundItemVo itemDetail) {
try {
String key = ITEM_DETAIL_KEY + itemId;
redisUtil.set(key, itemDetail, DETAIL_EXPIRE_TIME);
} catch (Exception e) {
log.error("设置失物信息详情缓存失败", e);
}
}
/**
* 删除失物信息列表缓存
*
* @param performanceId 演出ID
*/
public void deleteItemListCache(String performanceId) {
try {
String key = buildItemListKey(performanceId);
redisUtil.del(key);
} catch (Exception e) {
log.error("删除失物信息列表缓存失败", e);
}
}
/**
* 删除失物信息详情缓存
*
* @param itemId 物品ID
*/
public void deleteItemDetailCache(Long itemId) {
try {
String key = ITEM_DETAIL_KEY + itemId;
redisUtil.del(key);
} catch (Exception e) {
log.error("删除失物信息详情缓存失败", e);
}
}
/**
* 构建失物信息列表缓存key
*
* @param performanceId 演出ID
* @return 缓存key
*/
private String buildItemListKey(String performanceId) {
return ITEM_LIST_KEY + performanceId;
}
/*private String buildItemListKey(String performanceId) {
StringBuilder keyBuilder = new StringBuilder(ITEM_LIST_KEY);
keyBuilder.append(performanceId);
return keyBuilder.toString();
}*/
/**
* 检查管理员是否存在
*
* @param phone 手机号
* @param performanceId 演出ID
* @return 是否存在
*/
public boolean isAdminExists(String phone, String performanceId) {
try {
String key = ADMIN_LIST_KEY + phone + ":" + performanceId;
return redisUtil.hasKey(key);
} catch (Exception e) {
log.error("检查管理员缓存失败", e);
return false;
}
}
/**
* 设置管理员缓存
*
* @param phone 手机号
* @param performanceId 演出ID
* @param isAdmin 是否是管理员
*/
public void setAdminCache(String phone, String performanceId, boolean isAdmin) {
try {
String key = ADMIN_LIST_KEY + phone + ":" + performanceId;
redisUtil.set(key, isAdmin, 1800); // 30分钟
} catch (Exception e) {
log.error("设置管理员缓存失败", e);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment