RQCacheManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // RQCacheManager.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/17.
  6. //
  7. #import "RQCacheManager.h"
  8. @interface RQCacheManager ()
  9. @property (nonatomic, readwrite, strong) YYCache *cache;
  10. @property (nonatomic, readonly, copy) NSArray *pathArr;
  11. @end
  12. @implementation RQCacheManager
  13. @def_singleton(RQCacheManager);
  14. - (void)removeAllCache {
  15. [self.cache removeAllObjects];
  16. }
  17. - (void)removeCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters {
  18. @weakify(self)
  19. NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path];
  20. NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path];
  21. if (RQObjectIsNil(parameters)) {
  22. [self.cache removeObjectForKey:cacheResponseKeyName];
  23. [self.cache removeObjectForKey:cacheResponseObjectKeyName];
  24. } else {
  25. [[parameters.allKeys.rac_sequence.signal filter:^BOOL(NSString *key) {
  26. NSString *value = [NSString stringWithFormat:@"%@",parameters[key]];
  27. return RQStringIsNotEmpty(value);
  28. }] subscribeNext:^(NSString *key) {
  29. NSString *value = parameters[key];
  30. [cacheResponseKeyName appendFormat:@"=%@",value];
  31. [cacheResponseObjectKeyName appendFormat:@"=%@",value];
  32. } completed:^{
  33. @strongify(self)
  34. [self.cache removeObjectForKey:cacheResponseKeyName];
  35. [self.cache removeObjectForKey:cacheResponseObjectKeyName];
  36. }];
  37. }
  38. }
  39. - (BOOL)isNeedCacheWithPath:(NSString *)path {
  40. return [self.pathArr containsObject:path];
  41. }
  42. - (void)saveCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters response:(NSURLResponse *)response responseObject:(NSDictionary *)responseObject {
  43. @weakify(self)
  44. NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path];
  45. NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path];
  46. if (RQObjectIsNil(parameters)) {
  47. [self.cache setObject:response forKey:cacheResponseKeyName];
  48. [self.cache setObject:responseObject forKey:cacheResponseObjectKeyName];
  49. } else {
  50. [[parameters.allKeys.rac_sequence.signal filter:^BOOL(NSString *key) {
  51. NSString *value = [NSString stringWithFormat:@"%@",parameters[key]];
  52. return RQStringIsNotEmpty(value);
  53. }] subscribeNext:^(NSString *key) {
  54. NSString *value = parameters[key];
  55. [cacheResponseKeyName appendFormat:@"=%@",value];
  56. [cacheResponseObjectKeyName appendFormat:@"=%@",value];
  57. } completed:^{
  58. @strongify(self)
  59. [self.cache setObject:response forKey:cacheResponseKeyName];
  60. [self.cache setObject:responseObject forKey:cacheResponseObjectKeyName];
  61. }];
  62. }
  63. }
  64. - (NSDictionary *)getCacheWithPath:(NSString *)path parameters:(NSDictionary * __nullable)parameters {
  65. NSMutableString *cacheResponseKeyName = [NSMutableString stringWithFormat:@"Response-%@",path];
  66. NSMutableString *cacheResponseObjectKeyName = [NSMutableString stringWithFormat:@"ResponseObject-%@",path];
  67. if (RQObjectIsNil(parameters)) {
  68. NSURLResponse *response = (NSURLResponse *)[self.cache objectForKey:cacheResponseKeyName];
  69. NSDictionary *responseObject = (NSDictionary *)[self.cache objectForKey:cacheResponseObjectKeyName];
  70. return (!RQObjectIsNil(response) && !RQObjectIsNil(responseObject))? @{@"response" : response, @"responseObject" : responseObject} : nil;
  71. } else {
  72. for (NSString *key in parameters.allKeys) {
  73. NSString *value = [NSString stringWithFormat:@"%@",parameters[key]];
  74. if (RQStringIsNotEmpty(value)) {
  75. [cacheResponseKeyName appendFormat:@"=%@",value];
  76. [cacheResponseObjectKeyName appendFormat:@"=%@",value];
  77. }
  78. }
  79. NSURLResponse *response = (NSURLResponse *)[self.cache objectForKey:cacheResponseKeyName];
  80. NSDictionary *responseObject = (NSDictionary *)[self.cache objectForKey:cacheResponseObjectKeyName];
  81. return (!RQObjectIsNil(response) && !RQObjectIsNil(responseObject))? @{@"response" : response, @"responseObject" : responseObject} : nil;
  82. }
  83. }
  84. - (YYCache *)cache {
  85. if (!_cache) {
  86. _cache = [[YYCache alloc] initWithName:@"SDJKRequestCache"];
  87. }
  88. return _cache;
  89. }
  90. - (NSArray *)pathArr {
  91. return @[
  92. RQ_GET_TeachingVideoByTypeId,
  93. RQ_GET_Chapter_XC_1,
  94. RQ_GET_Chapter_XC_4,
  95. RQ_GET_Chapter_HC_1,
  96. RQ_GET_Chapter_HC_4,
  97. RQ_GET_Chapter_KC_1,
  98. RQ_GET_Chapter_KC_4,
  99. RQ_GET_Chapter_MTC_1,
  100. RQ_GET_Chapter_MTC_4,
  101. RQ_GET_Chapter_ZGZ_JLY,
  102. RQ_GET_Chapter_ZGZ_KY,
  103. RQ_GET_Chapter_ZGZ_HY,
  104. RQ_GET_Chapter_ZGZ_WXP,
  105. RQ_GET_Chapter_ZGZ_CZC,
  106. RQ_GET_Chapter_ZGZ_WYC,
  107. ];
  108. }
  109. @end