关于文心一言开发(流式接口)

首先需要获取你得accessToken,这个需要从在百度开业务,拿到APIkey与SecreKey.

有效期是一个月,我得方案是存在redis,每次获取比对时间,如果时间未到就用redis,超时则重启获取。

    private String  getRedisBaiduAccessToken() {
   
        String redisAccessToken;
        String redisTime=redisUtil.getString("baiduAccessTime");
        if (redisTime !=null){
   
            Calendar cal = Calendar.getInstance();
            Long redisAccessTime=Long.valueOf(redisTime);
            if (cal.getTimeInMillis()>redisAccessTime){
   
                redisUtil.deleteString("baiduAccessToken");
                redisUtil.deleteString("baiduAccessTime");
                redisAccessToken=pushRedisBaiduAccessToken();
                LoggerUtils.debug(getClass(),"accessToken过期,重新获取!");
            }else {
   
                redisAccessToken=  redisUtil.getString("baiduAccessToken");
            }
        }else {
   
            redisAccessToken=pushRedisBaiduAccessToken();
            LoggerUtils.debug(getClass(),"accessToken不存在,从百度获取!");
        }
        return redisAccessToken;
    }


    private String pushRedisBaiduAccessToken() {
   
        String baiduAccessToken;
        try {
   
            baiduAccessToken = getBaiduAccessToken();
        } catch (IOException e) {
   
            throw new BusinessException("baiduAccessToken获取失败");
        }
        //获取当前系统时间
        Calendar cal = Calendar.getInstance();
        //将时间增加三十天
        cal.add(Calendar.DATE, 30);
        //获取改变后的时间
        Long baiduAccessTime= cal.getTimeInMillis();
        redisUtil.deleteString("baiduAccessToken");
        redisUtil.deleteString("baiduAccessTime");
        redisUtil.

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