| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef IOS_CHROME_BROWSER_SNAPSHOTS_LRU_CACHE_H_ |
| 6 #define IOS_CHROME_BROWSER_SNAPSHOTS_LRU_CACHE_H_ |
| 7 |
| 8 #import <Foundation/Foundation.h> |
| 9 |
| 10 // The LRUCache delegate is called before an item is evicted from the cache. |
| 11 @protocol LRUCacheDelegate |
| 12 |
| 13 - (void)lruCacheWillEvictObject:(id<NSObject>)object; |
| 14 |
| 15 @end |
| 16 |
| 17 // This class implements a cache with a limited size. Once the cache reach its |
| 18 // size limit, it will start to evict items in a Least Recently Used order |
| 19 // (where the term "used" is determined in terms of query to the cache). |
| 20 @interface LRUCache : NSObject |
| 21 |
| 22 // The delegate of the LRUCache called when objects are evicted from the cache. |
| 23 @property(nonatomic, assign) id<LRUCacheDelegate> delegate; |
| 24 |
| 25 // The maximum amount of items that the cache can hold before starting to |
| 26 // evict. The value 0 is used to signify that the cache can hold an unlimited |
| 27 // amount of elements (i.e. never evicts). |
| 28 @property(nonatomic, readonly) NSUInteger maxCacheSize; |
| 29 |
| 30 // Use the initWithCacheSize: designated initializer. The is no good general |
| 31 // default value for the cache size. |
| 32 - (instancetype)init NS_UNAVAILABLE; |
| 33 |
| 34 // |maxCacheSize| value is used to specify the maximum amount of items that the |
| 35 // cache can hold before starting to evict items. |
| 36 - (instancetype)initWithCacheSize:(NSUInteger)maxCacheSize |
| 37 NS_DESIGNATED_INITIALIZER; |
| 38 |
| 39 // Query the cache for an item corresponding to the |key|. Returns nil if there |
| 40 // is no item corresponding to that key. |
| 41 - (id)objectForKey:(id<NSObject>)key; |
| 42 |
| 43 // Adds the pair |key|, |obj| to the cache. If the value of the maxCacheSize |
| 44 // property is non zero, the cache may evict an elements if the maximum cache |
| 45 // size is reached. If the |key| is already present in the cache, the value for |
| 46 // that key is replaced by |object|. For any evicted object and if the delegate |
| 47 // is |
| 48 // non nil, it will receive a call to the lruCacheWillEvictObject: selector. |
| 49 - (void)setObject:(id<NSObject>)object forKey:(NSObject*)key; |
| 50 |
| 51 // Remove the key, value pair corresponding to the given |key|. If the delegate |
| 52 // is non nil, it will receive a call to the lruCacheWillEvictObject: selector. |
| 53 - (void)removeObjectForKey:(id<NSObject>)key; |
| 54 |
| 55 // Remove all objects from the cache. For all evicted objects and if the |
| 56 // delegate is non nil, it will receive a call to the lruCacheWillEvictObject: |
| 57 // selector. |
| 58 - (void)removeAllObjects; |
| 59 |
| 60 // Returns the amount of items that the cache currently hold. |
| 61 - (NSUInteger)count; |
| 62 |
| 63 // Returns true if the cache is empty. |
| 64 - (BOOL)isEmpty; |
| 65 |
| 66 @end |
| 67 |
| 68 #endif // IOS_CHROME_BROWSER_SNAPSHOTS_LRU_CACHE_H_ |
| OLD | NEW |