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
| @Service public class CacheWarmupService { @Autowired private ProductService productService; @Autowired private UserService userService; @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private TaskExecutor taskExecutor; @EventListener(ApplicationReadyEvent.class) public void warmupCache() { log.info("Starting cache warmup..."); CompletableFuture<Void> productWarmup = CompletableFuture.runAsync( this::warmupProductCache, taskExecutor ); CompletableFuture<Void> userWarmup = CompletableFuture.runAsync( this::warmupUserCache, taskExecutor ); CompletableFuture<Void> configWarmup = CompletableFuture.runAsync( this::warmupConfigCache, taskExecutor ); CompletableFuture.allOf(productWarmup, userWarmup, configWarmup) .thenRun(() -> log.info("Cache warmup completed")) .exceptionally(throwable -> { log.error("Cache warmup failed", throwable); return null; }); } private void warmupProductCache() { try { log.info("Warming up product cache..."); List<Long> hotProductIds = productService.getHotProductIds(100); for (Long productId : hotProductIds) { try { productService.getProduct(productId); Thread.sleep(10); } catch (Exception e) { log.warn("Failed to warmup product: {}", productId, e); } } log.info("Product cache warmup completed: {} products", hotProductIds.size()); } catch (Exception e) { log.error("Product cache warmup failed", e); } } private void warmupUserCache() { try { log.info("Warming up user cache..."); List<Long> activeUserIds = userService.getActiveUserIds(1000); int batchSize = 50; for (int i = 0; i < activeUserIds.size(); i += batchSize) { int endIndex = Math.min(i + batchSize, activeUserIds.size()); List<Long> batch = activeUserIds.subList(i, endIndex); for (Long userId : batch) { try { userService.getUser(userId); } catch (Exception e) { log.warn("Failed to warmup user: {}", userId, e); } } Thread.sleep(100); } log.info("User cache warmup completed: {} users", activeUserIds.size()); } catch (Exception e) { log.error("User cache warmup failed", e); } } private void warmupConfigCache() { try { log.info("Warming up config cache..."); Map<String, Object> configs = Map.of( "system.settings", getSystemSettings(), "feature.flags", getFeatureFlags(), "rate.limits", getRateLimits() ); for (Map.Entry<String, Object> entry : configs.entrySet()) { redisTemplate.opsForValue().set( "config:" + entry.getKey(), entry.getValue(), 24, TimeUnit.HOURS ); } log.info("Config cache warmup completed: {} configs", configs.size()); } catch (Exception e) { log.error("Config cache warmup failed", e); } } @Scheduled(fixedRate = 300000) public void refreshHotData() { try { List<Long> currentHotProducts = productService.getHotProductIds(50); for (Long productId : currentHotProducts) { CompletableFuture.runAsync(() -> { try { Product product = productService.getProductFromDatabase(productId); if (product != null) { String cacheKey = "product:" + productId; redisTemplate.opsForValue().set(cacheKey, product, 30, TimeUnit.MINUTES); } } catch (Exception e) { log.warn("Failed to refresh hot product: {}", productId, e); } }, taskExecutor); } log.debug("Hot data refresh initiated for {} products", currentHotProducts.size()); } catch (Exception e) { log.error("Hot data refresh failed", e); } } private Object getSystemSettings() { return Map.of( "maintenance.mode", false, "max.upload.size", "10MB", "session.timeout", 3600 ); } private Object getFeatureFlags() { return Map.of( "new.ui.enabled", true, "payment.v2.enabled", false, "recommendation.enabled", true ); } private Object getRateLimits() { return Map.of( "api.rate.limit", 1000, "login.rate.limit", 10, "search.rate.limit", 100 ); } }
|