1、添加maven包
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.8.1</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.data</groupId> 8 <artifactId>spring-data-redis</artifactId> 9 <version>1.7.2.RELEASE</version> 10 </dependency>
2、添加配置文件redis.properties
1 redis.maxIdle=300 2 redis.maxActive=600 3 redis.maxWait=1000 4 redis.testOnBorrow=true 5 redis.port=6379 6 redis.pass= 7 redis.dbIndex=0 8 redis.host=10.10.10.1
3、添加配置文件spring-redis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.2.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 12 http://www.springframework.org/schema/cache 13 http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"> 14 15 16 <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> 17 18 <!-- redis 相关配置 --> 19 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 20 <property name="maxIdle" value="${redis.maxIdle}" /> 21 <property name="maxTotal" value="${redis.maxActive}" /> 22 <property name="maxWaitMillis" value="${redis.maxWait}" /> 23 <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 24 </bean> 25 26 <bean id="JedisConnectionFactory" 27 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 28 p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" 29 p:pool-config-ref="poolConfig" p:database="${redis.dbIndex}" /> 30 31 32 <!-- redis操作模板,这里采用尽量面向对象的模板 --> 33 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 34 <property name="connectionFactory" ref="JedisConnectionFactory" /> 35 <property name="keySerializer"> 36 <bean 37 class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 38 </property> 39 <property name="valueSerializer"> 40 <bean 41 class="centaline.small.course.tool.GenericFastJson2JsonRedisSerializer" /> 42 </property> 43 <property name="hashKeySerializer"> 44 <bean 45 class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 46 </property> 47 <property name="hashValueSerializer"> 48 <bean 49 class="centaline.small.course.tool.GenericFastJson2JsonRedisSerializer" /> 50 </property> 51 </bean> 52 </beans>
4、添加工具类
1 public class RedisUtil { 2 3 @Autowired 4 private RedisTemplate<String, Object> redisTemplate; 5 6 public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { 7 this.redisTemplate = redisTemplate; 8 } 9 10 11 public boolean expire(String key, long time,TimeUnit timeUnit) { 12 try { 13 if (time > 0) { 14 redisTemplate.expire(key, time, timeUnit); 15 } 16 return true; 17 } catch (Exception e) { 18 e.printStackTrace(); 19 return false; 20 } 21 } 22 23 /** 24 * 根据key 获取过期时间 25 * 26 * @param key 27 * 键 不能为null 28 * @return 时间 返回0代表为永久有效 29 */ 30 public long getExpire(String key) { 31 return redisTemplate.getExpire(key, TimeUnit.SECONDS); 32 } 33 34 /** 35 * 判断key是否存在 36 * 37 * @param key 38 * 键 39 * @return true 存在 false不存在 40 */ 41 public boolean hasKey(String key) { 42 try { 43 return redisTemplate.hasKey(key); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 return false; 47 } 48 } 49 50 /** 51 * 删除缓存 52 * 53 * @param key 54 * 可以传一个值 或多个 55 */ 56 @SuppressWarnings("unchecked") 57 public void del(String... key) { 58 if (key != null && key.length > 0) { 59 if (key.length == 1) { 60 redisTemplate.delete(key[0]); 61 } else { 62 redisTemplate.delete(CollectionUtils.arrayToList(key)); 63 } 64 } 65 } 66 67 // ============================String============================= 68 /** 69 * 普通缓存获取 70 * 71 * @param key 72 * 键 73 * @return 值 74 */ 75 public Object get(String key) { 76 return key == null ? null : redisTemplate.opsForValue().get(key); 77 } 78 79 /** 80 * 普通缓存放入 81 * 82 * @param key 83 * 键 84 * @param value 85 * 值 86 * @return true成功 false失败 87 */ 88 public boolean set(String key, Object value) { 89 try { 90 redisTemplate.opsForValue().set(key, value); 91 return true; 92 } catch (Exception e) { 93 e.printStackTrace(); 94 return false; 95 } 96 97 } 98 99 /** 100 * 普通缓存放入并设置时间 101 * 102 * @param key 103 * 键 104 * @param value 105 * 值 106 * @param time 107 * 时间 time要大于0 如果time小于等于0 将设置无限期 108 * @return true成功 false 失败 109 */ 110 public boolean set(String key, Object value, long time,TimeUnit timeUnit) { 111 try { 112 if (time > 0) { 113 redisTemplate.opsForValue().set(key, value, time,timeUnit); 114 } else { 115 set(key, value); 116 } 117 return true; 118 } catch (Exception e) { 119 e.printStackTrace(); 120 return false; 121 } 122 } 123 124 /** 125 * 递增 126 * 127 * @param key 128 * 键 129 * @param by 130 * 要增加几(大于0) 131 * @return 132 */ 133 public long incr(String key, long delta) { 134 if (delta < 0) { 135 throw new RuntimeException("递增因子必须大于0"); 136 } 137 return redisTemplate.opsForValue().increment(key, delta); 138 } 139 140 /** 141 * 递减 142 * 143 * @param key 144 * 键 145 * @param by 146 * 要减少几(小于0) 147 * @return 148 */ 149 public long decr(String key, long delta) { 150 if (delta < 0) { 151 throw new RuntimeException("递减因子必须大于0"); 152 } 153 return redisTemplate.opsForValue().increment(key, -delta); 154 } 155 156 157 public boolean setNX(String key,String value, long expire) { 158 try { 159 RedisCallback<String> callback = (connection) -> { 160 JedisCommands commands = (JedisCommands) connection.getNativeConnection(); 161 return commands.set(key, value, "NX", "PX", expire); 162 }; 163 String result = redisTemplate.execute(callback); 164 165 return !StringUtils.isEmpty(result); 166 } catch (Exception e) { 167 return false; 168 } 169 } 170 171 public String getNX(String key) { 172 try { 173 RedisCallback<String> callback = (connection) -> { 174 JedisCommands commands = (JedisCommands) connection.getNativeConnection(); 175 return commands.get(key); 176 }; 177 String result = redisTemplate.execute(callback); 178 179 return result; 180 } catch (Exception e) { 181 return ""; 182 } 183 } 184 185 186 // ================================Map================================= 187 /** 188 * HashGet 189 * 190 * @param key 191 * 键 不能为null 192 * @param item 193 * 项 不能为null 194 * @return 值 195 */ 196 public Object hget(String key, String item) { 197 return redisTemplate.opsForHash().get(key, item); 198 } 199 200 /** 201 * 获取hashKey对应的所有键值 202 * 203 * @param key 204 * 键 205 * @return 对应的多个键值 206 */ 207 public Map<Object, Object> hmget(String key) { 208 return redisTemplate.opsForHash().entries(key); 209 } 210 211 /** 212 * HashSet 213 * 214 * @param key 215 * 键 216 * @param map 217 * 对应多个键值 218 * @return true 成功 false 失败 219 */ 220 public boolean hmset(String key, Map<String, Object> map) { 221 try { 222 redisTemplate.opsForHash().putAll(key, map); 223 return true; 224 } catch (Exception e) { 225 e.printStackTrace(); 226 return false; 227 } 228 } 229 230 /** 231 * HashSet 并设置时间 232 * 233 * @param key 234 * 键 235 * @param map 236 * 对应多个键值 237 * @param time 238 * 时间 239 * @return true成功 false失败 240 */ 241 public boolean hmset(String key, Map<String, Object> map, long time,TimeUnit timeUnit) { 242 try { 243 redisTemplate.opsForHash().putAll(key, map); 244 if (time > 0) { 245 expire(key, time,timeUnit); 246 } 247 return true; 248 } catch (Exception e) { 249 e.printStackTrace(); 250 return false; 251 } 252 } 253 254 /** 255 * 向一张hash表中放入数据,如果不存在将创建 256 * 257 * @param key 258 * 键 259 * @param item 260 * 项 261 * @param value 262 * 值 263 * @return true 成功 false失败 264 */ 265 public boolean hset(String key, String item, Object value) { 266 try { 267 redisTemplate.opsForHash().put(key, item, value); 268 return true; 269 } catch (Exception e) { 270 e.printStackTrace(); 271 return false; 272 } 273 } 274 275 /** 276 * 向一张hash表中放入数据,如果不存在将创建 277 * 278 * @param key 279 * 键 280 * @param item 281 * 项 282 * @param value 283 * 值 284 * @param time 285 * 时间 注意:如果已存在的hash表有时间,这里将会替换原有的时间 286 * @return true 成功 false失败 287 */ 288 public boolean hset(String key, String item, Object value, long time,TimeUnit timeUnit) { 289 try { 290 redisTemplate.opsForHash().put(key, item, value); 291 if (time > 0) { 292 expire(key, time,timeUnit); 293 } 294 return true; 295 } catch (Exception e) { 296 e.printStackTrace(); 297 return false; 298 } 299 } 300 301 /** 302 * 删除hash表中的值 303 * 304 * @param key 305 * 键 不能为null 306 * @param item 307 * 项 可以使多个 不能为null 308 */ 309 public void hdel(String key, Object... item) { 310 redisTemplate.opsForHash().delete(key, item); 311 } 312 313 /** 314 * 判断hash表中是否有该项的值 315 * 316 * @param key 317 * 键 不能为null 318 * @param item 319 * 项 不能为null 320 * @return true 存在 false不存在 321 */ 322 public boolean hHasKey(String key, String item) { 323 return redisTemplate.opsForHash().hasKey(key, item); 324 } 325 326 /** 327 * hash递增 如果不存在,就会创建一个 并把新增后的值返回 328 * 329 * @param key 330 * 键 331 * @param item 332 * 项 333 * @param by 334 * 要增加几(大于0) 335 * @return 336 */ 337 public double hincr(String key, String item, double by) { 338 return redisTemplate.opsForHash().increment(key, item, by); 339 } 340 341 /** 342 * hash递减 343 * 344 * @param key 345 * 键 346 * @param item 347 * 项 348 * @param by 349 * 要减少记(小于0) 350 * @return 351 */ 352 public double hdecr(String key, String item, double by) { 353 return redisTemplate.opsForHash().increment(key, item, -by); 354 } 355 356 // ============================set============================= 357 /** 358 * 根据key获取Set中的所有值 359 * 360 * @param key 361 * 键 362 * @return 363 */ 364 public Set<Object> sGet(String key) { 365 try { 366 return redisTemplate.opsForSet().members(key); 367 } catch (Exception e) { 368 e.printStackTrace(); 369 return null; 370 } 371 } 372 373 /** 374 * 根据value从一个set中查询,是否存在 375 * 376 * @param key 377 * 键 378 * @param value 379 * 值 380 * @return true 存在 false不存在 381 */ 382 public boolean sHasKey(String key, Object value) { 383 try { 384 return redisTemplate.opsForSet().isMember(key, value); 385 } catch (Exception e) { 386 e.printStackTrace(); 387 return false; 388 } 389 } 390 391 /** 392 * 将数据放入set缓存 393 * 394 * @param key 395 * 键 396 * @param values 397 * 值 可以是多个 398 * @return 成功个数 399 */ 400 public long sSet(String key, Object... values) { 401 try { 402 return redisTemplate.opsForSet().add(key, values); 403 } catch (Exception e) { 404 e.printStackTrace(); 405 return 0; 406 } 407 } 408 409 /** 410 * 将set数据放入缓存 411 * 412 * @param key 413 * 键 414 * @param time 415 * 时间 416 * @param values 417 * 值 可以是多个 418 * @return 成功个数 419 */ 420 public long sSetAndTime(String key, long time,TimeUnit timeUnit, Object... values) { 421 try { 422 Long count = redisTemplate.opsForSet().add(key, values); 423 if (time > 0) 424 expire(key, time,timeUnit); 425 return count; 426 } catch (Exception e) { 427 e.printStackTrace(); 428 return 0; 429 } 430 } 431 432 /** 433 * 获取set缓存的长度 434 * 435 * @param key 436 * 键 437 * @return 438 */ 439 public long sGetSetSize(String key) { 440 try { 441 return redisTemplate.opsForSet().size(key); 442 } catch (Exception e) { 443 e.printStackTrace(); 444 return 0; 445 } 446 } 447 448 /** 449 * 移除值为value的 450 * 451 * @param key 452 * 键 453 * @param values 454 * 值 可以是多个 455 * @return 移除的个数 456 */ 457 public long setRemove(String key, Object... values) { 458 try { 459 Long count = redisTemplate.opsForSet().remove(key, values); 460 return count; 461 } catch (Exception e) { 462 e.printStackTrace(); 463 return 0; 464 } 465 } 466 // ===============================list================================= 467 468 /** 469 * 获取list缓存的内容 470 * 471 * @param key 472 * 键 473 * @param start 474 * 开始 475 * @param end 476 * 结束 0 到 -1代表所有值 477 * @return 478 */ 479 public List<Object> lGet(String key, long start, long end) { 480 try { 481 return redisTemplate.opsForList().range(key, start, end); 482 } catch (Exception e) { 483 e.printStackTrace(); 484 return null; 485 } 486 } 487 488 /** 489 * 获取list缓存的长度 490 * 491 * @param key 492 * 键 493 * @return 494 */ 495 public long lGetListSize(String key) { 496 try { 497 return redisTemplate.opsForList().size(key); 498 } catch (Exception e) { 499 e.printStackTrace(); 500 return 0; 501 } 502 } 503 504 /** 505 * 通过索引 获取list中的值 506 * 507 * @param key 508 * 键 509 * @param index 510 * 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 511 * @return 512 */ 513 public Object lGetIndex(String key, long index) { 514 try { 515 return redisTemplate.opsForList().index(key, index); 516 } catch (Exception e) { 517 e.printStackTrace(); 518 return null; 519 } 520 } 521 522 /** 523 * 将list放入缓存 524 * 525 * @param key 526 * 键 527 * @param value 528 * 值 529 * @param time 530 * 时间 531 * @return 532 */ 533 public boolean lSet(String key, Object value) { 534 try { 535 redisTemplate.opsForList().rightPush(key, value); 536 return true; 537 } catch (Exception e) { 538 e.printStackTrace(); 539 return false; 540 } 541 } 542 543 /** 544 * 将list放入缓存 545 * 546 * @param key 547 * 键 548 * @param value 549 * 值 550 * @param time 551 * 时间 552 * @return 553 */ 554 public boolean lSet(String key, Object value, long time,TimeUnit timeUnit) { 555 try { 556 redisTemplate.opsForList().rightPush(key, value); 557 if (time > 0) 558 expire(key, time,timeUnit); 559 return true; 560 } catch (Exception e) { 561 e.printStackTrace(); 562 return false; 563 } 564 } 565 566 /** 567 * 将list放入缓存 568 * 569 * @param key 570 * 键 571 * @param value 572 * 值 573 * @param time 574 * 时间 575 * @return 576 */ 577 public boolean lSet(String key, List<Object> value) { 578 try { 579 redisTemplate.opsForList().rightPushAll(key, value); 580 return true; 581 } catch (Exception e) { 582 e.printStackTrace(); 583 return false; 584 } 585 } 586 587 /** 588 * 将list放入缓存 589 * 590 * @param key 591 * 键 592 * @param value 593 * 值 594 * @param time 595 * 时间 596 * @return 597 */ 598 public boolean lSet(String key, List<Object> value, long time,TimeUnit timeUnit) { 599 try { 600 redisTemplate.opsForList().rightPushAll(key, value); 601 if (time > 0) 602 expire(key, time,timeUnit); 603 return true; 604 } catch (Exception e) { 605 e.printStackTrace(); 606 return false; 607 } 608 } 609 610 /** 611 * 根据索引修改list中的某条数据 612 * 613 * @param key 614 * 键 615 * @param index 616 * 索引 617 * @param value 618 * 值 619 * @return 620 */ 621 public boolean lUpdateIndex(String key, long index, Object value) { 622 try { 623 redisTemplate.opsForList().set(key, index, value); 624 return true; 625 } catch (Exception e) { 626 e.printStackTrace(); 627 return false; 628 } 629 } 630 631 /** 632 * 移除N个值为value 633 * 634 * @param key 635 * 键 636 * @param count 637 * 移除多少个 638 * @param value 639 * 值 640 * @return 移除的个数 641 */ 642 public long lRemove(String key, long count, Object value) { 643 try { 644 Long remove = redisTemplate.opsForList().remove(key, count, value); 645 return remove; 646 } catch (Exception e) { 647 e.printStackTrace(); 648 return 0; 649 } 650 } 651 652 653 }
然后在需要用到redis的地方就直接调用我们的工具类中的方法就好了。