LRUCache.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. @end
  45. NS_ASSUME_NONNULL_END