[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常用配置参数

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

🎉String(字符串)

@SpringBootTest
class AppTests_String {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private final String key = "A";
    @Test
    public void test1() {
        //设KEY为A,value为100,30秒过期
        stringRedisTemplate.opsForValue().set(key, "100", 30, TimeUnit.SECONDS);
        //获取A的值
        String value = stringRedisTemplate.opsForValue().get(key);
        System.out.println(value);
        //拼接
        stringRedisTemplate.opsForValue().set("B","aaa");
        stringRedisTemplate.opsForValue().append("B", "--bbb");
        System.out.println(stringRedisTemplate.opsForValue().get("B"));
        //获取后删除
        String b = stringRedisTemplate.opsForValue().getAndDelete("B");
        System.out.println(b);
        //加指定值,默认1
        Long c = stringRedisTemplate.opsForValue().increment("C");
        System.out.println(c);
        Long c1 = stringRedisTemplate.opsForValue().increment("C", 9);
        System.out.println(c1);
        //setnx锁
        Boolean d1 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
        System.out.println(d1);
        Boolean d2 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
        System.out.println(d2);
        //设置声明周期
        String d3 = stringRedisTemplate.opsForValue().getAndExpire("D", 60, TimeUnit.SECONDS);
        System.out.println(d1);
        //批量设置
        Map<String,String> map = new HashMap<>();
        map.put("F1","1");
        map.put("F2","2");
        map.put("F3","3");
        stringRedisTemplate.opsForValue().multiSet(map);
        //批量获取
        List<String> keys = new ArrayList<>();
        keys.add("F1");
        keys.add("F2");
        keys.add("F3");
        List<String> values = stringRedisTemplate.opsForValue().multiGet(keys);
        System.out.println(values);
    }
}

🎉List(列表)

@SpringBootTest
class AppTests_List {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static final String name = "wangwu";

    @Test
    void test1() {
        //左推
        stringRedisTemplate.opsForList().leftPush(name, "aaa");
        //批量左推
        Long aLong = stringRedisTemplate.opsForList().leftPushAll(name, "aaa", "bbb", "ccc");
        System.out.println(aLong);

        //左弹
        String v = stringRedisTemplate.opsForList().leftPop(name);
        System.out.println(v);
        //右弹,限时30秒后结束监听
        String v1 = stringRedisTemplate.opsForList().rightPop(name, 30, TimeUnit.MINUTES);
        System.out.println("--------------v1------------");
        System.out.println(v1);
        
        //一直监听,有值就弹出
        //while (true) {
        //    String v2 = stringRedisTemplate.opsForList().rightPop(name, 10, TimeUnit.SECONDS);
        //    if (v2 != null) {
        //        System.out.println("------------获取到数据------------");
        //        System.out.println(v2);
        //    } else {
        //        System.out.println("-----------没有拉取到新数据,继续监听----------");
        //    }
        //}
        
        //长度
        Long size = stringRedisTemplate.opsForList().size(name);
        System.out.println(size);
    }
}

🎉Set(集合)

@SpringBootTest
class AppTests_Set {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String name1 = "zhangsan";
    private static final String name2 = "lisi";

    @Test
    void test1() {
        //添加
        stringRedisTemplate.opsForSet().add(name1, "张三", "李四", "王五");
        stringRedisTemplate.opsForSet().add(name2, "张三丰", "李四", "王五");
        //交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(name1, name2);
        //stringRedisTemplate.opsForSet().intersectAndStore()
        System.out.println(intersect);
        //并集
        Set<String> union = stringRedisTemplate.opsForSet().union(name1, name2);
        //stringRedisTemplate.opsForSet().unionAndStore()
        System.out.println(union);
        //差集
        Set<String> difference = stringRedisTemplate.opsForSet().difference(name1, name2);
        //stringRedisTemplate.opsForSet().differenceAndStore()
        System.out.println(difference);
        //是否存在
        Boolean a = stringRedisTemplate.opsForSet().isMember(name1, "张三");
        Boolean b = stringRedisTemplate.opsForSet().isMember(name1, "张三丰");
        System.out.println(a);
        System.out.println(b);


    }
}

🎉ZSet(有序集合)

@SpringBootTest
class AppTests_Zset {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String name1 = "zhangsan";

    @Test
    void test1() {
        stringRedisTemplate.opsForZSet().add(name1, "语文", 110);
        stringRedisTemplate.opsForZSet().add(name1, "数学", 130);
        stringRedisTemplate.opsForZSet().add(name1, "英语", 90);
        //数量
        Long size = stringRedisTemplate.opsForZSet().size(name1);
        System.out.println(size);
        //范围数量
        Long count = stringRedisTemplate.opsForZSet().count(name1, 100, 150);
        System.out.println(count);
        //弹出最值
        //ZSetOperations.TypedTuple<String> max = stringRedisTemplate.opsForZSet().popMax(name1);
        //ZSetOperations.TypedTuple<String> min = stringRedisTemplate.opsForZSet().popMin(name1);
        //System.out.println(max+"---"+min);

        //范围内的值,根据分数升序
        Set<String> strings = stringRedisTemplate.opsForZSet().rangeByScore(name1, 0, 150);
        System.out.println(strings);
        //排名
        Long rank = stringRedisTemplate.opsForZSet().rank(name1, "数学");
        System.out.println(rank);
        //倒叙
        Set<String> strings1 = stringRedisTemplate.opsForZSet().reverseRangeByScore(name1, 0, 150);
        System.out.println(strings1);

    }
}

🎉Hash(字典)

@SpringBootTest
class AppTests_Hash {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String name = "zhangsan";
    private static final String name2 = "lisi";

    @Test
    void test1() {
        //放
        stringRedisTemplate.opsForHash().put(name, "20221107", "洛阳");
        stringRedisTemplate.opsForHash().put(name, "20221108", "新乡");
        stringRedisTemplate.opsForHash().put(name, "20221109", "郑州");
        //取
        Object o = stringRedisTemplate.opsForHash().get(name, "20221108");
        System.out.println(o);

        //所有value
        List<Object> values = stringRedisTemplate.opsForHash().values(name);
        System.out.println(values);
        //所有hashKey
        Set<Object> keys = stringRedisTemplate.opsForHash().keys(name);
        System.out.println(keys);
        //所有hashKey和value
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(name);
        System.out.println(entries);
        //是否存在指定hashKey
        Boolean b = stringRedisTemplate.opsForHash().hasKey(name, "20221108-");
        System.out.println(b);
        // stringRedisTemplate.opsForHash().put(name,"20221109","开封");
        //如果没有.则放
        Boolean b1 = stringRedisTemplate.opsForHash().putIfAbsent(name, "20221110", "南阳");
        System.out.println(b1);
        //删除
        // stringRedisTemplate.opsForHash().delete(name,"20221110");
        //批量获取
        List<Object> objects = stringRedisTemplate.opsForHash().multiGet(name, CollUtil.newArrayList("20221107", "20221109"));
        System.out.println(objects);
        //批量放
        Map<String, String> map = new HashMap<>();
        map.put("20221107", "驻马店");
        map.put("20221108", "信阳");
        map.put("20221109", "平顶山");
        stringRedisTemplate.opsForHash().putAll(name2, map);
        //加减
        Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 5);
        //Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 100);
        //Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", -8);
        System.out.println(qty);
        //随机获取
        List<Object> keys = stringRedisTemplate.opsForHash().randomKeys(name2, 2);
        System.out.println(keys);
    }
}

