常用校验token加上二级缓存
This commit is contained in:
parent
0f25b9414e
commit
c69efa101f
|
@ -57,4 +57,13 @@
|
||||||
overflowToDisk="false"
|
overflowToDisk="false"
|
||||||
memoryStoreEvictionPolicy="LRU"/>
|
memoryStoreEvictionPolicy="LRU"/>
|
||||||
|
|
||||||
|
<!-- getByToken缓存 -->
|
||||||
|
<cache name="getByToken"
|
||||||
|
maxElementsInMemory="10000"
|
||||||
|
eternal="false"
|
||||||
|
timeToIdleSeconds="60"
|
||||||
|
timeToLiveSeconds="600"
|
||||||
|
overflowToDisk="true"
|
||||||
|
memoryStoreEvictionPolicy="LRU"/>
|
||||||
|
|
||||||
</ehcache>
|
</ehcache>
|
|
@ -4,6 +4,8 @@ import io.renren.common.service.impl.BaseServiceImpl;
|
||||||
import io.renren.dao.TokenDao;
|
import io.renren.dao.TokenDao;
|
||||||
import io.renren.entity.TokenEntity;
|
import io.renren.entity.TokenEntity;
|
||||||
import io.renren.service.TokenService;
|
import io.renren.service.TokenService;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -12,17 +14,20 @@ import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> implements TokenService {
|
public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> implements TokenService {
|
||||||
|
private static final String getByTokenKey = "getByToken";
|
||||||
/**
|
/**
|
||||||
* 12小时后过期
|
* 12小时后过期
|
||||||
*/
|
*/
|
||||||
private final static int EXPIRE = 3600 * 12;
|
private final static int EXPIRE = 3600 * 12;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Cacheable(value = getByTokenKey, key = "#p1")
|
||||||
public TokenEntity getByToken(String token) {
|
public TokenEntity getByToken(String token) {
|
||||||
return baseDao.getByToken(token);
|
return baseDao.getByToken(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@CacheEvict(key = getByTokenKey, allEntries = true)
|
||||||
public TokenEntity createToken(Long userId) {
|
public TokenEntity createToken(Long userId) {
|
||||||
//当前时间
|
//当前时间
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
@ -34,7 +39,7 @@ public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> imp
|
||||||
|
|
||||||
//判断是否生成过token
|
//判断是否生成过token
|
||||||
TokenEntity tokenEntity = baseDao.getByUserId(userId);
|
TokenEntity tokenEntity = baseDao.getByUserId(userId);
|
||||||
if(tokenEntity == null){
|
if (tokenEntity == null) {
|
||||||
//生成一个token
|
//生成一个token
|
||||||
token = generateToken();
|
token = generateToken();
|
||||||
|
|
||||||
|
@ -46,12 +51,12 @@ public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> imp
|
||||||
|
|
||||||
//保存token
|
//保存token
|
||||||
this.insert(tokenEntity);
|
this.insert(tokenEntity);
|
||||||
}else{
|
} else {
|
||||||
//判断token是否过期
|
//判断token是否过期
|
||||||
if(tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){
|
if (tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()) {
|
||||||
//token过期,重新生成token
|
//token过期,重新生成token
|
||||||
token = generateToken();
|
token = generateToken();
|
||||||
}else {
|
} else {
|
||||||
token = tokenEntity.getToken();
|
token = tokenEntity.getToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +72,7 @@ public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> imp
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expireToken(Long userId){
|
public void expireToken(Long userId) {
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
TokenEntity tokenEntity = new TokenEntity();
|
TokenEntity tokenEntity = new TokenEntity();
|
||||||
|
@ -78,7 +83,7 @@ public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> imp
|
||||||
this.updateById(tokenEntity);
|
this.updateById(tokenEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateToken(){
|
private String generateToken() {
|
||||||
return UUID.randomUUID().toString().replace("-", "");
|
return UUID.randomUUID().toString().replace("-", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue