// // RQCacheManager.m // JSJP // // Created by 张嵘 on 2021/8/17. // #import "RQCacheManager.h" @interface RQCacheManager () @property (nonatomic, readwrite, strong) YYCache *cache; @property (nonatomic, readonly, copy) NSArray *pathArr; @end @implementation RQCacheManager @def_singleton(RQCacheManager); - (void)removeAllCache { [self.cache removeAllObjects]; } - (void)removeCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters { @weakify(self) NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path]; NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path]; if (RQObjectIsNil(parameters)) { [self.cache removeObjectForKey:cacheResponseKeyName]; [self.cache removeObjectForKey:cacheResponseObjectKeyName]; } else { [[parameters.allKeys.rac_sequence.signal filter:^BOOL(NSString *key) { NSString *value = [NSString stringWithFormat:@"%@",parameters[key]]; return RQStringIsNotEmpty(value); }] subscribeNext:^(NSString *key) { NSString *value = parameters[key]; [cacheResponseKeyName appendFormat:@"=%@",value]; [cacheResponseObjectKeyName appendFormat:@"=%@",value]; } completed:^{ @strongify(self) [self.cache removeObjectForKey:cacheResponseKeyName]; [self.cache removeObjectForKey:cacheResponseObjectKeyName]; }]; } } - (BOOL)isNeedCacheWithPath:(NSString *)path { return [self.pathArr containsObject:path]; } - (void)saveCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters response:(NSURLResponse *)response responseObject:(NSDictionary *)responseObject { @weakify(self) NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path]; NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path]; if (RQObjectIsNil(parameters)) { [self.cache setObject:response forKey:cacheResponseKeyName]; [self.cache setObject:responseObject forKey:cacheResponseObjectKeyName]; } else { [[parameters.allKeys.rac_sequence.signal filter:^BOOL(NSString *key) { NSString *value = [NSString stringWithFormat:@"%@",parameters[key]]; return RQStringIsNotEmpty(value); }] subscribeNext:^(NSString *key) { NSString *value = parameters[key]; [cacheResponseKeyName appendFormat:@"=%@",value]; [cacheResponseObjectKeyName appendFormat:@"=%@",value]; } completed:^{ @strongify(self) [self.cache setObject:response forKey:cacheResponseKeyName]; [self.cache setObject:responseObject forKey:cacheResponseObjectKeyName]; }]; } } - (NSDictionary *)getCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters { NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path]; NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path]; if (RQObjectIsNil(parameters)) { NSURLResponse *response = (NSURLResponse *)[self.cache objectForKey:cacheResponseKeyName]; NSDictionary *responseObject = (NSDictionary *)[self.cache objectForKey:cacheResponseObjectKeyName]; return (!RQObjectIsNil(response) && !RQObjectIsNil(responseObject))? @{@"response" : response, @"responseObject" : responseObject} : nil; } else { for (NSString *key in parameters.allKeys) { NSString *value = [NSString stringWithFormat:@"%@",parameters[key]]; if (RQStringIsNotEmpty(value)) { [cacheResponseKeyName appendFormat:@"=%@",value]; [cacheResponseObjectKeyName appendFormat:@"=%@",value]; } } NSURLResponse *response = (NSURLResponse *)[self.cache objectForKey:cacheResponseKeyName]; NSDictionary *responseObject = (NSDictionary *)[self.cache objectForKey:cacheResponseObjectKeyName]; return (!RQObjectIsNil(response) && !RQObjectIsNil(responseObject))? @{@"response" : response, @"responseObject" : responseObject} : nil; } } - (YYCache *)cache { if (!_cache) { _cache = [[YYCache alloc] initWithName:@"SDJKRequestCache"]; } return _cache; } - (NSArray *)pathArr { return @[ RQ_GET_TeachingVideoByTypeId, RQ_GET_Chapter_XC_1, RQ_GET_Chapter_XC_4, RQ_GET_Chapter_HC_1, RQ_GET_Chapter_HC_4, RQ_GET_Chapter_KC_1, RQ_GET_Chapter_KC_4, RQ_GET_Chapter_MTC_1, RQ_GET_Chapter_MTC_4, RQ_GET_Chapter_ZGZ_JLY, RQ_GET_Chapter_ZGZ_KY, RQ_GET_Chapter_ZGZ_HY, RQ_GET_Chapter_ZGZ_WXP, RQ_GET_Chapter_ZGZ_CZC, RQ_GET_Chapter_ZGZ_WYC, ]; } @end