88
99import Foundation
1010
11- /// Temporary image cache.
12- public final class TemporaryImageCache {
13- // MARK: - Public Properties
11+ /// Image cache protocol .
12+ public protocol ImageCacheProtocol {
13+ subscript ( _ url : URL ) -> CPImage ? { get set }
1414
15- /// The singleton instance .
15+ /// Set cache limit .
1616 ///
17- /// - Returns: The singleton `TemporaryImageCache` instance.
18- public static let shared = TemporaryImageCache ( )
17+ /// - Parameters:
18+ /// - countLimit: The maximum number of objects the cache should hold.
19+ /// If `0`, there is no count limit. The default value is `0`.
20+ /// - totalCostLimit: The maximum total cost that the cache can hold before
21+ /// it starts evicting objects.
22+ /// When you add an object to the cache, you may pass in a specified cost for the object,
23+ /// such as the size in bytes of the object.
24+ /// If `0`, there is no total cost limit. The default value is `0`.
25+ func setCacheLimit( countLimit: Int , totalCostLimit: Int )
1926
27+ /// Empties the cache.
28+ func removeCache( )
29+ }
30+
31+ struct TemporaryImageCache : ImageCacheProtocol {
2032 // MARK: - Private Properties
2133
22- private lazy var cache : NSCache < NSURL , CPImage > = {
34+ private let cache : NSCache < NSURL , CPImage > = {
2335 let cache = NSCache < NSURL , CPImage > ( )
2436 return cache
2537 } ( )
@@ -35,29 +47,14 @@ public final class TemporaryImageCache {
3547 }
3648 }
3749
38- // MARK: - Private Initializers
39-
40- private init ( ) { }
41-
4250 // MARK: - Public Methods
4351
44- /// Set cache limit.
45- ///
46- /// - Parameters:
47- /// - countLimit: The maximum number of objects the cache should hold.
48- /// If `0`, there is no count limit. The default value is `0`.
49- /// - totalCostLimit: The maximum total cost that the cache can hold before
50- /// it starts evicting objects.
51- /// When you add an object to the cache, you may pass in a specified cost for the object,
52- /// such as the size in bytes of the object.
53- /// If `0`, there is no total cost limit. The default value is `0`.
54- public func setCacheLimit( countLimit: Int = 0 , totalCostLimit: Int = 0 ) {
52+ func setCacheLimit( countLimit: Int = 0 , totalCostLimit: Int = 0 ) {
5553 cache. countLimit = countLimit
5654 cache. totalCostLimit = totalCostLimit
5755 }
5856
59- /// Empties the cache.
60- public func removeCache( ) {
57+ func removeCache( ) {
6158 cache. removeAllObjects ( )
6259 }
6360}
0 commit comments