Skip to content

Commit

Permalink
handle cache timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Nov 6, 2023
1 parent 9e74e8f commit 8dac547
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 3 additions & 9 deletions QiniuSDK/Common/QNAutoZone.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ + (QNCache *)zoneShareCache {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
QNCacheOption *option = [[QNCacheOption alloc] init];
option.version = @"v1.0.0";
option.version = @"v1.0.1";
queryCache = [QNCache cache:[QNZonesInfo class] option:option];
});
return queryCache;
Expand Down Expand Up @@ -130,7 +130,6 @@ - (void)preQuery:(QNUpToken *)token on:(QNPrequeryReturn)ret {
return;
}

__block int32_t saveTime = 0;
kQNWeakSelf;
QNSingleFlight *singleFlight = [QNAutoZone UCQuerySingleFlight];
[singleFlight perform:token.index action:^(QNSingleFlightComplete _Nonnull complete) {
Expand Down Expand Up @@ -162,13 +161,8 @@ - (void)preQuery:(QNUpToken *)token on:(QNPrequeryReturn)ret {
if (responseInfo && responseInfo.isOK) {
QNZonesInfo *zonesInfo = [QNZonesInfo infoWithDictionary:response];
if ([zonesInfo isValid]) {
@synchronized (cacheKey) {
if (saveTime == 0) {
saveTime++;
[self setZonesInfo:zonesInfo forKey:cacheKey];
[[QNAutoZone zoneShareCache] cache:zonesInfo forKey:cacheKey atomically:false];
}
}
[self setZonesInfo:zonesInfo forKey:cacheKey];
[[QNAutoZone zoneShareCache] cache:zonesInfo forKey:cacheKey atomically:false];
ret(0, responseInfo, metrics);
} else {
ret(NSURLErrorCannotDecodeRawData, responseInfo, metrics);
Expand Down
14 changes: 11 additions & 3 deletions QiniuSDK/Common/QNZoneInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "QNZoneInfo.h"
#import "QNUtils.h"

NSString * const QNZoneInfoSDKDefaultIOHost = @"default_io_host";
NSString * const QNZoneInfoEmptyRegionId = @"none";
Expand Down Expand Up @@ -52,11 +53,17 @@ + (QNZoneInfo *)zoneInfoWithMainHosts:(NSArray <NSString *> *)mainHosts
return zoneInfo;
}

+ (QNZoneInfo *)zoneInfoFromDictionary:(NSDictionary *)detailInfo {
if (![detailInfo isKindOfClass:[NSDictionary class]]) {
+ (QNZoneInfo *)zoneInfoFromDictionary:(NSDictionary *)detail {
if (![detail isKindOfClass:[NSDictionary class]]) {
return nil;
}

NSMutableDictionary *detailInfo = [detail mutableCopy];
if (detailInfo[@"timestamp"] == nil) {
detailInfo[@"timestamp"] = @([QNUtils currentTimestamp]*0.001);
}
long timestamp = [detailInfo[@"timestamp"] longValue];

NSString *regionId = [detailInfo objectForKey:@"region"];
if (regionId == nil) {
regionId = QNZoneInfoEmptyRegionId;
Expand All @@ -73,6 +80,7 @@ + (QNZoneInfo *)zoneInfoFromDictionary:(NSDictionary *)detailInfo {

NSMutableArray *allHosts = [NSMutableArray array];
QNZoneInfo *zoneInfo = [[QNZoneInfo alloc] init:ttl regionId:regionId];
zoneInfo.buildDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
zoneInfo.http3Enabled = http3Enabled;
if ([domains isKindOfClass:[NSArray class]]) {
zoneInfo.domains = domains;
Expand All @@ -84,7 +92,7 @@ + (QNZoneInfo *)zoneInfoFromDictionary:(NSDictionary *)detailInfo {
}
zoneInfo.allHosts = [allHosts copy];

zoneInfo.detailInfo = detailInfo;
zoneInfo.detailInfo = [detailInfo copy];

return zoneInfo;
}
Expand Down

0 comments on commit 8dac547

Please sign in to comment.