FCachePolicy.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseDatabase/Sources/Persistence/FCachePolicy.h"
  17. @interface FLRUCachePolicy ()
  18. @property(nonatomic, readwrite) NSUInteger maxSize;
  19. @end
  20. static const NSUInteger kFServerUpdatesBetweenCacheSizeChecks = 1000;
  21. static const NSUInteger kFMaxNumberOfPrunableQueriesToKeep = 1000;
  22. static const float kFPercentOfQueriesToPruneAtOnce = 0.2f;
  23. @implementation FLRUCachePolicy
  24. - (id)initWithMaxSize:(NSUInteger)maxSize {
  25. self = [super init];
  26. if (self != nil) {
  27. self->_maxSize = maxSize;
  28. }
  29. return self;
  30. }
  31. - (BOOL)shouldPruneCacheWithSize:(NSUInteger)cacheSize
  32. numberOfTrackedQueries:(NSUInteger)numTrackedQueries {
  33. return cacheSize > self.maxSize ||
  34. numTrackedQueries > kFMaxNumberOfPrunableQueriesToKeep;
  35. }
  36. - (BOOL)shouldCheckCacheSize:(NSUInteger)serverUpdatesSinceLastCheck {
  37. return serverUpdatesSinceLastCheck > kFServerUpdatesBetweenCacheSizeChecks;
  38. }
  39. - (float)percentOfQueriesToPruneAtOnce {
  40. return kFPercentOfQueriesToPruneAtOnce;
  41. }
  42. - (NSUInteger)maxNumberOfQueriesToKeep {
  43. return kFMaxNumberOfPrunableQueriesToKeep;
  44. }
  45. @end
  46. @implementation FNoCachePolicy
  47. + (FNoCachePolicy *)noCachePolicy {
  48. return [[FNoCachePolicy alloc] init];
  49. }
  50. - (BOOL)shouldPruneCacheWithSize:(NSUInteger)cacheSize
  51. numberOfTrackedQueries:(NSUInteger)numTrackedQueries {
  52. return NO;
  53. }
  54. - (BOOL)shouldCheckCacheSize:(NSUInteger)serverUpdatesSinceLastCheck {
  55. return NO;
  56. }
  57. - (float)percentOfQueriesToPruneAtOnce {
  58. return 0;
  59. }
  60. - (NSUInteger)maxNumberOfQueriesToKeep {
  61. return NSUIntegerMax;
  62. }
  63. @end