AVIMCache.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // AVIMCache.m
  3. // TCShow
  4. //
  5. // Created by AlexiChen on 16/4/14.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "AVIMCache.h"
  9. @implementation AVIMCache
  10. - (instancetype)initWith:(NSUInteger)capacity
  11. {
  12. if (self = [super init])
  13. {
  14. _capacity = capacity;
  15. _cahceQueue = [[NSMutableArray alloc] init];
  16. }
  17. return self;
  18. }
  19. - (NSUInteger)count
  20. {
  21. return [_cahceQueue count];
  22. }
  23. - (void)enCache:(id)obj
  24. {
  25. if (obj)
  26. {
  27. if ([_cahceQueue count] == _capacity)
  28. {
  29. [_cahceQueue removeObjectAtIndex:0];
  30. }
  31. [_cahceQueue addObject:obj];
  32. }
  33. }
  34. - (id)deCache
  35. {
  36. if (_cahceQueue.count)
  37. {
  38. id obj = [_cahceQueue objectAtIndex:0];
  39. [_cahceQueue removeObjectAtIndex:0];
  40. return obj;
  41. }
  42. return nil;
  43. }
  44. - (void)clear
  45. {
  46. [_cahceQueue removeAllObjects];
  47. }
  48. @end
  49. // 会自动增长
  50. @implementation AVIMMutableCache
  51. - (void)enCache:(id)obj
  52. {
  53. if (obj)
  54. {
  55. [_cahceQueue addObject:obj];
  56. }
  57. }
  58. - (void)clear
  59. {
  60. [_cahceQueue removeAllObjects];
  61. }
  62. @end