package com.ruoyi.cai.ws.cache;

import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.constant.RedisConstant;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * key ：sessionKey
 * value ： 当前用户信息
 * 一个call 有两个redis
 * <p>created on 2024/1/11 22:44</p>
 * @author duet
 */
@Component
public class FdCtxDataCache {
    @Autowired
    private RedissonClient redissonClient;

    public String getKey(String sessionKey){
        return String.format(RedisConstant.FDCTX_DATA,sessionKey);
    }

    public void save(FdCtxData fdCtxData) {
        RBucket<FdCtxData> bucket = redissonClient.getBucket(getKey(fdCtxData.getSessionKey()));
        bucket.set(fdCtxData,5,TimeUnit.DAYS);
    }

    public FdCtxData getByRoomId(String sessionKey){
        String key = getKey(sessionKey);
        RBucket<FdCtxData> bucket = redissonClient.getBucket(key);
        return bucket.get();
    }

}
