补充调整后的缓存方案

This commit is contained in:
eggmanQQQ
2024-05-23 17:21:40 +08:00
parent 9be7bbfe48
commit 5f59ab8305
3 changed files with 124 additions and 83 deletions

View File

@@ -121,8 +121,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
@property(nonatomic,assign) BOOL isReloadTicket; @property(nonatomic,assign) BOOL isReloadTicket;
/// ///
@property(nonatomic,strong) PIFullScreenBannerAnimation *roomAnimation; @property(nonatomic,strong) PIFullScreenBannerAnimation *roomAnimation;
///
@property(nonatomic,assign) int downloadGiftCount;
@end @end
@@ -326,55 +324,42 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
} }
/// ///
-(void)requestGiftList{ -(void)requestGiftList{
[self removeOldVersionCacheFromOldMethod];
@kWeakify(self); @kWeakify(self);
[[AFNetworkReachabilityManager sharedManager] startMonitoring]; AFNetworkReachabilityManager *networkManager = [AFNetworkReachabilityManager sharedManager];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { [networkManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
@kStrongify(self); @kStrongify(self);
switch (status) { if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
case AFNetworkReachabilityStatusUnknown: if(self.isFirstReachability == NO){
break; [Api requestCacheGiftDynamicEffectList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
case AFNetworkReachabilityStatusNotReachable: if(code == 200){
self.isFirstReachability = YES;
break; NSArray *allResourceList = data.data;
case AFNetworkReachabilityStatusReachableViaWWAN: NSSet *allResourceSet = [[NSSet alloc] initWithArray:allResourceList];
case AFNetworkReachabilityStatusReachableViaWiFi: NSMutableArray *allResourceURLs = @[@"https://image.hfighting.com/fenjie2.mp4",
{ @"https://image.hfighting.com/treasure_fairy_ten.mp4",
if(self.isFirstReachability == NO){ @"https://image.hfighting.com/hecheng3.mp4",
@"https://image.hfighting.com/treasure_fairy_one.mp4"].mutableCopy;
[self dealWithGiftList]; for (NSString *url in allResourceSet) {
[Api requestCacheGiftDynamicEffectList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { NSString *encodingUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
if(code == 200){ [allResourceURLs addObject:encodingUrl];
self.isFirstReachability = YES;
NSMutableArray *giftList = [[NSMutableArray alloc]initWithArray:data.data ?: @[]];
NSArray *elfList = @[@"https://image.hfighting.com/fenjie2.mp4",@"https://image.hfighting.com/treasure_fairy_ten.mp4",@"https://image.hfighting.com/hecheng3.mp4",@"https://image.hfighting.com/treasure_fairy_one.mp4"];
[giftList addObjectsFromArray:elfList];
NSSet *setList = [[NSSet alloc]initWithArray:giftList];///
[self cacheGiftDynamicEffectList:setList];
} }
}]; [[UploadFile share] startBatchDownloadWithURLs:allResourceURLs];
} }
}];
} else {
[[UploadFile share] resumeBatchDownload];
} }
break; } else {
default: [[UploadFile share] pauseBatchDownload];
break;
} }
}]; }];
[networkManager startMonitoring];
} }
-(void)removeOldVersionCacheFromOldMethod{
/// //MARK: 2
-(void)cacheGiftDynamicEffectList:(NSSet *)list{
[UploadFile downloadGiftDynamicEffectWithList:list completion:^(BOOL isSuccess, NSMutableArray * _Nonnull failList) {
}];
}
-(void)dealWithGiftList{
///
NSString *svgaFileName = @"/GiftSvga"; NSString *svgaFileName = @"/GiftSvga";
NSString *svgaFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:svgaFileName]; NSString *svgaFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:svgaFileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:svgaFilePath]) { if ([[NSFileManager defaultManager] fileExistsAtPath:svgaFilePath]) {
@@ -382,7 +367,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
if(isSuccess == YES){ if(isSuccess == YES){
NSLog(@"删除成功"); NSLog(@"删除成功");
} }
} }
NSString *mp4FileName = @"/GiftMp4"; NSString *mp4FileName = @"/GiftMp4";
@@ -392,7 +376,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
if(isSuccess == YES){ if(isSuccess == YES){
NSLog(@"删除成功"); NSLog(@"删除成功");
} }
} }
} }

View File

