LRUCache.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * See the NOTICE file distributed with this work for additional
  5. * information regarding copyright ownership.
  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 <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. @interface LRUCache : NSObject
  19. /*! Maximum cache capacity. Could only be set in the constructor */
  20. @property (nonatomic, readonly) NSUInteger capacity;
  21. /**
  22. Constructs a new LRU cache instance with the given capacity
  23. @param capacity Maximum cache capacity
  24. */
  25. - (instancetype)initWithCapacity:(NSUInteger)capacity;
  26. /**
  27. Puts a new object into the cache. nil cannot be stored in the cache.
  28. @param object Object to put
  29. @param key Object's key
  30. */
  31. - (void)setObject:(id)object forKey:(id<NSCopying>)key;
  32. /**
  33. Retrieves an object from the cache. Every time this method is called the matched
  34. object is bumped in the cache (if exists)
  35. @param key Object's key
  36. @returns Either the stored instance or nil if the object does not exist or has expired
  37. */
  38. - (nullable id)objectForKey:(id<NSCopying>)key;
  39. /**
  40. Retrieves all values from the cache ORDERED by recent bump. No bump is performed
  41. @return Array of all cache values ordred by recent usage (oldest items are at the tail)
  42. */
  43. - (NSArray *)allObjects;
  44. /**
  45. Removes the object associated with the specified key from the cache.
  46. @param key The key identifying the object to remove.
  47. */
  48. - (void)removeObjectForKey:(id<NSCopying>)key;
  49. @end
  50. NS_ASSUME_NONNULL_END