mirror of
https://github.com/tiennm99/store-scraper-bot-java.git
synced 2026-05-14 09:53:03 +00:00
feat(mongodb): manual handle expire
This commit is contained in:
@@ -2,8 +2,11 @@ package com.miti99.storescraperbot.api.apple;
|
||||
|
||||
import com.miti99.storescraperbot.api.apple.request.AppleAppRequest;
|
||||
import com.miti99.storescraperbot.api.apple.response.AppleAppResponse;
|
||||
import com.miti99.storescraperbot.constant.Constant;
|
||||
import com.miti99.storescraperbot.model.AppleApp;
|
||||
import com.miti99.storescraperbot.repository.AppleAppRepository;
|
||||
import com.miti99.storescraperbot.util.GsonUtil;
|
||||
import com.miti99.storescraperbot.util.Time;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpClient.Redirect;
|
||||
@@ -51,18 +54,23 @@ public class AppStoreScraper {
|
||||
}
|
||||
|
||||
public static AppleAppResponse getAppResponse(String appId, String country) {
|
||||
boolean isInCache = AppleAppRepository.INSTANCE.exist(appId);
|
||||
if (isInCache) {
|
||||
var app = AppleAppRepository.INSTANCE.load(appId);
|
||||
return app.getApp();
|
||||
} else {
|
||||
long now = Time.currentTimeMillis();
|
||||
AppleApp app = null;
|
||||
if (AppleAppRepository.INSTANCE.exist(appId)) {
|
||||
app = AppleAppRepository.INSTANCE.load(appId);
|
||||
if (now - app.getMillis() > Constant.APP_CACHE_MILLIS) {
|
||||
app = null;
|
||||
}
|
||||
}
|
||||
if (app == null) {
|
||||
var response = app(new AppleAppRequest(appId, country));
|
||||
AppleAppRepository.INSTANCE.init(appId);
|
||||
var app = AppleAppRepository.INSTANCE.load(appId);
|
||||
app = AppleAppRepository.INSTANCE.load(appId);
|
||||
app.setApp(response);
|
||||
app.setMillis(now);
|
||||
AppleAppRepository.INSTANCE.save(appId, app);
|
||||
return response;
|
||||
}
|
||||
return app.getApp();
|
||||
}
|
||||
|
||||
public static LocalDate getAppUpdated(String appId, String country) {
|
||||
|
||||
@@ -2,8 +2,11 @@ package com.miti99.storescraperbot.api.google;
|
||||
|
||||
import com.miti99.storescraperbot.api.google.request.GoogleAppRequest;
|
||||
import com.miti99.storescraperbot.api.google.response.GoogleAppResponse;
|
||||
import com.miti99.storescraperbot.constant.Constant;
|
||||
import com.miti99.storescraperbot.model.GoogleApp;
|
||||
import com.miti99.storescraperbot.repository.GoogleAppRepository;
|
||||
import com.miti99.storescraperbot.util.GsonUtil;
|
||||
import com.miti99.storescraperbot.util.Time;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpClient.Redirect;
|
||||
@@ -50,18 +53,23 @@ public class GooglePlayScraper {
|
||||
}
|
||||
|
||||
private static GoogleAppResponse getAppResponse(String appId, String country) {
|
||||
boolean isInCache = GoogleAppRepository.INSTANCE.exist(appId);
|
||||
if (isInCache) {
|
||||
var app = GoogleAppRepository.INSTANCE.load(appId);
|
||||
return app.getApp();
|
||||
} else {
|
||||
long now = Time.currentTimeMillis();
|
||||
GoogleApp app = null;
|
||||
if (GoogleAppRepository.INSTANCE.exist(appId)) {
|
||||
app = GoogleAppRepository.INSTANCE.load(appId);
|
||||
if (now - app.getMillis() > Constant.APP_CACHE_MILLIS) {
|
||||
app = null;
|
||||
}
|
||||
}
|
||||
if (app == null) {
|
||||
var response = app(new GoogleAppRequest(appId, country));
|
||||
GoogleAppRepository.INSTANCE.init(appId);
|
||||
var app = GoogleAppRepository.INSTANCE.load(appId);
|
||||
app = GoogleAppRepository.INSTANCE.load(appId);
|
||||
app.setApp(response);
|
||||
app.setMillis(now);
|
||||
GoogleAppRepository.INSTANCE.save(appId, app);
|
||||
return response;
|
||||
}
|
||||
return app.getApp();
|
||||
}
|
||||
|
||||
public static LocalDate getLastUpdateOfApp(String appId, String country) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Set;
|
||||
|
||||
public class Constant {
|
||||
public static final long APP_CACHE_SECONDS = 600;
|
||||
public static final long APP_CACHE_MILLIS = APP_CACHE_SECONDS * 1000;
|
||||
public static final long NUM_DAYS_WARNING_NOT_UPDATED = 30;
|
||||
public static final LocalTime SCHEDULE_CHECK_APP_TIME = LocalTime.of(7, 0);
|
||||
|
||||
|
||||
@@ -8,5 +8,5 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class AppleApp extends AbstractModel {
|
||||
AppleAppResponse app;
|
||||
long millis; // TODO: handle expire
|
||||
long millis;
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class GoogleApp extends AbstractModel {
|
||||
GoogleAppResponse app;
|
||||
long millis; // TODO: handle expire
|
||||
long millis;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.miti99.storescraperbot.util;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class Time {
|
||||
private static final TimeZone TIME_ZONE = TimeZone.getDefault();
|
||||
private static final ZoneId ZONE_ID = TIME_ZONE.toZoneId();
|
||||
private static long deltaMillis = 0L;
|
||||
|
||||
public static LocalDate currentDate() {
|
||||
return LocalDate.ofInstant(currentInstant(), ZONE_ID);
|
||||
}
|
||||
|
||||
public static LocalTime currentTime() {
|
||||
return LocalTime.ofInstant(currentInstant(), ZONE_ID);
|
||||
}
|
||||
|
||||
public static LocalDateTime currentDateTime() {
|
||||
return LocalDateTime.ofInstant(currentInstant(), ZONE_ID);
|
||||
}
|
||||
|
||||
public static OffsetDateTime currentOffsetDateTime() {
|
||||
return OffsetDateTime.ofInstant(currentInstant(), ZONE_ID);
|
||||
}
|
||||
|
||||
public static ZonedDateTime currentZonedDateTime() {
|
||||
return ZonedDateTime.ofInstant(currentInstant(), ZONE_ID);
|
||||
}
|
||||
|
||||
public static Instant currentInstant() {
|
||||
return Instant.ofEpochMilli(currentTimeMillis());
|
||||
}
|
||||
|
||||
public static long currentTimeMillis() {
|
||||
return System.currentTimeMillis() + deltaMillis;
|
||||
}
|
||||
|
||||
public static void useMockTime(LocalDateTime dateTime, ZoneId zoneId) {
|
||||
deltaMillis = dateTime.atZone(zoneId).toInstant().toEpochMilli() - System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static void useSystemDefaultZoneClock() {
|
||||
deltaMillis = 0L;
|
||||
}
|
||||
|
||||
private static Clock getClock() {
|
||||
return Clock.fixed(currentInstant(), ZONE_ID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user