使用spring切面統計uv

 根據解析token的兩個方法進行統計

重點:springRedis、HyperLogLog

@Aspect
@Component
@Slf4j
public class RpcLogAspect {

    @Resource
    private RedisTemplate redisTemplate;
     /**
     * 統計用戶UV
     * @param jp
     * @param rvt
     */
    @AfterReturning(returning="rvt", pointcut="execution(* com.liwen.user.dubbo.UserAuthenticationProvider.verifyAccessToken(..)) || execution(* com.liwen.user.dubbo.UserAuthenticationProvider.getUserIdByToken(..)) ")
    public void loginRecord(JoinPoint jp,Object rvt) {
        if(rvt == null){
            return;
        }
        try {
            String time = DateTimeUtils.dateToStr(new Date(), DateTimeUtils.FORMAT_DEFAULT_YMD_NS);
            String key = "user" + time;
            if(rvt instanceof AccessTokenResult){
                AccessTokenResult tokenResult = (AccessTokenResult) rvt;
                if (tokenResult != null && tokenResult.getUserId() > 0) {
                    insertHyperLogLog(tokenResult.getUserId(),key);
                }
            }else if(rvt instanceof Long && (long)rvt>0){
                insertHyperLogLog((long)rvt,key);
            }
        }catch (Exception e){
            log.info("統計用戶uv失敗",e);
        }
    }

    private void insertHyperLogLog(Long userId,String key){
        CompletableFuture.runAsync(() -> {
            redisTemplate.expire(key, 30, TimeUnit.DAYS);
            redisTemplate.opsForHyperLogLog().add(key, userId);
        });
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章