springboot集合redis之操作BitMap.1.模拟签到,2.模拟活动参加人数

1.模拟签到

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;

@SpringBootTest
class BitMapDemoTest {

  @Resource(name = "redisTemplate")
  private ValueOperations valueOperations;
  @Autowired
  private StringRedisTemplate stringRedisTemplate;

    private final  String key = "student#01";

    /**
     * BitMap模拟签到
     */
    @Test
    void test() {
        //签到,获取这个学生 整年的签到次数
        valueOperations.setBit(key,2,true);
        valueOperations.setBit(key,120,true);
        //接口, lamda表达式
        RedisCallback<Long> redisCallback=connection -> {
            return connection.bitCount(key.getBytes());
        };
        Long count=stringRedisTemplate.execute(redisCallback);
        //整年的签到次数
        System.out.println("签到次数:"+count);
    }
}

2.模拟活动参加人数

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
 /**
     * BitMap模拟活动参加人数
     */
@SpringBootTest
class BitMapDemoTest2 {

    @Resource(name = "redisTemplate")
    private ValueOperations valueOperations;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //参加三天活动
    private  final  Integer user1 = 1;
    private final  Integer user2  = 2;
    private final Integer user3 = 3;

    private  final String  key20220601 ="20220601";
    private  final String  key20220602 ="20220602";
    private  final String  key20220603 ="20220603";

    private final String  destKey = "20220601#20220602#20220603";


    @Test
    void test() {
        //第一天来了两个人 user1 user2
        valueOperations.setBit(key20220601,user1, true);
        valueOperations.setBit(key20220601,user2,true);

        //第二天 user2
        valueOperations.setBit(key20220602,user2,true);

        //第三天
        valueOperations.setBit(key20220603,user2,true);
        valueOperations.setBit(key20220603,user3,true);


        //老板 :  1: 一直参加活动的人数量
        RedisCallback<Long> redisCallback = connection -> {
            return  connection.bitOp(RedisStringCommands.BitOperation.AND,destKey.getBytes(),key20220601.getBytes(),key20220602.getBytes(),key20220603.getBytes());
        };
        Long value = stringRedisTemplate.execute(redisCallback);

        RedisCallback<Long> redisCallback2 = connection -> {

            return  connection.bitCount(destKey.getBytes());
        };
        Long value2 = stringRedisTemplate.execute(redisCallback2);
        System.out.println("一直参加活动的人数量");
        System.out.println(value2);

        //老板: 2: 参与我们3天活动的一共有多少人?

        RedisCallback<Long> redisCallback3= connection -> {
            return  connection.bitOp(RedisStringCommands.BitOperation.OR,destKey.getBytes(),key20220601.getBytes(),key20220602.getBytes(),key20220603.getBytes());
        };
        Long value3 = stringRedisTemplate.execute(redisCallback3);

        RedisCallback<Long> redisCallback4 = connection -> {

            return  connection.bitCount(destKey.getBytes());
        };
        Long value4 = stringRedisTemplate.execute(redisCallback4);
        System.out.println("参与我们3天活动的一共有多少人?");
        System.out.println(value4);
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

great-sun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值