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
|
@Service @Slf4j public class RedisCacheService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private RedisCacheProperties properties;
public void set(String key, Object value, int ttl) { log.info("设置缓存,键: {}, TTL: {}", key, ttl); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } if (value == null) { throw new IllegalArgumentException("缓存值不能为空"); } String fullKey = buildKey(key); redisTemplate.opsForValue().set(fullKey, value, Duration.ofSeconds(ttl)); log.info("设置缓存成功,键: {}", key); } catch (Exception e) { log.error("设置缓存异常,键: {}", key, e); throw new RuntimeException("设置缓存失败: " + e.getMessage()); } }
public void set(String key, Object value) { set(key, value, properties.getDefaultTtl() * 60); }
public Object get(String key) { log.info("获取缓存,键: {}", key); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } String fullKey = buildKey(key); Object value = redisTemplate.opsForValue().get(fullKey); if (value != null) { log.info("获取缓存成功,键: {}", key); } else { log.warn("缓存不存在,键: {}", key); } return value; } catch (Exception e) { log.error("获取缓存异常,键: {}", key, e); return null; } }
@SuppressWarnings("unchecked") public <T> T get(String key, Class<T> clazz) { Object value = get(key); if (value != null && clazz.isAssignableFrom(value.getClass())) { return (T) value; } return null; }
public boolean delete(String key) { log.info("删除缓存,键: {}", key); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } String fullKey = buildKey(key); boolean result = Boolean.TRUE.equals(redisTemplate.delete(fullKey)); log.info("删除缓存结果,键: {}, 结果: {}", key, result); return result; } catch (Exception e) { log.error("删除缓存异常,键: {}", key, e); return false; } }
public boolean exists(String key) { log.info("检查缓存是否存在,键: {}", key); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } String fullKey = buildKey(key); boolean result = Boolean.TRUE.equals(redisTemplate.hasKey(fullKey)); log.info("缓存存在检查结果,键: {}, 结果: {}", key, result); return result; } catch (Exception e) { log.error("检查缓存是否存在异常,键: {}", key, e); return false; } }
public boolean expire(String key, int ttl) { log.info("设置缓存过期时间,键: {}, TTL: {}", key, ttl); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } String fullKey = buildKey(key); boolean result = Boolean.TRUE.equals(redisTemplate.expire(fullKey, Duration.ofSeconds(ttl))); log.info("设置缓存过期时间结果,键: {}, 结果: {}", key, result); return result; } catch (Exception e) { log.error("设置缓存过期时间异常,键: {}", key, e); return false; } }
public long getExpire(String key) { log.info("获取缓存过期时间,键: {}", key); try { if (key == null || key.isEmpty()) { throw new IllegalArgumentException("缓存键不能为空"); } String fullKey = buildKey(key); Long expire = redisTemplate.getExpire(fullKey); long result = expire != null ? expire : -1; log.info("获取缓存过期时间结果,键: {}, 过期时间: {}", key, result); return result; } catch (Exception e) { log.error("获取缓存过期时间异常,键: {}", key, e); return -1; } }
public long delete(String... keys) { log.info("批量删除缓存,键数量: {}", keys.length); try { if (keys == null || keys.length == 0) { throw new IllegalArgumentException("缓存键列表不能为空"); } String[] fullKeys = Arrays.stream(keys) .map(this::buildKey) .toArray(String[]::new); Long result = redisTemplate.delete(Arrays.asList(fullKeys)); long deleteCount = result != null ? result : 0; log.info("批量删除缓存结果,请求数量: {}, 删除数量: {}", keys.length, deleteCount); return deleteCount; } catch (Exception e) { log.error("批量删除缓存异常,键数量: {}", keys.length, e); return 0; } }
public CacheStatistics getStatistics() { log.info("获取缓存统计信息"); try { CacheStatistics statistics = new CacheStatistics(); Properties info = redisTemplate.getConnectionFactory() .getConnection() .info(); statistics.setTotalKeys(Long.parseLong(info.getProperty("db0", "0"))); statistics.setUsedMemory(Long.parseLong(info.getProperty("used_memory", "0"))); statistics.setConnectedClients(Long.parseLong(info.getProperty("connected_clients", "0"))); statistics.setTotalCommandsProcessed(Long.parseLong(info.getProperty("total_commands_processed", "0"))); statistics.setLastUpdateTime(LocalDateTime.now()); log.info("获取缓存统计信息成功"); return statistics; } catch (Exception e) { log.error("获取缓存统计信息异常", e); return null; } }
private String buildKey(String key) { return properties.getKeyPrefix() + key; } }
|