parent
d154aab9ed
commit
ff1ae38707
@ -0,0 +1,74 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.cache; |
||||
|
||||
import cn.dev33.satoken.stp.StpUtil; |
||||
import cn.dev33.satoken.util.SaFoxUtil; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import net.lensfrex.dscape.configure.GlobalConstant; |
||||
import net.lensfrex.dscape.dto.response.general.ResponseCode; |
||||
import net.lensfrex.dscape.exception.GlobalException; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
|
||||
@Slf4j |
||||
@Service |
||||
public class InviteCodeService { |
||||
private static final int INVITE_CODE_LENGTH = 6; |
||||
|
||||
private static final String REGISTER_INVITE_CODE_KEY = GlobalConstant.REGISTER_INVITE_CODE_KEY; |
||||
|
||||
private static final String ADMIN_INVITE_CODE_COUNTER_KEY = GlobalConstant.ADMIN_INVITE_CODE_COUNTER_KEY; |
||||
|
||||
private final RedisTemplate<String, String> redis; |
||||
|
||||
public InviteCodeService(RedisTemplate<String, String> redis) { |
||||
this.redis = redis; |
||||
} |
||||
|
||||
public List<String> generateInviteCode(long expired, int count) { |
||||
String adminUid = StpUtil.getLoginIdAsString(); |
||||
String adminInviteCodeCounterKey = String.format(ADMIN_INVITE_CODE_COUNTER_KEY, adminUid); |
||||
|
||||
String countResult = redis.opsForValue().get(adminInviteCodeCounterKey); |
||||
|
||||
if (countResult != null && Integer.parseInt(countResult) > 1024) { |
||||
throw new GlobalException(ResponseCode.INVITE_CODE_REACHED_LIMIT); |
||||
} |
||||
|
||||
List<String> inviteCodes = new ArrayList<>(count); |
||||
|
||||
for (int i = 0; i < count; i++) { |
||||
inviteCodes.add(SaFoxUtil.getRandomString(INVITE_CODE_LENGTH)); |
||||
} |
||||
HashMap<String, String> codeAdmin = new HashMap<>(count); |
||||
inviteCodes.forEach(inviteCode -> codeAdmin.put(inviteCode, adminUid)); |
||||
|
||||
redis.opsForHash().putAll(REGISTER_INVITE_CODE_KEY, codeAdmin); |
||||
redis.opsForValue().increment(adminInviteCodeCounterKey, count); |
||||
|
||||
return inviteCodes; |
||||
} |
||||
|
||||
public boolean checkInviteCode(String code) { |
||||
return redis.opsForHash().get(REGISTER_INVITE_CODE_KEY, code) == null; |
||||
} |
||||
|
||||
public String useInviteCode(String code) { |
||||
String superior = (String) redis.opsForHash().get(REGISTER_INVITE_CODE_KEY, code); |
||||
if (superior == null) { |
||||
throw new GlobalException(ResponseCode.INVITE_CODE_WRONG); |
||||
} |
||||
|
||||
redis.opsForHash().delete(REGISTER_INVITE_CODE_KEY, code); |
||||
redis.opsForValue().decrement(String.format(ADMIN_INVITE_CODE_COUNTER_KEY, superior)); |
||||
|
||||
return superior; |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.cache; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import net.lensfrex.dscape.configure.GlobalConstant; |
||||
import net.lensfrex.dscape.dao.entity.UserBasic; |
||||
import net.lensfrex.dscape.utils.ObjectJsonSerializer; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class UserBasicCache { |
||||
|
||||
private static final String USER_INFO_CACHE_KEY = GlobalConstant.USER_INFO_CACHE_KEY; |
||||
|
||||
private static final String USERNAME_UID_CACHE_KEY = GlobalConstant.USERNAME_UID_CACHE_KEY; |
||||
|
||||
private final ObjectJsonSerializer objectJsonSerializer; |
||||
|
||||
private final RedisTemplate<String, String> redis; |
||||
|
||||
public UserBasicCache(ObjectJsonSerializer objectJsonSerializer, RedisTemplate<String, String> redis) { |
||||
this.objectJsonSerializer = objectJsonSerializer; |
||||
this.redis = redis; |
||||
} |
||||
|
||||
public UserBasic getUserByUid(String uid) { |
||||
String cacheData = (String) redis.opsForHash().get(USER_INFO_CACHE_KEY, uid); |
||||
if (cacheData != null) { |
||||
return objectJsonSerializer.deserialize(cacheData, UserBasic.class); |
||||
} else { |
||||
log.debug("缓存中没有用户信息"); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public UserBasic getUserByUserName(String username) { |
||||
String uid = (String) redis.opsForHash().get(USERNAME_UID_CACHE_KEY, username); |
||||
if (uid != null) { |
||||
return this.getUserByUid(uid); |
||||
} else { |
||||
log.debug("缓存中没有用户名和uid的对应信息"); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public void save(UserBasic userBasic) { |
||||
redis.opsForHash().put(USER_INFO_CACHE_KEY, userBasic.getUid(), objectJsonSerializer.serialize(userBasic)); |
||||
redis.opsForHash().put(USERNAME_UID_CACHE_KEY, userBasic.getUserName(), userBasic.getUid()); |
||||
} |
||||
|
||||
public void clear(String uid) { |
||||
redis.opsForHash().delete(USER_INFO_CACHE_KEY, uid); |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.configure; |
||||
|
||||
public class GlobalConstant { |
||||
public static final String USER_INFO_CACHE_KEY = "dscape:user:basic"; |
||||
|
||||
public static final String USERNAME_UID_CACHE_KEY = "dscape:user:uid"; |
||||
|
||||
public static final String REGISTER_INVITE_CODE_KEY = "dscape:user:inviteCodes"; |
||||
|
||||
public static final String ADMIN_INVITE_CODE_COUNTER_KEY = "dscape:admin:inviteCodeCounter:%s"; |
||||
} |
Loading…
Reference in new issue