| | |
| | | Boolean redisFlag = false; |
| | | if (MapUtils.isNotEmpty(redisMap)) { |
| | | long tCmp = System.currentTimeMillis(); |
| | | ServiceSubtaskEntity oldCondition = (ServiceSubtaskEntity) redisMap.get("searchCondition"); |
| | | ServiceSubtaskEntity oldCondition = convertCachedCondition(redisMap.get("searchCondition")); |
| | | if (ObjectUtils.isNotEmpty(oldCondition) && isSameCondition(oldCondition, serviceSubtaskEntity)) { |
| | | redisFlag = true; |
| | | map = redisMap; |
| | | // 顺手回填转换后的实体,让这条缓存下次能以正确类型反序列化 |
| | | map.put("searchCondition", oldCondition); |
| | | redisCache.setCacheObject(userId + "patItemCount", map, 60, TimeUnit.MINUTES); |
| | | } |
| | | log.info("[patItem耗时] 4.2 条件比对={}ms,命中={}", System.currentTimeMillis() - tCmp, redisFlag); |
| | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 把 Redis 缓存里的查询条件还原成 {@link ServiceSubtaskEntity}。 |
| | | * <p> |
| | | * 缓存由 {@code FastJson2JsonRedisSerializer} 按 {@code Object.class} 反序列化: |
| | | * 当 {@code searchCondition} 的 {@code @type} 缺失、或指向已迁移的旧包名 |
| | | * (如 {@code com.smartor.domain.entity.ServiceSubtaskEntity})时,fastjson2 会 |
| | | * 静默退化成 {@link com.alibaba.fastjson2.JSONObject},直接强转会抛 |
| | | * {@code ClassCastException}。这里统一转换;转换失败返回 {@code null}, |
| | | * 调用方按缓存未命中处理(重新查库),避免整个接口 500。 |
| | | */ |
| | | private ServiceSubtaskEntity convertCachedCondition(Object cachedCondition) { |
| | | if (cachedCondition == null) { |
| | | return null; |
| | | } |
| | | if (cachedCondition instanceof ServiceSubtaskEntity) { |
| | | return (ServiceSubtaskEntity) cachedCondition; |
| | | } |
| | | try { |
| | | // 不能直接用 TypeUtils.cast / toJavaObject:旧缓存里的 @type 可能指向已迁移的类, |
| | | // fastjson2 会按 @type 去找类并抛 "No suitable ObjectReader found"。 |
| | | // 转成 JSON 字符串后按目标类解析,未开启 SupportAutoType 时 @type 只会被当作普通字段忽略。 |
| | | return com.alibaba.fastjson2.JSON.parseObject( |
| | | com.alibaba.fastjson2.JSON.toJSONString(cachedCondition), |
| | | ServiceSubtaskEntity.class); |
| | | } catch (Exception e) { |
| | | log.warn("【patItemCount】缓存中的查询条件无法还原为 ServiceSubtaskEntity,按缓存未命中处理,实际类型:{}", |
| | | cachedCondition.getClass().getName(), e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 比较两个ServiceSubtaskVO的查询条件是否一致 |