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);
    }

}

你可能感兴趣的:(Redis,redis,spring,boot,java)