add
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
package com.qiaoba.auth.service;
|
||||
|
||||
import com.qiaoba.auth.entity.OnlineUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 在线用户 服务层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/25 17:15
|
||||
*/
|
||||
public interface OnlineUserService {
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param onlineUser onlineUser
|
||||
*/
|
||||
void insert(OnlineUser onlineUser);
|
||||
|
||||
/**
|
||||
* 删除(强退)
|
||||
*
|
||||
* @param username 登录账号
|
||||
*/
|
||||
void delete(String username);
|
||||
|
||||
/**
|
||||
* 查询
|
||||
*
|
||||
* @param username 登录账号
|
||||
* @return 在线用户
|
||||
*/
|
||||
OnlineUser selectByUsername(String username);
|
||||
|
||||
/**
|
||||
* 批量查询
|
||||
*
|
||||
* @param username username
|
||||
* @return list
|
||||
*/
|
||||
List<OnlineUser> selectList(String username);
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.qiaoba.auth.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.auth.constants.SecurityConstant;
|
||||
import com.qiaoba.auth.entity.OnlineUser;
|
||||
import com.qiaoba.auth.service.OnlineUserService;
|
||||
import com.qiaoba.auth.utils.TokenUtil;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 在线用户 服务层实现
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/25 17:15
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OnlineUserServiceImpl implements OnlineUserService {
|
||||
|
||||
private final RedisService redisService;
|
||||
|
||||
@Override
|
||||
public void insert(OnlineUser onlineUser) {
|
||||
redisService.set(handleKey(onlineUser.getUsername()), onlineUser, TokenUtil.expireTime * 3600);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String username) {
|
||||
redisService.del(handleKey(username));
|
||||
}
|
||||
|
||||
@Override
|
||||
public OnlineUser selectByUsername(String username) {
|
||||
if (redisService.hasKey(handleKey(username))) {
|
||||
return redisService.getObject(handleKey(username), OnlineUser.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OnlineUser> selectList(String username) {
|
||||
String key = SecurityConstant.ONLINE_USER_REDIS_KEY + "*";
|
||||
if (StrUtil.isNotBlank(username)) {
|
||||
key = key + username + "*";
|
||||
}
|
||||
|
||||
if (redisService.hasKey(key)) {
|
||||
return redisService.getObjectList(key, OnlineUser.class);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private String handleKey(String key) {
|
||||
return SecurityConstant.ONLINE_USER_REDIS_KEY + key;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user