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
|
@Service public class MyBatisCacheOptimizationService {
@Autowired private UserMapper userMapper;
@Autowired private RedisTemplate<String, Object> redisTemplate;
@Autowired private CaffeineCache localCache;
private final String CACHE_OPTIMIZATION_PREFIX = "cache_optimization:"; private final long CACHE_EXPIRE_TIME = 3600;
public List<User> searchUsersByFirstLevelCache(String keyword) { try { List<User> users = userMapper.findUsersByFirstLevelCache(keyword);
log.info("一级缓存查询完成: 关键词={}, 结果数量={}", keyword, users.size());
return users;
} catch (Exception e) { log.error("一级缓存查询失败", e); throw new MyBatisOptimizationException("一级缓存查询失败", e); } }
public List<User> searchUsersBySecondLevelCache(String keyword) { try { List<User> users = userMapper.findUsersBySecondLevelCache(keyword);
log.info("二级缓存查询完成: 关键词={}, 结果数量={}", keyword, users.size());
return users;
} catch (Exception e) { log.error("二级缓存查询失败", e); throw new MyBatisOptimizationException("二级缓存查询失败", e); } }
public List<User> searchUsersByRedisCache(String keyword) { try { String cacheKey = CACHE_OPTIMIZATION_PREFIX + "redis:" + keyword;
List<User> cachedUsers = (List<User>) redisTemplate.opsForValue().get(cacheKey); if (cachedUsers != null) { log.info("Redis缓存命中: 关键词={}, 结果数量={}", keyword, cachedUsers.size()); return cachedUsers; }
List<User> users = userMapper.findUsersByKeyword(keyword);
redisTemplate.opsForValue().set(cacheKey, users, Duration.ofSeconds(CACHE_EXPIRE_TIME));
log.info("Redis缓存查询完成: 关键词={}, 结果数量={}", keyword, users.size());
return users;
} catch (Exception e) { log.error("Redis缓存查询失败", e); throw new MyBatisOptimizationException("Redis缓存查询失败", e); } }
public List<User> searchUsersByLocalCache(String keyword) { try { String cacheKey = CACHE_OPTIMIZATION_PREFIX + "local:" + keyword;
List<User> cachedUsers = (List<User>) localCache.getIfPresent(cacheKey); if (cachedUsers != null) { log.info("本地缓存命中: 关键词={}, 结果数量={}", keyword, cachedUsers.size()); return cachedUsers; }
List<User> users = userMapper.findUsersByKeyword(keyword);
localCache.put(cacheKey, users);
log.info("本地缓存查询完成: 关键词={}, 结果数量={}", keyword, users.size());
return users;
} catch (Exception e) { log.error("本地缓存查询失败", e); throw new MyBatisOptimizationException("本地缓存查询失败", e); } }
public List<User> searchUsersByMultiLevelCache(String keyword) { try { String cacheKey = CACHE_OPTIMIZATION_PREFIX + "multi:" + keyword;
List<User> cachedUsers = (List<User>) localCache.getIfPresent(cacheKey); if (cachedUsers != null) { log.info("本地缓存命中: 关键词={}, 结果数量={}", keyword, cachedUsers.size()); return cachedUsers; }
cachedUsers = (List<User>) redisTemplate.opsForValue().get(cacheKey); if (cachedUsers != null) { localCache.put(cacheKey, cachedUsers); log.info("Redis缓存命中: 关键词={}, 结果数量={}", keyword, cachedUsers.size()); return cachedUsers; }
List<User> users = userMapper.findUsersByKeyword(keyword);
localCache.put(cacheKey, users); redisTemplate.opsForValue().set(cacheKey, users, Duration.ofSeconds(CACHE_EXPIRE_TIME));
log.info("多级缓存查询完成: 关键词={}, 结果数量={}", keyword, users.size());
return users;
} catch (Exception e) { log.error("多级缓存查询失败", e); throw new MyBatisOptimizationException("多级缓存查询失败", e); } }
public void warmupCache(List<String> keywords) { try { for (String keyword : keywords) { try { searchUsersByMultiLevelCache(keyword);
} catch (Exception e) { log.error("缓存预热失败: {}", keyword, e); } }
log.info("缓存预热完成: 关键词数量={}", keywords.size());
} catch (Exception e) { log.error("缓存预热失败", e); } }
public void clearCache(String keyword) { try { String cacheKey = CACHE_OPTIMIZATION_PREFIX + "multi:" + keyword;
localCache.invalidate(cacheKey);
redisTemplate.delete(cacheKey);
log.info("缓存清理完成: 关键词={}", keyword);
} catch (Exception e) { log.error("缓存清理失败", e); } }
public CacheHitRateAnalysis analyzeCacheHitRate() { try { CacheHitRateAnalysis analysis = new CacheHitRateAnalysis(); analysis.setAnalysisTime(LocalDateTime.now());
analysis.setLocalCacheHitRate(analyzeLocalCacheHitRate());
analysis.setRedisCacheHitRate(analyzeRedisCacheHitRate());
analysis.setMultiLevelCacheHitRate(analyzeMultiLevelCacheHitRate());
return analysis;
} catch (Exception e) { log.error("分析缓存命中率失败", e); throw new MyBatisOptimizationException("分析缓存命中率失败", e); } }
private double analyzeLocalCacheHitRate() { try { return 0.85;
} catch (Exception e) { log.error("分析本地缓存命中率失败", e); return 0.0; } }
private double analyzeRedisCacheHitRate() { try { return 0.90;
} catch (Exception e) { log.error("分析Redis缓存命中率失败", e); return 0.0; } }
private double analyzeMultiLevelCacheHitRate() { try { return 0.95;
} catch (Exception e) { log.error("分析多级缓存命中率失败", e); return 0.0; } } }
|