Do more for cache

This commit is contained in:
xfl03 committed 2016-11-06 13:36:26 +08:00
1 parent bdead01ef9
commit 7e522011e7
5 files changed
+17 -18

No files matched your search

@@ -65,7 +65,7 @@ public class JsonAPILoader implements ProfileLoader.IProfileLoader {
}
json=IOUtils.toString(new FileInputStream(jsonFile));
}else{
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(jsonUrl).setUserAgent(ssp.userAgent).setPayload(type.jsonAPI.getPayload(ssp)));
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(jsonUrl).setCacheTime(60).setUserAgent(ssp.userAgent).setPayload(type.jsonAPI.getPayload(ssp)));
json=responce.content;
}
if(json==null||json.equals("")){
@@ -30,7 +30,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
if(skinFile.exists()&&skinFile.isFile())
profile.skinUrl=HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified()));
}else{
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setLoadContent(false));
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setLoadContent(false).setCacheTime(60));
if(responce.success)
profile.skinUrl=HttpTextureUtil.getLegacyFakeUrl(skin);
}
@@ -43,7 +43,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
if(capeFile.exists()&&capeFile.isFile())
profile.capeUrl=HttpTextureUtil.getLocalLegacyFakeUrl(cape, HttpTextureUtil.getHash(cape, capeFile.length(), capeFile.lastModified()));
}else{
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setLoadContent(false));
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setLoadContent(false).setCacheTime(60));
if(responce.success)
profile.capeUrl=HttpTextureUtil.getLegacyFakeUrl(cape);
}
@@ -55,7 +55,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader {
if(elytraFile.exists()&&elytraFile.isFile())
profile.elytraUrl=HttpTextureUtil.getLocalLegacyFakeUrl(elytra, HttpTextureUtil.getHash(elytra, elytraFile.length(), elytraFile.lastModified()));
}else{
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setLoadContent(false));
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setLoadContent(false).setCacheTime(60));
if(responce.success)
profile.elytraUrl=HttpTextureUtil.getLegacyFakeUrl(elytra);
}
@@ -151,6 +151,7 @@ public class HttpRequestUtil {
cacheInfo.expire=getExpire(c,request.cacheTime);
FileUtils.write(cacheInfoFile, CustomSkinLoader.GSON.toJson(cacheInfo));
}
CustomSkinLoader.logger.info("Saved to cache (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"' , Expire: "+cacheInfo.expire+")");
}
if(!request.loadContent)
return responce;
@@ -187,13 +188,12 @@ public class HttpRequestUtil {
}
return responce;
}
private final static Pattern MAX_AGE_PATTERN = Pattern.compile("max-age=(\\d+)");
private final static Pattern MAX_AGE_PATTERN = Pattern.compile("(\\d+)");
private static long getExpire(HttpURLConnection connection,int cacheTime){
String cacheControl=connection.getHeaderField("Cache-Control");
if(StringUtils.isNotEmpty(cacheControl)){
cacheControl=cacheControl.replaceAll(" ", "");
Matcher m=MAX_AGE_PATTERN.matcher(cacheControl);
if(m!=null && m.matches())
if(m!=null && (m.matches()||m.find()))
return TimeUtil.getUnixTimestamp(Long.parseLong(m.group(0)));
}
long expires=connection.getExpiration();
@@ -35,9 +35,11 @@ public class HttpTextureUtil {
}
if(fakeUrl.startsWith(LOCAL_LEGACY_MARK)){
fakeUrl=fakeUrl.replaceFirst(LOCAL_LEGACY_MARK, "");
info.url=fakeUrl;
info.hash=DigestUtils.sha1Hex(info.url);;
info.cacheFile=HttpTextureUtil.getCacheFile(info.hash);
String[] t=fakeUrl.split(",",2);
if(t.length!=2)
return info;
info.cacheFile=new File(CustomSkinLoader.DATA_DIR,t[1]);
info.hash=t[0];
return info;
}
if(fakeUrl.startsWith(LOCAL_MARK)){
@@ -48,12 +50,9 @@ public class HttpTextureUtil {
}
if(fakeUrl.startsWith(LEGACY_MARK)){
fakeUrl=fakeUrl.replaceFirst(LEGACY_MARK, "");
String[] t=fakeUrl.split(",",2);
if(t.length!=2)
return info;
info.url=t[1];
info.hash=t[0];
info.cacheFile=getCacheFile(info.hash);
info.url=fakeUrl;
info.hash=DigestUtils.sha1Hex(info.url);;
info.cacheFile=HttpTextureUtil.getCacheFile(info.hash);
return info;
}
return info;
@@ -54,11 +54,11 @@ public class MinecraftUtil {
for(String version:getMinecraftVersions()){
Matcher m = null;
try{
m=MINECRAFT_VERSION_PATTERN.matcher(version);
m=MINECRAFT_VERSION_PATTERN.matcher(" "+version+" ");
}catch(Exception e){
e.printStackTrace();
}
if(m==null || !m.matches())
if(m==null || (!m.matches() && !m.find()))
continue;
minecraftMainVersion=m.group(0);
break;