com.github.rexsheng.springboot.faster.system.auth.domain.TokenContainer Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.auth.domain;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
@ConditionalOnClass(RedisTemplate.class)
public class TokenContainer {
@Autowired(required = false)
private RedisTemplate redisTemplate;
private Map container=new HashMap<>();
public Boolean save(String key,Object value,long expireSeconds){
if(redisTemplate!=null){
redisTemplate.opsForValue().set(key,value,expireSeconds, TimeUnit.SECONDS);
}
else{
container.put(key, new ExpiredObject(value,expireSeconds));
}
return true;
}
public V getAndDel(String key){
if(redisTemplate!=null){
return (V) redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
String value= (String)operations.opsForValue().get(key);
operations.delete(key);
return value;
}
});
}
ExpiredObject value=container.get(key);
if(value!=null){
remove(key);
return (V) value.getObject();
}
return null;
}
public V get(String key){
if(redisTemplate!=null){
return (V) redisTemplate.opsForValue().get(key);
}
ExpiredObject value=container.get(key);
if(value!=null){
if(!value.isExpired()){
return (V) value.getObject();
}
remove(key);
}
return null;
}
public List getKeysWithPrefix(String prefix){
if(redisTemplate!=null){
return redisTemplate.keys(prefix+"*").stream().toList();
}
List keys=new ArrayList<>();
for(Map.Entry entry:container.entrySet()){
if(entry.getKey().startsWith(prefix)){
keys.add(entry.getKey());
}
}
return keys;
}
public void remove(String key){
if(redisTemplate!=null){
redisTemplate.delete(key);
}
else{
container.remove(key);
}
}
public void renew(String key,long expireSeconds){
if(redisTemplate!=null){
redisTemplate.expire(key,expireSeconds, TimeUnit.SECONDS);
}
else{
ExpiredObject value=container.get(key);
if(value!=null){
save(key,new ExpiredObject(value.getObject(),expireSeconds),expireSeconds);
}
}
}
public static class ExpiredObject{
private Object object;
private Long expiredSeconds;
private LocalDateTime expiredDate;
public ExpiredObject(Object object, Long expiredSeconds) {
this.object = object;
this.expiredSeconds = expiredSeconds;
this.expiredDate= DateUtil.currentDateTime().plusSeconds(expiredSeconds);
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public Long getExpiredSeconds() {
return expiredSeconds;
}
public void setExpiredSeconds(Long expiredSeconds) {
this.expiredSeconds = expiredSeconds;
}
public Boolean isExpired(){
return DateUtil.currentDateTime().isAfter(expiredDate);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy