栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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";

    
    @Test
    void test() {
        //签到,获取这个学生 整年的签到次数
        valueOperations.setBit(key,2,true);
        valueOperations.setBit(key,120,true);
        //接口, lamda表达式
        RedisCallback 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;
 
@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 redisCallback = connection -> {
            return  connection.bitOp(RedisStringCommands.BitOperation.AND,destKey.getBytes(),key20220601.getBytes(),key20220602.getBytes(),key20220603.getBytes());
        };
        Long value = stringRedisTemplate.execute(redisCallback);

        RedisCallback redisCallback2 = connection -> {

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

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

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

        RedisCallback redisCallback4 = connection -> {

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

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号