Skip to content

Commit 3b53dfe

Browse files
authored
Merge pull request #165 from tettoffensive/master
Add Package.swift for server side swift support
2 parents 847da7e + ee6135c commit 3b53dfe

File tree

13 files changed

+72
-19
lines changed

13 files changed

+72
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ Pods
3131

3232
# Carthage
3333
Carthage
34+
/.build

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0
1+
4.0.2

Package.resolved

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "Cache",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "Cache",
12+
targets: ["Cache"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
],
17+
targets: [
18+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
19+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
20+
.target(
21+
name: "Cache",
22+
path: "Source/Shared",
23+
exclude: ["Library/ImageWrapper.swift"]), // relative to the target path
24+
.testTarget(
25+
name: "CacheTests",
26+
dependencies: ["Cache"],
27+
path: "Tests"),
28+
]
29+
)

Source/Shared/Configuration/DiskConfig.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public struct DiskConfig {
1010
public let maxSize: UInt
1111
/// A folder to store the disk cache contents. Defaults to a prefixed directory in Caches if nil
1212
public let directory: URL?
13+
#if os(iOS) || os(tvOS)
1314
/// Data protection is used to store files in an encrypted format on disk and to decrypt them on demand.
1415
/// Support only on iOS and tvOS.
1516
public let protectionType: FileProtectionType?
@@ -23,4 +24,13 @@ public struct DiskConfig {
2324
self.directory = directory
2425
self.protectionType = protectionType
2526
}
27+
#else
28+
public init(name: String, expiry: Expiry = .never,
29+
maxSize: UInt = 0, directory: URL? = nil) {
30+
self.name = name
31+
self.expiry = expiry
32+
self.maxSize = maxSize
33+
self.directory = directory
34+
}
35+
#endif
2636
}

Source/Shared/Storage/AsyncStorage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Dispatch
23

34
/// Manipulate storage in a "all async" manner.
45
/// The completion closure will be called when operation completes.

Source/Shared/Storage/AsyncStorageAware.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Dispatch
23

34
/// A protocol used for saving and loading from storage in async manner.
45
public protocol AsyncStorageAware: class {

Source/Shared/Storage/DiskStorage.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension DiskStorage: StorageAware {
6969
let expiry = expiry ?? config.expiry
7070
let data = try DataSerializer.serialize(object: object)
7171
let filePath = makeFilePath(for: key)
72-
fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
72+
_ = fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
7373
try fileManager.setAttributes([.modificationDate: expiry.date], ofItemAtPath: filePath)
7474
}
7575

@@ -104,18 +104,18 @@ extension DiskStorage: StorageAware {
104104
}
105105

106106
for url in urlArray {
107-
let resourceValues = try (url as NSURL).resourceValues(forKeys: resourceKeys)
108-
guard (resourceValues[.isDirectoryKey] as? NSNumber)?.boolValue == false else {
107+
let resourceValues = try url.resourceValues(forKeys: Set(resourceKeys))
108+
guard resourceValues.isDirectory != true else {
109109
continue
110110
}
111111

112-
if let expiryDate = resourceValues[.contentModificationDateKey] as? Date, expiryDate.inThePast {
112+
if let expiryDate = resourceValues.contentModificationDate, expiryDate.inThePast {
113113
filesToDelete.append(url)
114114
continue
115115
}
116116

117-
if let fileSize = resourceValues[.totalFileAllocatedSizeKey] as? NSNumber {
118-
totalSize += fileSize.uintValue
117+
if let fileSize = resourceValues.totalFileAllocatedSize {
118+
totalSize += UInt(fileSize)
119119
resourceObjects.append((url: url, resourceValues: resourceValues))
120120
}
121121
}
@@ -135,12 +135,12 @@ extension DiskStorage {
135135
Sets attributes on the disk cache folder.
136136
- Parameter attributes: Directory attributes
137137
*/
138-
func setDirectoryAttributes(_ attributes: [FileAttributeKey : Any]) throws {
138+
func setDirectoryAttributes(_ attributes: [FileAttributeKey: Any]) throws {
139139
try fileManager.setAttributes(attributes, ofItemAtPath: path)
140140
}
141141
}
142142

143-
typealias ResourceObject = (url: Foundation.URL, resourceValues: [AnyHashable: Any])
143+
typealias ResourceObject = (url: Foundation.URL, resourceValues: URLResourceValues)
144144

145145
extension DiskStorage {
146146
/**
@@ -166,7 +166,7 @@ extension DiskStorage {
166166
var size: UInt64 = 0
167167
let contents = try fileManager.contentsOfDirectory(atPath: path)
168168
for pathComponent in contents {
169-
let filePath = (path as NSString).appendingPathComponent(pathComponent)
169+
let filePath = NSString(string: path).appendingPathComponent(pathComponent)
170170
let attributes = try fileManager.attributesOfItem(atPath: filePath)
171171
if let fileSize = attributes[.size] as? UInt64 {
172172
size += fileSize
@@ -198,9 +198,8 @@ extension DiskStorage {
198198
let targetSize = config.maxSize / 2
199199

200200
let sortedFiles = objects.sorted {
201-
let key = URLResourceKey.contentModificationDateKey
202-
if let time1 = ($0.resourceValues[key] as? Date)?.timeIntervalSinceReferenceDate,
203-
let time2 = ($1.resourceValues[key] as? Date)?.timeIntervalSinceReferenceDate {
201+
if let time1 = $0.resourceValues.contentModificationDate?.timeIntervalSinceReferenceDate,
202+
let time2 = $1.resourceValues.contentModificationDate?.timeIntervalSinceReferenceDate {
204203
return time1 > time2
205204
} else {
206205
return false
@@ -209,8 +208,8 @@ extension DiskStorage {
209208

210209
for file in sortedFiles {
211210
try fileManager.removeItem(at: file.url)
212-
if let fileSize = file.resourceValues[URLResourceKey.totalFileAllocatedSizeKey] as? NSNumber {
213-
totalSize -= fileSize.uintValue
211+
if let fileSize = file.resourceValues.totalFileAllocatedSize {
212+
totalSize -= UInt(fileSize)
214213
}
215214
if totalSize < targetSize {
216215
break

Source/Shared/Storage/MemoryStorage.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class MemoryStorage {
2020

2121
extension MemoryStorage: StorageAware {
2222
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
23-
guard let capsule = cache.object(forKey: key as NSString) else {
23+
guard let capsule = cache.object(forKey: NSString(string: key)) else {
2424
throw StorageError.notFound
2525
}
2626

@@ -32,13 +32,13 @@ extension MemoryStorage: StorageAware {
3232
}
3333

3434
func removeObject(forKey key: String) {
35-
cache.removeObject(forKey: key as NSString)
35+
cache.removeObject(forKey: NSString(string: key))
3636
keys.remove(key)
3737
}
3838

3939
func setObject<T: Codable>(_ object: T, forKey key: String, expiry: Expiry? = nil) {
4040
let capsule = MemoryCapsule(value: object, expiry: expiry ?? config.expiry)
41-
cache.setObject(capsule, forKey: key as NSString)
41+
cache.setObject(capsule, forKey: NSString(string: key))
4242
keys.insert(key)
4343
}
4444

@@ -61,7 +61,7 @@ extension MemoryStorage {
6161
- Parameter key: Unique key to identify the object in the cache
6262
*/
6363
func removeObjectIfExpired(forKey key: String) {
64-
if let capsule = cache.object(forKey: key as NSString), capsule.expiry.isExpired {
64+
if let capsule = cache.object(forKey: NSString(string: key)), capsule.expiry.isExpired {
6565
removeObject(forKey: key)
6666
}
6767
}

Source/Shared/Storage/Storage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Dispatch
23

34
/// Manage storage. Use memory storage if specified.
45
/// Synchronous by default. Use `async` for asynchronous operations.

0 commit comments

Comments
 (0)