🎉BitMap(String)

@SpringBootTest
class AppTests_BitMap {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private final String name1 = "张三";

    @Test
    public void test1() {
        stringRedisTemplate.opsForValue().setBit(name1, 1, true);
        stringRedisTemplate.opsForValue().setBit(name1, 100, true);
        stringRedisTemplate.opsForValue().setBit(name1, 365, true);

        Boolean bit = stringRedisTemplate.opsForValue().getBit(name1, 1);
        Boolean bit1 = stringRedisTemplate.opsForValue().getBit(name1, 2);
        System.out.println(bit + "---" + bit1);
        //原生统计范围内1/true的个数
        RedisCallback<Long> callback = (connection) ->
                connection.bitCount(name1.getBytes(StandardCharsets.UTF_8), 0, 366);
        Long aLong = stringRedisTemplate.execute(callback);
        System.out.println(aLong);

        //stringRedisTemplate.opsForValue().getOperations().execute(callback);
    }
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
shiro-redis-spring-boot-starter是一个用于集成Apache Shiro和RedisSpring Boot Starter项目。Apache Shiro是一个强大而灵活的Java安全框架,用于身份验证、授权和会话管理等安全功能。而Redis是一个高性能的内存数据库,其具有快速的数据存取能力和持久化支持。 shiro-redis-spring-boot-starter提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易。通过使用该Starter,我们可以方便地将Shiro的会话管理功能存储到Redis中,从而支持分布式环境下的会话共享和管理。 使用shiro-redis-spring-boot-starter可以带来以下好处: 1. 分布式环境的会话共享:通过将Shiro的会话数据存储到Redis中,不同的应用节点可以共享同一个会话,从而实现分布式环境下的会话管理和跨节点的身份验证和授权。 2. 高可用性和性能:Redis作为一个高性能的内存数据库,具有出色的数据读写能力和持久化支持,可以提供可靠的会话存储和高性能的数据访问能力。 3. 简化配置和集成:shiro-redis-spring-boot-starter提供了封装好的配置和集成方式,减少了我们自己实现集成的复杂性和工作量。 总结来说,shiro-redis-spring-boot-starter为我们提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易和高效。通过它,我们可以实现分布式环境下的会话共享和管理,提供高可用性和性能的数据存取能力,同时简化了配置和集成的复杂性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沫洺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值