1、pom文件中添加redis引用
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-data-redis</artifactId> 8 </dependency>
2、配置application.properties
1 server.port=8080 2 3 spring.redis.host=127.0.0.1 4 spring.redis.port=6379 5 spring.redis.database=0 6 spring.redis.password= 7 spring.redis.timeout=6000 8 spring.redis.lettuce.pool.max-active=8 9 spring.redis.lettuce.pool.min-idle=0 10 spring.redis.lettuce.pool.max-idle=8 11 spring.redis.lettuce.pool.max-wait=-1
启动类
1 package com.atguigu.boot00testredis; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class TestRedisApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(TestRedisApplication.class, args); 11 } 12 13 }
3、写测试类直接调用Redis
1 package com.atguigu.boot00testredis; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.data.redis.core.StringRedisTemplate; 7 8 import java.util.concurrent.TimeUnit; 9 10 @SpringBootTest 11 class Boot00TestRedisApplicationTests { 12 13 @Autowired 14 private StringRedisTemplate stringRedisTemplate; 15 16 @Test 17 void contextLoads() { 18 System.out.println("redis"); 19 stringRedisTemplate.opsForValue().set("c","ccc",60, TimeUnit.SECONDS); 20 System.out.println(stringRedisTemplate.opsForValue().get("c")); 21 } 22 23 }
运行结果:
4、添加redis工具类
1 package com.atguigu.boot00testredis.utils; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.data.redis.core.RedisTemplate; 5 import org.springframework.stereotype.Component; 6 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 import java.util.concurrent.TimeUnit; 11 12 @Component 13 public class RedisUtil { 14 15 @Autowired 16 private RedisTemplate redisTemplate; 17 /** 18 * 给一个指定的 key 值附加过期时间 19 * 20 * @param key 21 * @param time 22 * @return 23 */ 24 public boolean expire(String key, long time) { 25 return redisTemplate.expire(key, time, TimeUnit.SECONDS); 26 } 27 /** 28 * 根据key 获取过期时间 29 * 30 * @param key 31 * @return 32 */ 33 public long getTime(String key) { 34 return redisTemplate.getExpire(key, TimeUnit.SECONDS); 35 } 36 /** 37 * 根据key 获取过期时间 38 * 39 * @param key 40 * @return 41 */ 42 public boolean hasKey(String key) { 43 return redisTemplate.hasKey(key); 44 } 45 /** 46 * 移除指定key 的过期时间 47 * 48 * @param key 49 * @return 50 */ 51 public boolean persist(String key) { 52 return redisTemplate.boundValueOps(key).persist(); 53 } 54 55 //- - - - - - - - - - - - - - - - - - - - - String类型 - - - - - - - - - - - - - - - - - - - - 56 57 /** 58 * 根据key获取值 59 * 60 * @param key 键 61 * @return 值 62 */ 63 public Object get(String key) { 64 return key == null ? null : redisTemplate.opsForValue().get(key); 65 } 66 67 /** 68 * 将值放入缓存 69 * 70 * @param key 键 71 * @param value 值 72 * @return true成功 false 失败 73 */ 74 public void set(String key, String value) { 75 redisTemplate.opsForValue().set(key, value); 76 } 77 78 /** 79 * 将值放入缓存并设置时间 80 * 81 * @param key 键 82 * @param value 值 83 * @param time 时间(秒) -1为无期限 84 * @return true成功 false 失败 85 */ 86 public void set(String key, String value, long time) { 87 if (time > 0) { 88 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); 89 } else { 90 redisTemplate.opsForValue().set(key, value); 91 } 92 } 93 94 /** 95 * 批量添加 key (重复的键会覆盖) 96 * 97 * @param keyAndValue 98 */ 99 public void batchSet(Map<String, String> keyAndValue) { 100 redisTemplate.opsForValue().multiSet(keyAndValue); 101 } 102 103 /** 104 * 批量添加 key-value 只有在键不存在时,才添加 105 * map 中只要有一个key存在,则全部不添加 106 * 107 * @param keyAndValue 108 */ 109 public void batchSetIfAbsent(Map<String, String> keyAndValue) { 110 redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue); 111 } 112 113 /** 114 * 对一个 key-value 的值进行加减操作, 115 * 如果该 key 不存在 将创建一个key 并赋值该 number 116 * 如果 key 存在,但 value 不是长整型 ,将报错 117 * 118 * @param key 119 * @param number 120 */ 121 public Long increment(String key, long number) { 122 return redisTemplate.opsForValue().increment(key, number); 123 } 124 125 /** 126 * 对一个 key-value 的值进行加减操作, 127 * 如果该 key 不存在 将创建一个key 并赋值该 number 128 * 如果 key 存在,但 value 不是 纯数字 ,将报错 129 * 130 * @param key 131 * @param number 132 */ 133 public Double increment(String key, double number) { 134 return redisTemplate.opsForValue().increment(key, number); 135 } 136 137 //- - - - - - - - - - - - - - - - - - - - - set类型 - - - - - - - - - - - - - - - - - - - - 138 139 /** 140 * 将数据放入set缓存 141 * 142 * @param key 键 143 * @return 144 */ 145 public void sSet(String key, String value) { 146 redisTemplate.opsForSet().add(key, value); 147 } 148 149 /** 150 * 获取变量中的值 151 * 152 * @param key 键 153 * @return 154 */ 155 public Set<Object> members(String key) { 156 return redisTemplate.opsForSet().members(key); 157 } 158 159 /** 160 * 随机获取变量中指定个数的元素 161 * 162 * @param key 键 163 * @param count 值 164 * @return 165 */ 166 public void randomMembers(String key, long count) { 167 redisTemplate.opsForSet().randomMembers(key, count); 168 } 169 170 /** 171 * 随机获取变量中的元素 172 * 173 * @param key 键 174 * @return 175 */ 176 public Object randomMember(String key) { 177 return redisTemplate.opsForSet().randomMember(key); 178 } 179 180 /** 181 * 弹出变量中的元素 182 * 183 * @param key 键 184 * @return 185 */ 186 public Object pop(String key) { 187 return redisTemplate.opsForSet().pop("setValue"); 188 } 189 190 /** 191 * 获取变量中值的长度 192 * 193 * @param key 键 194 * @return 195 */ 196 public long size(String key) { 197 return redisTemplate.opsForSet().size(key); 198 } 199 200 /** 201 * 根据value从一个set中查询,是否存在 202 * 203 * @param key 键 204 * @param value 值 205 * @return true 存在 false不存在 206 */ 207 public boolean sHasKey(String key, Object value) { 208 return redisTemplate.opsForSet().isMember(key, value); 209 } 210 211 /** 212 * 检查给定的元素是否在变量中。 213 * 214 * @param key 键 215 * @param obj 元素对象 216 * @return 217 */ 218 public boolean isMember(String key, Object obj) { 219 return redisTemplate.opsForSet().isMember(key, obj); 220 } 221 222 /** 223 * 转移变量的元素值到目的变量。 224 * 225 * @param key 键 226 * @param value 元素对象 227 * @param destKey 元素对象 228 * @return 229 */ 230 public boolean move(String key, String value, String destKey) { 231 return redisTemplate.opsForSet().move(key, value, destKey); 232 } 233 234 /** 235 * 批量移除set缓存中元素 236 * 237 * @param key 键 238 * @param values 值 239 * @return 240 */ 241 public void remove(String key, Object... values) { 242 redisTemplate.opsForSet().remove(key, values); 243 } 244 245 /** 246 * 通过给定的key求2个set变量的差值 247 * 248 * @param key 键 249 * @param destKey 键 250 * @return 251 */ 252 public Set<Set> difference(String key, String destKey) { 253 return redisTemplate.opsForSet().difference(key, destKey); 254 } 255 256 257 //- - - - - - - - - - - - - - - - - - - - - hash类型 - - - - - - - - - - - - - - - - - - - - 258 259 /** 260 * 加入缓存 261 * 262 * @param key 键 263 * @param map 键 264 * @return 265 */ 266 public void add(String key, Map<String, String> map) { 267 redisTemplate.opsForHash().putAll(key, map); 268 } 269 270 /** 271 * 获取 key 下的 所有 hashkey 和 value 272 * 273 * @param key 键 274 * @return 275 */ 276 public Map<Object, Object> getHashEntries(String key) { 277 return redisTemplate.opsForHash().entries(key); 278 } 279 280 /** 281 * 验证指定 key 下 有没有指定的 hashkey 282 * 283 * @param key 284 * @param hashKey 285 * @return 286 */ 287 public boolean hashKey(String key, String hashKey) { 288 return redisTemplate.opsForHash().hasKey(key, hashKey); 289 } 290 291 /** 292 * 获取指定key的值string 293 * 294 * @param key 键 295 * @param key2 键 296 * @return 297 */ 298 public String getMapString(String key, String key2) { 299 return redisTemplate.opsForHash().get("map1", "key1").toString(); 300 } 301 302 /** 303 * 获取指定的值Int 304 * 305 * @param key 键 306 * @param key2 键 307 * @return 308 */ 309 public Integer getMapInt(String key, String key2) { 310 return (Integer) redisTemplate.opsForHash().get("map1", "key1"); 311 } 312 313 /** 314 * 弹出元素并删除 315 * 316 * @param key 键 317 * @return 318 */ 319 public String popValue(String key) { 320 return redisTemplate.opsForSet().pop(key).toString(); 321 } 322 323 /** 324 * 删除指定 hash 的 HashKey 325 * 326 * @param key 327 * @param hashKeys 328 * @return 删除成功的 数量 329 */ 330 public Long delete(String key, String... hashKeys) { 331 return redisTemplate.opsForHash().delete(key, hashKeys); 332 } 333 334 /** 335 * 给指定 hash 的 hashkey 做增减操作 336 * 337 * @param key 338 * @param hashKey 339 * @param number 340 * @return 341 */ 342 public Long increment(String key, String hashKey, long number) { 343 return redisTemplate.opsForHash().increment(key, hashKey, number); 344 } 345 346 /** 347 * 给指定 hash 的 hashkey 做增减操作 348 * 349 * @param key 350 * @param hashKey 351 * @param number 352 * @return 353 */ 354 public Double increment(String key, String hashKey, Double number) { 355 return redisTemplate.opsForHash().increment(key, hashKey, number); 356 } 357 358 /** 359 * 获取 key 下的 所有 hashkey 字段 360 * 361 * @param key 362 * @return 363 */ 364 public Set<Object> hashKeys(String key) { 365 return redisTemplate.opsForHash().keys(key); 366 } 367 368 /** 369 * 获取指定 hash 下面的 键值对 数量 370 * 371 * @param key 372 * @return 373 */ 374 public Long hashSize(String key) { 375 return redisTemplate.opsForHash().size(key); 376 } 377 378 //- - - - - - - - - - - - - - - - - - - - - list类型 - - - - - - - - - - - - - - - - - - - - 379 380 /** 381 * 在变量左边添加元素值 382 * 383 * @param key 384 * @param value 385 * @return 386 */ 387 public void leftPush(String key, Object value) { 388 redisTemplate.opsForList().leftPush(key, value); 389 } 390 391 /** 392 * 获取集合指定位置的值。 393 * 394 * @param key 395 * @param index 396 * @return 397 */ 398 public Object index(String key, long index) { 399 return redisTemplate.opsForList().index("list", 1); 400 } 401 402 /** 403 * 获取指定区间的值。 404 * 405 * @param key 406 * @param start 407 * @param end 408 * @return 409 */ 410 public List<Object> range(String key, long start, long end) { 411 return redisTemplate.opsForList().range(key, start, end); 412 } 413 414 /** 415 * 把最后一个参数值放到指定集合的第一个出现中间参数的前面, 416 * 如果中间参数值存在的话。 417 * 418 * @param key 419 * @param pivot 420 * @param value 421 * @return 422 */ 423 public void leftPush(String key, String pivot, String value) { 424 redisTemplate.opsForList().leftPush(key, pivot, value); 425 } 426 427 /** 428 * 向左边批量添加参数元素。 429 * 430 * @param key 431 * @param values 432 * @return 433 */ 434 public void leftPushAll(String key, String... values) { 435 // redisTemplate.opsForList().leftPushAll(key,"w","x","y"); 436 redisTemplate.opsForList().leftPushAll(key, values); 437 } 438 439 /** 440 * 向集合最右边添加元素。 441 * 442 * @param key 443 * @param value 444 * @return 445 */ 446 public void leftPushAll(String key, String value) { 447 redisTemplate.opsForList().rightPush(key, value); 448 } 449 450 /** 451 * 向左边批量添加参数元素。 452 * 453 * @param key 454 * @param values 455 * @return 456 */ 457 public void rightPushAll(String key, String... values) { 458 //redisTemplate.opsForList().leftPushAll(key,"w","x","y"); 459 redisTemplate.opsForList().rightPushAll(key, values); 460 } 461 462 /** 463 * 向已存在的集合中添加元素。 464 * 465 * @param key 466 * @param value 467 * @return 468 */ 469 public void rightPushIfPresent(String key, Object value) { 470 redisTemplate.opsForList().rightPushIfPresent(key, value); 471 } 472 473 /** 474 * 向已存在的集合中添加元素。 475 * 476 * @param key 477 * @return 478 */ 479 public long listLength(String key) { 480 return redisTemplate.opsForList().size(key); 481 } 482 483 /** 484 * 移除集合中的左边第一个元素。 485 * 486 * @param key 487 * @return 488 */ 489 public void leftPop(String key) { 490 redisTemplate.opsForList().leftPop(key); 491 } 492 493 /** 494 * 移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。 495 * 496 * @param key 497 * @return 498 */ 499 public void leftPop(String key, long timeout, TimeUnit unit) { 500 redisTemplate.opsForList().leftPop(key, timeout, unit); 501 } 502 503 /** 504 * 移除集合中右边的元素。 505 * 506 * @param key 507 * @return 508 */ 509 public void rightPop(String key) { 510 redisTemplate.opsForList().rightPop(key); 511 } 512 513 /** 514 * 移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。 515 * 516 * @param key 517 * @return 518 */ 519 public void rightPop(String key, long timeout, TimeUnit unit) { 520 redisTemplate.opsForList().rightPop(key, timeout, unit); 521 } 522 }
5、写Controller方法
1 package com.atguigu.boot00testredis.controller; 2 3 import com.atguigu.boot00testredis.utils.RedisUtil; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 @RestController 11 @RequestMapping("/hello") 12 public class HelloController 13 { 14 /** 15 * 注入redis工具类 16 */ 17 @Autowired 18 private RedisUtil redisUtil; 19 20 @GetMapping("/hi/{name}") 21 public String hi(@PathVariable("name") String name) 22 { 23 System.out.println("name="+name); 24 25 if(redisUtil.get(name)!=null) 26 { 27 return redisUtil.get(name).toString(); 28 } 29 else { 30 redisUtil.set(name,"My name is "+name+"!",60); 31 32 return redisUtil.get(name).toString(); 33 } 34 } 35 }
调用结果:
- SpringBoot2 SpringBoot 步骤 Redisspringboot2 springboot步骤redis springboot2 springboot lettuce redis springboot2 springboot redis 集群springboot2 springboot redis springboot2 springboot2 springboot springboot2 springboot swagger3 swagger springboot2 springboot mybatis springboot2 springboot spring3 spring springboot2 springboot后台 管理系统