@@ -37,9 +37,17 @@ NS_ASSUME_NONNULL_BEGIN
failure:(void (^)(NSNumber *resCode, NSString *message))failure; failure:(void (^)(NSNumber *resCode, NSString *message))failure;
///下载资料 ///下载资料
-(void)downloadAnimationFileName:(NSString *)fileName localPath:(NSString *)localPath completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion; - (void)downloadAnimationFileName:(NSString *)fileName localPath:(NSString *)localPath completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion;
///下载礼物特效 ///下载礼物特效
+(void)downloadGiftDynamicEffectWithList:(NSSet *)list completion:(void (^) (BOOL isSuccess, NSMutableArray *editAudioPath))completion; +(void)downloadGiftDynamicEffectWithList:(NSSet *)list completion:(void (^) (BOOL isSuccess, NSMutableArray *editAudioPath))completion;
// 批量下载
- (void)startBatchDownloadWithURLs:(NSArray<NSString *> *)URLs;
- (void)pauseBatchDownload;
- (void)resumeBatchDownload;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@@ -15,6 +15,11 @@ static UploadFile* manager;
// //
@property (nonatomic) QCloudCredentailFenceQueue* credentialFenceQueue; @property (nonatomic) QCloudCredentailFenceQueue* credentialFenceQueue;
@property(nonatomic,strong) UploadFileModel *fileModel; @property(nonatomic,strong) UploadFileModel *fileModel;
// MARK: object
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@property (nonatomic, strong) NSMutableArray<NSURLSessionDownloadTask *> *tasks;
@end @end
@implementation UploadFile @implementation UploadFile
@@ -258,9 +263,12 @@ static UploadFile* manager;
[[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request]; [[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request];
} }
+(void)downloadAudioWithFileName:(NSString *)fileName musicUrl:(NSString *)musicUrl mainFileName:(NSString *)mainFileName completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion { +(void)downloadAudioWithFileName:(NSString *)fileName
musicUrl:(NSString *)musicUrl
mainFileName:(NSString *)mainFileName
completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURL *url = [NSURL URLWithString:[musicUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; NSURL *url = [NSURL URLWithString:[musicUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL :url]; NSURLRequest *request = [NSURLRequest requestWithURL :url];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
@@ -276,49 +284,91 @@ static UploadFile* manager;
[download resume]; [download resume];
} }
+(void)downloadGiftDynamicEffectWithList:(NSSet *)list completion:(void (^) (BOOL isSuccess, NSMutableArray *editAudioPath))completion{ //
- (void)startBatchDownloadWithURLs:(NSArray<NSString *> *)URLs {
if (_manager == nil) {
_manager = [AFHTTPSessionManager manager];
_manager.operationQueue.maxConcurrentOperationCount = 10;
_tasks = [NSMutableArray array];
}
if (URLs.count == 0) {
return;
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [self createCacheLocalPath];
manager.operationQueue.maxConcurrentOperationCount = 10; //
dispatch_group_t requestGroup = dispatch_group_create(); for (NSString *urlStr in URLs) {
NSMutableArray *failList = [NSMutableArray array]; //
for (NSString *giftUrl in list) { NSURL *destinationURL = [self localPath:urlStr];
NSString *encodingUrl = [giftUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
NSString *fileName = [[encodingUrl componentsSeparatedByString:@"/"] lastObject]; //
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"GiftDynamicEffectList"]; if ([[NSFileManager defaultManager] fileExistsAtPath:[destinationURL path]]) {
NSString *fullPath = [filePath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
continue; continue;
} }
NSFileManager *fileMgr = [[NSFileManager alloc] init];
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; NSURL *url = [NSURL URLWithString:urlStr];
dispatch_group_enter(requestGroup); NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURL *url = [NSURL URLWithString:[encodingUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL :url]; @kWeakify(self);
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { __block NSURLSessionDownloadTask *downloadTask;
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { downloadTask = [self.manager downloadTaskWithRequest:request
return [NSURL fileURLWithPath:fullPath]; progress:nil
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return destinationURL;
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
dispatch_group_leave(requestGroup); @kStrongify(self);
if (!error) { if ([self.tasks containsObject:downloadTask]) {
NSLog(@"下载完成"); [self.tasks removeObject:downloadTask];
} else {
NSLog(@"下载失败");
[failList addObject:encodingUrl];
} }
}]; }];
[download resume];
[self.tasks addObject:downloadTask];
} }
dispatch_group_notify(requestGroup, dispatch_get_main_queue(), ^{
if(failList.count > 0){ [self resumeBatchDownload];
completion(NO,failList); }
NSLog(@"下载有失败,失败数:%ld,成功数:%ld,总数:%ld",failList.count,list.count - failList.count,list.count);
}else{ - (void)pauseBatchDownload {
NSLog(@"下载全部完成,总数:%ld",list.count); for (NSURLSessionDownloadTask *task in self.tasks) {
completion(NO,nil); if (task.state == NSURLSessionTaskStateRunning) {
[task suspend];
} }
}); }
}
- (void)resumeBatchDownload {
for (NSURLSessionDownloadTask *task in self.tasks) {
if (task.state == NSURLSessionTaskStateSuspended) {
[task resume];
}
}
}
- (void)createCacheLocalPath {
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [self cacheFilePath];
if ([fileManager fileExistsAtPath:filePath] == NO) {
NSFileManager *fileMgr = [[NSFileManager alloc] init];
BOOL success = [fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (success) {
NSLog(@"Folder created successfully.");
} else {
NSLog(@"Could not create folder: %@", [error localizedDescription]);
}
}
}
- (NSURL *)localPath:(NSString *)encodingUrl {
NSString *fileName = [[encodingUrl componentsSeparatedByString:@"/"] lastObject];
NSString *filePath = [self cacheFilePath];
NSString *cacheLocalPath = [filePath stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:cacheLocalPath];
}
- (NSString *)cacheFilePath {
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"GiftDynamicEffectList"];
} }
@end @end