1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
@Service public class SocialRedisPipelineService {
@Autowired private RedisPipelineService pipelineService;
@Autowired private RedisPipelineOptimizationService optimizationService;
public void batchCacheUserInfo(List<UserInfo> userInfos) { try { Map<String, Object> userInfoMap = new HashMap<>();
for (UserInfo userInfo : userInfos) { String key = "user:info:" + userInfo.getUserId(); userInfoMap.put(key, userInfo); }
optimizationService.optimizedBatchSet(userInfoMap);
log.info("批量缓存用户信息完成,数量: {}", userInfos.size());
} catch (Exception e) { log.error("批量缓存用户信息失败", e); throw new SocialException("批量缓存用户信息失败", e); } }
public Map<String, UserInfo> batchGetUserInfo(List<String> userIds) { try { List<String> keys = new ArrayList<>();
for (String userId : userIds) { String key = "user:info:" + userId; keys.add(key); }
Map<String, Object> resultMap = optimizationService.optimizedBatchGet(keys);
Map<String, UserInfo> userInfoMap = new HashMap<>(); for (Map.Entry<String, Object> entry : resultMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue();
if (value instanceof UserInfo) { String userId = key.substring("user:info:".length()); userInfoMap.put(userId, (UserInfo) value); } }
log.info("批量获取用户信息完成,数量: {}", userIds.size()); return userInfoMap;
} catch (Exception e) { log.error("批量获取用户信息失败", e); throw new SocialException("批量获取用户信息失败", e); } }
public void batchCachePostInfo(List<PostInfo> postInfos) { try { Map<String, Object> postInfoMap = new HashMap<>();
for (PostInfo postInfo : postInfos) { String key = "post:info:" + postInfo.getPostId(); postInfoMap.put(key, postInfo); }
optimizationService.optimizedBatchSet(postInfoMap);
log.info("批量缓存帖子信息完成,数量: {}", postInfos.size());
} catch (Exception e) { log.error("批量缓存帖子信息失败", e); throw new SocialException("批量缓存帖子信息失败", e); } }
public Map<String, PostInfo> batchGetPostInfo(List<String> postIds) { try { List<String> keys = new ArrayList<>();
for (String postId : postIds) { String key = "post:info:" + postId; keys.add(key); }
Map<String, Object> resultMap = optimizationService.optimizedBatchGet(keys);
Map<String, PostInfo> postInfoMap = new HashMap<>(); for (Map.Entry<String, Object> entry : resultMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue();
if (value instanceof PostInfo) { String postId = key.substring("post:info:".length()); postInfoMap.put(postId, (PostInfo) value); } }
log.info("批量获取帖子信息完成,数量: {}", postIds.size()); return postInfoMap;
} catch (Exception e) { log.error("批量获取帖子信息失败", e); throw new SocialException("批量获取帖子信息失败", e); } }
public void batchCacheCommentInfo(List<CommentInfo> commentInfos) { try { Map<String, Object> commentInfoMap = new HashMap<>();
for (CommentInfo commentInfo : commentInfos) { String key = "comment:info:" + commentInfo.getCommentId(); commentInfoMap.put(key, commentInfo); }
optimizationService.optimizedBatchSet(commentInfoMap);
log.info("批量缓存评论信息完成,数量: {}", commentInfos.size());
} catch (Exception e) { log.error("批量缓存评论信息失败", e); throw new SocialException("批量缓存评论信息失败", e); } }
public Map<String, CommentInfo> batchGetCommentInfo(List<String> commentIds) { try { List<String> keys = new ArrayList<>();
for (String commentId : commentIds) { String key = "comment:info:" + commentId; keys.add(key); }
Map<String, Object> resultMap = optimizationService.optimizedBatchGet(keys);
Map<String, CommentInfo> commentInfoMap = new HashMap<>(); for (Map.Entry<String, Object> entry : resultMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue();
if (value instanceof CommentInfo) { String commentId = key.substring("comment:info:".length()); commentInfoMap.put(commentId, (CommentInfo) value); } }
log.info("批量获取评论信息完成,数量: {}", commentIds.size()); return commentInfoMap;
} catch (Exception e) { log.error("批量获取评论信息失败", e); throw new SocialException("批量获取评论信息失败", e); } }
public void batchUpdateUserFollow(String userId, Set<String> followUserIds) { try { String followKey = "user:follow:" + userId;
pipelineService.batchSetOperations(followKey, followUserIds);
log.info("批量更新用户关注完成,用户: {}, 关注数量: {}", userId, followUserIds.size());
} catch (Exception e) { log.error("批量更新用户关注失败", e); throw new SocialException("批量更新用户关注失败", e); } }
public Set<String> batchGetUserFollow(String userId) { try { String followKey = "user:follow:" + userId;
Set<Object> followSet = pipelineService.getSetMembers(followKey);
Set<String> followUserIds = new HashSet<>(); for (Object follow : followSet) { if (follow instanceof String) { followUserIds.add((String) follow); } }
log.info("批量获取用户关注完成,用户: {}", userId); return followUserIds;
} catch (Exception e) { log.error("批量获取用户关注失败", e); throw new SocialException("批量获取用户关注失败", e); } }
public void batchUpdateUserFans(String userId, Set<String> fanUserIds) { try { String fanKey = "user:fan:" + userId;
pipelineService.batchSetOperations(fanKey, fanUserIds);
log.info("批量更新用户粉丝完成,用户: {}, 粉丝数量: {}", userId, fanUserIds.size());
} catch (Exception e) { log.error("批量更新用户粉丝失败", e); throw new SocialException("批量更新用户粉丝失败", e); } }
public Set<String> batchGetUserFans(String userId) { try { String fanKey = "user:fan:" + userId;
Set<Object> fanSet = pipelineService.getSetMembers(fanKey);
Set<String> fanUserIds = new HashSet<>(); for (Object fan : fanSet) { if (fan instanceof String) { fanUserIds.add((String) fan); } }
log.info("批量获取用户粉丝完成,用户: {}", userId); return fanUserIds;
} catch (Exception e) { log.error("批量获取用户粉丝失败", e); throw new SocialException("批量获取用户粉丝失败", e); } }
public void batchUpdatePostLike(String postId, Set<String> likeUserIds) { try { String likeKey = "post:like:" + postId;
pipelineService.batchSetOperations(likeKey, likeUserIds);
log.info("批量更新帖子点赞完成,帖子: {}, 点赞数量: {}", postId, likeUserIds.size());
} catch (Exception e) { log.error("批量更新帖子点赞失败", e); throw new SocialException("批量更新帖子点赞失败", e); } }
public Set<String> batchGetPostLike(String postId) { try { String likeKey = "post:like:" + postId;
Set<Object> likeSet = pipelineService.getSetMembers(likeKey);
Set<String> likeUserIds = new HashSet<>(); for (Object like : likeSet) { if (like instanceof String) { likeUserIds.add((String) like); } }
log.info("批量获取帖子点赞完成,帖子: {}", postId); return likeUserIds;
} catch (Exception e) { log.error("批量获取帖子点赞失败", e); throw new SocialException("批量获取帖子点赞失败", e); } }
public void batchUpdatePostComment(String postId, List<String> commentIds) { try { String commentKey = "post:comment:" + postId;
pipelineService.batchListOperations(commentKey, commentIds, false);
log.info("批量更新帖子评论完成,帖子: {}, 评论数量: {}", postId, commentIds.size());
} catch (Exception e) { log.error("批量更新帖子评论失败", e); throw new SocialException("批量更新帖子评论失败", e); } }
public List<String> batchGetPostComment(String postId, int start, int end) { try { String commentKey = "post:comment:" + postId;
List<Object> commentList = pipelineService.getListRange(commentKey, start, end);
List<String> commentIds = new ArrayList<>(); for (Object comment : commentList) { if (comment instanceof String) { commentIds.add((String) comment); } }
log.info("批量获取帖子评论完成,帖子: {}", postId); return commentIds;
} catch (Exception e) { log.error("批量获取帖子评论失败", e); throw new SocialException("批量获取帖子评论失败", e); } } }
|