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