mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			101 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			101 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | // | ||
|  | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | @objc | ||
|  | public class AnyLRUCache: NSObject { | ||
|  | 
 | ||
|  |     private let backingCache: LRUCache<NSObject, NSObject> | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public init(maxSize: Int) { | ||
|  |         backingCache = LRUCache(maxSize: maxSize) | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public func get(key: NSObject) -> NSObject? { | ||
|  |         return self.backingCache.get(key: key) | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public func set(key: NSObject, value: NSObject) { | ||
|  |         self.backingCache.set(key: key, value: value) | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public func clear() { | ||
|  |         self.backingCache.clear() | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | // A simple LRU cache bounded by the number of entries. | ||
|  | public class LRUCache<KeyType: Hashable & Equatable, ValueType> { | ||
|  | 
 | ||
|  |     private var cacheMap: [KeyType: ValueType] = [:] | ||
|  |     private var cacheOrder: [KeyType] = [] | ||
|  |     private let maxSize: Int | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public init(maxSize: Int) { | ||
|  |         self.maxSize = maxSize | ||
|  | 
 | ||
|  |         NotificationCenter.default.addObserver(self, | ||
|  |                                                selector: #selector(didReceiveMemoryWarning), | ||
|  |                                                name: UIApplication.didReceiveMemoryWarningNotification, | ||
|  |                                                object: nil) | ||
|  |         NotificationCenter.default.addObserver(self, | ||
|  |                                                selector: #selector(didEnterBackground), | ||
|  |                                                name: NSNotification.Name.OWSApplicationDidEnterBackground, | ||
|  |                                                object: nil) | ||
|  |     } | ||
|  | 
 | ||
|  |     deinit { | ||
|  |         NotificationCenter.default.removeObserver(self) | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc func didEnterBackground() { | ||
|  |         clear() | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc func didReceiveMemoryWarning() { | ||
|  |         clear() | ||
|  |     } | ||
|  | 
 | ||
|  |     private func updateCacheOrder(key: KeyType) { | ||
|  |         cacheOrder = cacheOrder.filter { $0 != key } | ||
|  |         cacheOrder.append(key) | ||
|  |     } | ||
|  | 
 | ||
|  |     public func get(key: KeyType) -> ValueType? { | ||
|  |         guard let value = cacheMap[key] else { | ||
|  |             // Miss | ||
|  |             return nil | ||
|  |         } | ||
|  | 
 | ||
|  |         // Hit | ||
|  |         updateCacheOrder(key: key) | ||
|  | 
 | ||
|  |         return value | ||
|  |     } | ||
|  | 
 | ||
|  |     public func set(key: KeyType, value: ValueType) { | ||
|  |         cacheMap[key] = value | ||
|  | 
 | ||
|  |         updateCacheOrder(key: key) | ||
|  | 
 | ||
|  |         while cacheOrder.count > maxSize { | ||
|  |             guard let staleKey = cacheOrder.first else { | ||
|  |                 return | ||
|  |             } | ||
|  |             cacheOrder.removeFirst() | ||
|  |             cacheMap.removeValue(forKey: staleKey) | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public func clear() { | ||
|  |         cacheMap.removeAll() | ||
|  |         cacheOrder.removeAll() | ||
|  |     } | ||
|  | } |