-
Notifications
You must be signed in to change notification settings - Fork 1
/
CachedOptions.swift
74 lines (66 loc) 路 1.75 KB
/
CachedOptions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// CachedOptions.swift
// CacheX
//
// Created by Condy on 2023/3/30.
//
import Foundation
public struct CachedOptions: OptionSet, Hashable {
public let rawValue: UInt16
public init(rawValue: UInt16) {
self.rawValue = rawValue
}
/// Do not use any cache.
public static let none = CachedOptions(rawValue: 1 << 0)
/// Cache the data in memory.
public static let memory = CachedOptions(rawValue: 1 << 1)
/// Cache the data in disk.
public static let disk = CachedOptions(rawValue: 1 << 2)
/// Use memory and disk cache at the same time to read memory first.
public static let diskAndMemory: CachedOptions = [.memory, .disk]
/// All cache types.
public static let all: CachedOptions = [.memory, .disk]
}
extension CachedOptions {
func cacheNameds() -> [String] {
var nameds = [String]()
if contains(.memory) {
nameds.append(Memory.named)
}
if contains(.disk) {
nameds.append(Disk.named)
}
return nameds
}
func caches() -> [String: Cacheable] {
var caches = [String: Cacheable]()
if contains(.memory) {
caches[Memory.named] = Memory()
}
if contains(.disk) {
caches[Disk.named] = Disk()
}
return caches
}
}
@objc public enum OCCachedOptions: Int {
case none = 0
case memory
case disk
case diskAndMemory
case all
var options: CachedOptions {
switch self {
case .none:
return .none
case .memory:
return .memory
case .disk:
return .disk
case .diskAndMemory:
return .diskAndMemory
case .all:
return .all
}
}
}