Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created June 15, 2018 08:28
Show Gist options
  • Save onmyway133/5911a368039551870d14ec9eee2487d9 to your computer and use it in GitHub Desktop.
Save onmyway133/5911a368039551870d14ec9eee2487d9 to your computer and use it in GitHub Desktop.
/// Manage storage. Use memory storage if specified.
/// Synchronous by default. Use `async` for asynchronous operations.
public class Storage {
/// Used for sync operations
fileprivate let sync: StorageAware
/// Storage used internally by both sync and async storages
private let interalStorage: StorageAware
/// Initialize storage with configuration options.
///
/// - Parameters:
/// - diskConfig: Configuration for disk storage
/// - memoryConfig: Optional. Pass confi if you want memory cache
/// - Throws: Throw StorageError if any.
public required init(diskConfig: DiskConfig, memoryConfig: MemoryConfig? = nil) throws {
// Disk or Hybrid
let storage: StorageAware
let disk = try DiskStorage(config: diskConfig)
if let memoryConfig = memoryConfig {
let memory = MemoryStorage(config: memoryConfig)
storage = HybridStorage(memoryStorage: memory, diskStorage: disk)
} else {
storage = disk
}
// Wrapper
self.interalStorage = TypeWrapperStorage(storage: storage)
// Sync
self.sync = SyncStorage(storage: interalStorage,
serialQueue: DispatchQueue(label: "Cache.SyncStorage.SerialQueue"))
}
/// Used for async operations
public lazy var async: AsyncStorage = AsyncStorage(storage: self.interalStorage,
serialQueue: DispatchQueue(label: "Cache.AsyncStorage.SerialQueue"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment