1. 在pom.xml 添加redis依賴
1 <!-- redis --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-redis</artifactId> 5 </dependency>
2.配置 application.properties 配置文件
1 # redis配置 2 # Redis數據庫索引(默認為0) 3 spring.redis.database=0 4 # Redis服務器地址 5 spring.redis.host=127.0.0.1 6 # Redis服務器連接端口 7 spring.redis.port=6379 8 # Redis服務器連接密碼(默認為空) 9 spring.redis.password=
3. 自定義redis配置類
1 import com.fasterxml.jackson.annotation.JsonAutoDetect; 2 import com.fasterxml.jackson.annotation.PropertyAccessor; 3 import com.fasterxml.jackson.databind.ObjectMapper; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.data.redis.connection.RedisConnectionFactory; 7 import org.springframework.data.redis.core.RedisTemplate; 8 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 9 import org.springframework.data.redis.serializer.StringRedisSerializer; 10 11 /** 12 * redis 配置類 13 * @author zz 14 */ 15 @Configuration 16 public class RedisConfig { 17 18 @Bean 19 @SuppressWarnings("all") 20 public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) { 21 RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); 22 template.setConnectionFactory(factory); 23 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 24 ObjectMapper om = new ObjectMapper(); 25 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 26 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 27 jackson2JsonRedisSerializer.setObjectMapper(om); 28 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); 29 // key采用String的序列化方式 30 template.setKeySerializer(stringRedisSerializer); 31 // hash的key也采用String的序列化方式 32 template.setHashKeySerializer(stringRedisSerializer); 33 // value序列化方式采用jackson 34 template.setValueSerializer(jackson2JsonRedisSerializer); 35 // hash的value序列化方式采用jackson 36 template.setHashValueSerializer(jackson2JsonRedisSerializer); 37 template.afterPropertiesSet(); 38 return template; 39 } 40 }
4.封裝redis工具
1 import java.util.List; 2 import java.util.Map; 3 import java.util.Set; 4 import java.util.concurrent.TimeUnit; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.data.redis.core.RedisTemplate; 7 import org.springframework.stereotype.Component; 8 import org.springframework.util.CollectionUtils; 9 /** 10 * Redis工具類 11 * @author zz 12 * @date 2020年6月24日 13 */ 14 @Component 15 public final class RedisUtil { 16 @Autowired 17 private RedisTemplate<String, Object> redisTemplate; 18 19 /** 20 * 指定緩存失效時間 21 * @param key 鍵 22 * @param time 時間(秒) 23 * @return 24 */ 25 public boolean expire(String key, long time) { 26 try { 27 if (time > 0) { 28 redisTemplate.expire(key, time, TimeUnit.SECONDS); 29 } 30 return true; 31 } catch (Exception e) { 32 e.printStackTrace(); 33 return false; 34 } 35 } 36 37 /** 38 * 根據key 獲取過期時間 39 * @param key 鍵 不能為null 40 * @return 時間(秒) 返回0代表為永久有效 41 */ 42 public long getExpire(String key) { 43 return redisTemplate.getExpire(key, TimeUnit.SECONDS); 44 } 45 46 /** 47 * 判斷key是否存在 48 * @param key 鍵 49 * @return true 存在 false不存在 50 */ 51 public boolean hasKey(String key) { 52 try { 53 return redisTemplate.hasKey(key); 54 } catch (Exception e) { 55 e.printStackTrace(); 56 return false; 57 } 58 } 59 60 /** 61 * 刪除緩存 62 * @param key 可以傳一個值 或多個 63 */ 64 @SuppressWarnings("unchecked") 65 public void del(String... key) { 66 if (key != null && key.length > 0) { 67 if (key.length == 1) { 68 redisTemplate.delete(key[0]); 69 } else { 70 redisTemplate.delete(CollectionUtils.arrayToList(key)); 71 } 72 } 73 } 74 75 /** 76 * 普通緩存獲取 77 * @param key 鍵 78 * @return 值 79 */ 80 public Object get(String key) { 81 return key == null ? null : redisTemplate.opsForValue().get(key); 82 } 83 84 /** 85 * 普通緩存放入 86 * @param key 鍵 87 * @param value 值 88 * @return true成功 false失敗 89 */ 90 public boolean set(String key, Object value) { 91 try { 92 redisTemplate.opsForValue().set(key, value); 93 return true; 94 } catch (Exception e) { 95 e.printStackTrace(); 96 return false; 97 } 98 } 99 100 /** 101 * 普通緩存放入並設置時間 102 * @param key 鍵 103 * @param value 值 104 * @param time 時間(秒) time要大於0 如果time小於等於0 將設置無限期 105 * @return true成功 false 失敗 106 */ 107 public boolean set(String key, Object value, long time) { 108 try { 109 if (time > 0) { 110 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); 111 } else { 112 set(key, value); 113 } 114 return true; 115 } catch (Exception e) { 116 e.printStackTrace(); 117 return false; 118 } 119 } 120 /** 121 * 遞增 122 * @param key 鍵 123 * @param delta 要增加幾(大於0) 124 * @return 125 */ 126 public long incr(String key, long delta) { 127 if (delta < 0) { 128 throw new RuntimeException("遞增因子必須大於0"); 129 } 130 return redisTemplate.opsForValue().increment(key, delta); 131 } 132 133 /** 134 * 遞減 135 * @param key 鍵 136 * @param delta 要減少幾(小於0) 137 * @return 138 */ 139 public long decr(String key, long delta) { 140 if (delta < 0) { 141 throw new RuntimeException("遞減因子必須大於0"); 142 } 143 return redisTemplate.opsForValue().increment(key, -delta); 144 } 145 146 /** 147 * HashGet 148 * @param key 鍵 不能為null 149 * @param item 項 不能為null 150 * @return 值 151 */ 152 public Object hget(String key, String item) { 153 return redisTemplate.opsForHash().get(key, item); 154 } 155 156 /** 157 * 獲取hashKey對應的所有鍵值 158 * @param key 鍵 159 * @return 對應的多個鍵值 160 */ 161 public Map<Object, Object> hmget(String key) { 162 return redisTemplate.opsForHash().entries(key); 163 } 164 165 /** 166 * HashSet 167 * @param key 鍵 168 * @param map 對應多個鍵值 169 * @return true 成功 false 失敗 170 */ 171 public boolean hmset(String key, Map<String, Object> map) { 172 try { 173 redisTemplate.opsForHash().putAll(key, map); 174 return true; 175 } catch (Exception e) { 176 e.printStackTrace(); 177 return false; 178 } 179 } 180 181 /** 182 * HashSet 並設置時間 183 * @param key 鍵 184 * @param map 對應多個鍵值 185 * @param time 時間(秒) 186 * @return true成功 false失敗 187 */ 188 public boolean hmset(String key, Map<String, Object> map, long time) { 189 try { 190 redisTemplate.opsForHash().putAll(key, map); 191 if (time > 0) { 192 expire(key, time); 193 } 194 return true; 195 } catch (Exception e) { 196 e.printStackTrace(); 197 return false; 198 } 199 } 200 201 /** 202 * 向一張hash表中放入數據,如果不存在將創建 203 * @param key 鍵 204 * @param item 項 205 * @param value 值 206 * @return true 成功 false失敗 207 */ 208 public boolean hset(String key, String item, Object value) { 209 try { 210 redisTemplate.opsForHash().put(key, item, value); 211 return true; 212 } catch (Exception e) { 213 e.printStackTrace(); 214 return false; 215 } 216 } 217 218 /** 219 * 向一張hash表中放入數據,如果不存在將創建 220 * @param key 鍵 221 * @param item 項 222 * @param value 值 223 * @param time 時間(秒) 注意:如果已存在的hash表有時間,這里將會替換原有的時間 224 * @return true 成功 false失敗 225 */ 226 public boolean hset(String key, String item, Object value, long time) { 227 try { 228 redisTemplate.opsForHash().put(key, item, value); 229 if (time > 0) { 230 expire(key, time); 231 } 232 return true; 233 } catch (Exception e) { 234 e.printStackTrace(); 235 return false; 236 } 237 } 238 239 /** 240 * 刪除hash表中的值 241 * @param key 鍵 不能為null 242 * @param item 項 可以使多個 不能為null 243 */ 244 245 public void hdel(String key, Object... item) { 246 redisTemplate.opsForHash().delete(key, item); 247 } 248 249 /** 250 * 判斷hash表中是否有該項的值 251 * @param key 鍵 不能為null 252 * @param item 項 不能為null 253 * @return true 存在 false不存在 254 */ 255 public boolean hHasKey(String key, String item) { 256 return redisTemplate.opsForHash().hasKey(key, item); 257 } 258 259 /** 260 * hash遞增 如果不存在,就會創建一個 並把新增后的值返回 261 * @param key 鍵 262 * @param item 項 263 * @param by 要增加幾(大於0) 264 * @return 265 */ 266 public double hincr(String key, String item, double by) { 267 return redisTemplate.opsForHash().increment(key, item, by); 268 } 269 270 /** 271 * hash遞減 272 * @param key 鍵 273 * @param item 項 274 * @param by 要減少記(小於0) 275 * @return 276 */ 277 public double hdecr(String key, String item, double by) { 278 return redisTemplate.opsForHash().increment(key, item, -by); 279 } 280 281 /** 282 * 根據key獲取Set中的所有值 283 * @param key 鍵 284 * @return 285 */ 286 public Set<Object> sGet(String key) { 287 try { 288 return redisTemplate.opsForSet().members(key); 289 } catch (Exception e) { 290 e.printStackTrace(); 291 return null; 292 } 293 } 294 295 /** 296 * 根據value從一個set中查詢,是否存在 297 * @param key 鍵 298 * @param value 值 299 * @return true 存在 false不存在 300 */ 301 public boolean sHasKey(String key, Object value) { 302 try { 303 return redisTemplate.opsForSet().isMember(key, value); 304 } catch (Exception e) { 305 e.printStackTrace(); 306 return false; 307 } 308 } 309 310 /** 311 * 將數據放入set緩存 312 * @param key 鍵 313 * @param values 值 可以是多個 314 * @return 成功個數 315 */ 316 public long sSet(String key, Object... values) { 317 try { 318 return redisTemplate.opsForSet().add(key, values); 319 } catch (Exception e) { 320 e.printStackTrace(); 321 return 0; 322 } 323 } 324 325 /** 326 * 將set數據放入緩存 327 * @param key 鍵 328 * @param time 時間(秒) 329 * @param values 值 可以是多個 330 * @return 成功個數 331 */ 332 public long sSetAndTime(String key, long time, Object... values) { 333 try { 334 Long count = redisTemplate.opsForSet().add(key, values); 335 if (time > 0) { 336 expire(key, time); 337 } 338 return count; 339 } catch (Exception e) { 340 e.printStackTrace(); 341 return 0; 342 } 343 } 344 345 /** 346 * 獲取set緩存的長度 347 * @param key 鍵 348 * @return 349 */ 350 public long sGetSetSize(String key) { 351 try { 352 return redisTemplate.opsForSet().size(key); 353 } catch (Exception e) { 354 e.printStackTrace(); 355 return 0; 356 } 357 } 358 359 /** 360 * 移除值為value的 361 * @param key 鍵 362 * @param values 值 可以是多個 363 * @return 移除的個數 364 */ 365 public long setRemove(String key, Object... values) { 366 try { 367 Long count = redisTemplate.opsForSet().remove(key, values); 368 return count; 369 } catch (Exception e) { 370 e.printStackTrace(); 371 return 0; 372 } 373 } 374 375 /** 376 * 獲取list緩存的內容 377 * @param key 鍵 378 * @param start 開始 379 * @param end 結束 0 到 -1代表所有值 380 * @return 381 */ 382 public List<Object> lGet(String key, long start, long end) { 383 try { 384 return redisTemplate.opsForList().range(key, start, end); 385 } catch (Exception e) { 386 e.printStackTrace(); 387 return null; 388 } 389 } 390 391 /** 392 * 獲取list緩存的長度 393 * @param key 鍵 394 * @return 395 */ 396 public long lGetListSize(String key) { 397 try { 398 return redisTemplate.opsForList().size(key); 399 } catch (Exception e) { 400 e.printStackTrace(); 401 return 0; 402 } 403 } 404 405 /** 406 * 通過索引 獲取list中的值 407 * @param key 鍵 408 * @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數第二個元素,依次類推 409 * @return 410 */ 411 public Object lGetIndex(String key, long index) { 412 try { 413 return redisTemplate.opsForList().index(key, index); 414 } catch (Exception e) { 415 e.printStackTrace(); 416 return null; 417 } 418 } 419 420 /** 421 * 將list放入緩存 422 * @param key 鍵 423 * @param value 值 424 * @return 425 */ 426 public boolean lSet(String key, Object value) { 427 try { 428 redisTemplate.opsForList().rightPush(key, value); 429 return true; 430 } catch (Exception e) { 431 e.printStackTrace(); 432 return false; 433 } 434 } 435 436 /** 437 * 將list放入緩存 438 * @param key 鍵 439 * @param value 值 440 * @param time 時間(秒) 441 * @return 442 */ 443 public boolean lSet(String key, Object value, long time) { 444 try { 445 redisTemplate.opsForList().rightPush(key, value); 446 if (time > 0) { 447 expire(key, time); 448 } 449 return true; 450 } catch (Exception e) { 451 e.printStackTrace(); 452 return false; 453 } 454 } 455 456 /** 457 * 將list放入緩存 458 * @param key 鍵 459 * @param value 值 460 * @return 461 */ 462 public boolean lSet(String key, List<Object> value) { 463 try { 464 redisTemplate.opsForList().rightPushAll(key, value); 465 return true; 466 } catch (Exception e) { 467 e.printStackTrace(); 468 return false; 469 } 470 } 471 472 /** 473 * 將list放入緩存 474 * 475 * @param key 鍵 476 * @param value 值 477 * @param time 時間(秒) 478 * @return 479 */ 480 public boolean lSet(String key, List<Object> value, long time) { 481 try { 482 redisTemplate.opsForList().rightPushAll(key, value); 483 if (time > 0) { 484 expire(key, time); 485 } 486 return true; 487 } catch (Exception e) { 488 e.printStackTrace(); 489 return false; 490 } 491 } 492 493 /** 494 * 根據索引修改list中的某條數據 495 * @param key 鍵 496 * @param index 索引 497 * @param value 值 498 * @return 499 */ 500 public boolean lUpdateIndex(String key, long index, Object value) { 501 try { 502 503 redisTemplate.opsForList().set(key, index, value); 504 return true; 505 } catch (Exception e) { 506 e.printStackTrace(); 507 return false; 508 } 509 } 510 511 /** 512 * 移除N個值為value 513 * @param key 鍵 514 * @param count 移除多少個 515 * @param value 值 516 * @return 移除的個數 517 */ 518 public long lRemove(String key, long count, Object value) { 519 try { 520 Long remove = redisTemplate.opsForList().remove(key, count, value); 521 return remove; 522 } catch (Exception e) { 523 e.printStackTrace(); 524 return 0; 525 } 526 } 527 }
5 通過@Autowired spring容器依賴注入到需要調用的Service 層 就可以使用了
1 @Autowired 2 private RedisUtil redisUtil;