• [Redis] Spring Boot 使用Redis---StringRedisTemplate


    ✨✨个人主页:沫洺的主页

    📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏 

                               📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏

                               📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏     

    💖💖如果文章对你有所帮助请留下三连✨✨

    ✨Redis结构体

    Redis 有 5 种基础数据结构: 

    string (字符串) 、 list (列表) 、 hash (字典) 、 set (集合) 和 zset (有序集合) 

    🎊Spring Boot 使用Redis

    概念补充:

    Redis缓存穿透,缓存击穿,缓存雪崩

    • 穿透: 不存在
    • 击穿:一个热点的key失效了,这时大量的并发请求直接到达数据库.
    • 雪崩:大量key同时失效

     Redis常用配置参数

    1. # Redis服务器地址
    2. spring.redis.host=192.168.0.104
    3. # Redis数据库索引(默认为0
    4. spring.redis.database=0
    5. # Redis服务器连接端口
    6. spring.redis.port=6380
    7. # Redis服务器连接密码(默认为空)
    8. #spring.redis.password=
    9. # 连接池最大连接数(使用负值表示没有限制) 默认 8
    10. spring.redis.lettuce.pool.max-active=8
    11. # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
    12. spring.redis.lettuce.pool.max-wait=-1
    13. # 连接池中的最大空闲连接 默认 8
    14. spring.redis.lettuce.pool.max-idle=5
    15. # 连接池中的最小空闲连接 默认 0
    16. spring.redis.lettuce.pool.min-idle=5
    17. ## 连接超时时间(毫秒)
    18. spring.redis.timeout=30000

    🎉String(字符串)

    1. @SpringBootTest
    2. class AppTests_String {
    3. @Autowired
    4. private StringRedisTemplate stringRedisTemplate;
    5. private final String key = "A";
    6. @Test
    7. public void test1() {
    8. //设KEY为A,value为100,30秒过期
    9. stringRedisTemplate.opsForValue().set(key, "100", 30, TimeUnit.SECONDS);
    10. //获取A的值
    11. String value = stringRedisTemplate.opsForValue().get(key);
    12. System.out.println(value);
    13. //拼接
    14. stringRedisTemplate.opsForValue().set("B","aaa");
    15. stringRedisTemplate.opsForValue().append("B", "--bbb");
    16. System.out.println(stringRedisTemplate.opsForValue().get("B"));
    17. //获取后删除
    18. String b = stringRedisTemplate.opsForValue().getAndDelete("B");
    19. System.out.println(b);
    20. //加指定值,默认1
    21. Long c = stringRedisTemplate.opsForValue().increment("C");
    22. System.out.println(c);
    23. Long c1 = stringRedisTemplate.opsForValue().increment("C", 9);
    24. System.out.println(c1);
    25. //setnx锁
    26. Boolean d1 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
    27. System.out.println(d1);
    28. Boolean d2 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
    29. System.out.println(d2);
    30. //设置声明周期
    31. String d3 = stringRedisTemplate.opsForValue().getAndExpire("D", 60, TimeUnit.SECONDS);
    32. System.out.println(d1);
    33. //批量设置
    34. Map map = new HashMap<>();
    35. map.put("F1","1");
    36. map.put("F2","2");
    37. map.put("F3","3");
    38. stringRedisTemplate.opsForValue().multiSet(map);
    39. //批量获取
    40. List keys = new ArrayList<>();
    41. keys.add("F1");
    42. keys.add("F2");
    43. keys.add("F3");
    44. List values = stringRedisTemplate.opsForValue().multiGet(keys);
    45. System.out.println(values);
    46. }
    47. }

    🎉List(列表)

    1. @SpringBootTest
    2. class AppTests_List {
    3. @Autowired
    4. private StringRedisTemplate stringRedisTemplate;
    5. private static final String name = "wangwu";
    6. @Test
    7. void test1() {
    8. //左推
    9. stringRedisTemplate.opsForList().leftPush(name, "aaa");
    10. //批量左推
    11. Long aLong = stringRedisTemplate.opsForList().leftPushAll(name, "aaa", "bbb", "ccc");
    12. System.out.println(aLong);
    13. //左弹
    14. String v = stringRedisTemplate.opsForList().leftPop(name);
    15. System.out.println(v);
    16. //右弹,限时30秒后结束监听
    17. String v1 = stringRedisTemplate.opsForList().rightPop(name, 30, TimeUnit.MINUTES);
    18. System.out.println("--------------v1------------");
    19. System.out.println(v1);
    20. //一直监听,有值就弹出
    21. //while (true) {
    22. // String v2 = stringRedisTemplate.opsForList().rightPop(name, 10, TimeUnit.SECONDS);
    23. // if (v2 != null) {
    24. // System.out.println("------------获取到数据------------");
    25. // System.out.println(v2);
    26. // } else {
    27. // System.out.println("-----------没有拉取到新数据,继续监听----------");
    28. // }
    29. //}
    30. //长度
    31. Long size = stringRedisTemplate.opsForList().size(name);
    32. System.out.println(size);
    33. }
    34. }

    🎉Set(集合)

    1. @SpringBootTest
    2. class AppTests_Set {
    3. @Autowired
    4. private StringRedisTemplate stringRedisTemplate;
    5. private static final String name1 = "zhangsan";
    6. private static final String name2 = "lisi";
    7. @Test
    8. void test1() {
    9. //添加
    10. stringRedisTemplate.opsForSet().add(name1, "张三", "李四", "王五");
    11. stringRedisTemplate.opsForSet().add(name2, "张三丰", "李四", "王五");
    12. //交集
    13. Set intersect = stringRedisTemplate.opsForSet().intersect(name1, name2);
    14. //stringRedisTemplate.opsForSet().intersectAndStore()
    15. System.out.println(intersect);
    16. //并集
    17. Set union = stringRedisTemplate.opsForSet().union(name1, name2);
    18. //stringRedisTemplate.opsForSet().unionAndStore()
    19. System.out.println(union);
    20. //差集
    21. Set difference = stringRedisTemplate.opsForSet().difference(name1, name2);
    22. //stringRedisTemplate.opsForSet().differenceAndStore()
    23. System.out.println(difference);
    24. //是否存在
    25. Boolean a = stringRedisTemplate.opsForSet().isMember(name1, "张三");
    26. Boolean b = stringRedisTemplate.opsForSet().isMember(name1, "张三丰");
    27. System.out.println(a);
    28. System.out.println(b);
    29. }
    30. }

    🎉ZSet(有序集合)

    1. @SpringBootTest
    2. class AppTests_Zset {
    3. @Autowired
    4. private StringRedisTemplate stringRedisTemplate;
    5. private static final String name1 = "zhangsan";
    6. @Test
    7. void test1() {
    8. stringRedisTemplate.opsForZSet().add(name1, "语文", 110);
    9. stringRedisTemplate.opsForZSet().add(name1, "数学", 130);
    10. stringRedisTemplate.opsForZSet().add(name1, "英语", 90);
    11. //数量
    12. Long size = stringRedisTemplate.opsForZSet().size(name1);
    13. System.out.println(size);
    14. //范围数量
    15. Long count = stringRedisTemplate.opsForZSet().count(name1, 100, 150);
    16. System.out.println(count);
    17. //弹出最值
    18. //ZSetOperations.TypedTuple max = stringRedisTemplate.opsForZSet().popMax(name1);
    19. //ZSetOperations.TypedTuple min = stringRedisTemplate.opsForZSet().popMin(name1);
    20. //System.out.println(max+"---"+min);
    21. //范围内的值,根据分数升序
    22. Set strings = stringRedisTemplate.opsForZSet().rangeByScore(name1, 0, 150);
    23. System.out.println(strings);
    24. //排名
    25. Long rank = stringRedisTemplate.opsForZSet().rank(name1, "数学");
    26. System.out.println(rank);
    27. //倒叙
    28. Set strings1 = stringRedisTemplate.opsForZSet().reverseRangeByScore(name1, 0, 150);
    29. System.out.println(strings1);
    30. }
    31. }

    🎉Hash(字典)

    1. @SpringBootTest
    2. class AppTests_Hash {
    3. @Autowired
    4. private StringRedisTemplate stringRedisTemplate;
    5. private static final String name = "zhangsan";
    6. private static final String name2 = "lisi";
    7. @Test
    8. void test1() {
    9. //放
    10. stringRedisTemplate.opsForHash().put(name, "20221107", "洛阳");
    11. stringRedisTemplate.opsForHash().put(name, "20221108", "新乡");
    12. stringRedisTemplate.opsForHash().put(name, "20221109", "郑州");
    13. //取
    14. Object o = stringRedisTemplate.opsForHash().get(name, "20221108");
    15. System.out.println(o);
    16. //所有value
    17. List values = stringRedisTemplate.opsForHash().values(name);
    18. System.out.println(values);
    19. //所有hashKey
    20. Set keys = stringRedisTemplate.opsForHash().keys(name);
    21. System.out.println(keys);
    22. //所有hashKey和value
    23. Map entries = stringRedisTemplate.opsForHash().entries(name);
    24. System.out.println(entries);
    25. //是否存在指定hashKey
    26. Boolean b = stringRedisTemplate.opsForHash().hasKey(name, "20221108-");
    27. System.out.println(b);
    28. // stringRedisTemplate.opsForHash().put(name,"20221109","开封");
    29. //如果没有.则放
    30. Boolean b1 = stringRedisTemplate.opsForHash().putIfAbsent(name, "20221110", "南阳");
    31. System.out.println(b1);
    32. //删除
    33. // stringRedisTemplate.opsForHash().delete(name,"20221110");
    34. //批量获取
    35. List objects = stringRedisTemplate.opsForHash().multiGet(name, CollUtil.newArrayList("20221107", "20221109"));
    36. System.out.println(objects);
    37. //批量放
    38. Map map = new HashMap<>();
    39. map.put("20221107", "驻马店");
    40. map.put("20221108", "信阳");
    41. map.put("20221109", "平顶山");
    42. stringRedisTemplate.opsForHash().putAll(name2, map);
    43. //加减
    44. Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 5);
    45. //Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 100);
    46. //Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", -8);
    47. System.out.println(qty);
    48. //随机获取
    49. List keys = stringRedisTemplate.opsForHash().randomKeys(name2, 2);
    50. System.out.println(keys);
    51. }
    52. }
    53. 🎉BitMap(String)

      1. @SpringBootTest
      2. class AppTests_BitMap {
      3. @Autowired
      4. private StringRedisTemplate stringRedisTemplate;
      5. private final String name1 = "张三";
      6. @Test
      7. public void test1() {
      8. stringRedisTemplate.opsForValue().setBit(name1, 1, true);
      9. stringRedisTemplate.opsForValue().setBit(name1, 100, true);
      10. stringRedisTemplate.opsForValue().setBit(name1, 365, true);
      11. Boolean bit = stringRedisTemplate.opsForValue().getBit(name1, 1);
      12. Boolean bit1 = stringRedisTemplate.opsForValue().getBit(name1, 2);
      13. System.out.println(bit + "---" + bit1);
      14. //原生统计范围内1/true的个数
      15. RedisCallback callback = (connection) ->
      16. connection.bitCount(name1.getBytes(StandardCharsets.UTF_8), 0, 366);
      17. Long aLong = stringRedisTemplate.execute(callback);
      18. System.out.println(aLong);
      19. //stringRedisTemplate.opsForValue().getOperations().execute(callback);
      20. }
      21. }

    54. 相关阅读:
      python 断点续传下载
      java计算机毕业设计航帆学院网站MyBatis+系统+LW文档+源码+调试部署
      Flutter 开发者工具 Android Studio 开发Flutter应用
      功率放大器和电压放大器的区别是什么意思
      Spring Cloud【SkyWalking日志、SkyWalking告警 、Skywalking自定义告警规则】(十五)
      Windows(二):windows+nginx+openssl本地搭建nginx并配置ssl实现https访问
      java计算机毕业设计高校人事管理系统源码+数据库+系统+lw文档+部署
      【FastCAE源码阅读7】视图方向切换按钮实现原理
      外卖项目07---git
      C++ 性能优化指南 KurtGuntheroth 第6章 优化动态分配内存的变量 摘录
    55. 原文地址:https://blog.csdn.net/HeyVIrBbox/article/details/127788759