Refactor string cache store usage

pull/146/head
johnniang 2019-04-29 01:34:00 +08:00
parent b0cb31ad48
commit 6e8ef4b35a
3 changed files with 10 additions and 12 deletions

View File

@ -15,7 +15,6 @@ import run.halo.app.security.context.SecurityContextHolder;
import run.halo.app.security.context.SecurityContextImpl;
import run.halo.app.security.support.UserDetail;
import run.halo.app.service.UserService;
import run.halo.app.utils.JsonUtils;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
@ -84,15 +83,14 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
if (StringUtils.isNotBlank(token)) {
// Valid the token
// TODO Add key prefix
Optional<String> userDetailOptional = cacheStore.get(token);
Optional<UserDetail> optionalUserDetail = cacheStore.getAny(token, UserDetail.class);
if (!userDetailOptional.isPresent()) {
if (!optionalUserDetail.isPresent()) {
getFailureHandler().onFailure(request, response, new AuthenticationException("The token has been expired or not exist").setErrorData(token));
return;
}
UserDetail userDetail = JsonUtils.jsonToObject(userDetailOptional.get(), UserDetail.class);
UserDetail userDetail = optionalUserDetail.get();
// Set security
SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(userDetail)));

View File

@ -36,7 +36,6 @@ import run.halo.app.service.support.HaloMediaType;
import run.halo.app.utils.FileUtils;
import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HaloUtils;
import run.halo.app.utils.JsonUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -122,12 +121,13 @@ public class ThemeServiceImpl implements ThemeService {
@Override
public Set<ThemeProperty> getThemes() {
Optional<String> themeCacheString = cacheStore.get(THEMES_CACHE_KEY);
Optional<ThemeProperty[]> themePropertiesOptional = cacheStore.getAny(THEMES_CACHE_KEY, ThemeProperty[].class);
try {
if (themeCacheString.isPresent()) {
if (themePropertiesOptional.isPresent()) {
// Convert to theme properties
ThemeProperty[] themeProperties = JsonUtils.jsonToObject(themeCacheString.get(), ThemeProperty[].class);
ThemeProperty[] themeProperties = themePropertiesOptional.get();
return new HashSet<>(Arrays.asList(themeProperties));
}
@ -142,7 +142,7 @@ public class ThemeServiceImpl implements ThemeService {
Set<ThemeProperty> themes = themePaths.stream().map(this::getProperty).collect(Collectors.toSet());
// Cache the themes
cacheStore.put(THEMES_CACHE_KEY, JsonUtils.objectToJson(themes));
cacheStore.putAny(THEMES_CACHE_KEY, themes);
return themes;
} catch (IOException e) {

View File

@ -128,7 +128,7 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
if (!BCrypt.checkpw(password, user.getPassword())) {
// If the password is mismatch
// Add login failure count
Integer loginFailureCount = stringCacheStore.get(LOGIN_FAILURE_COUNT_KEY).map(Integer::valueOf).orElse(0);
Integer loginFailureCount = stringCacheStore.getAny(LOGIN_FAILURE_COUNT_KEY, Integer.class).orElse(0);
if (loginFailureCount >= MAX_LOGIN_TRY - 1) {
// Set expiration
@ -139,7 +139,7 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
loginFailureCount++;
stringCacheStore.put(LOGIN_FAILURE_COUNT_KEY, loginFailureCount.toString(), LOCK_MINUTES, TimeUnit.MINUTES);
stringCacheStore.putAny(LOGIN_FAILURE_COUNT_KEY, loginFailureCount, LOCK_MINUTES, TimeUnit.MINUTES);
int remainder = MAX_LOGIN_TRY - loginFailureCount;