diff --git a/README.md b/README.md index 1792780..6504bce 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,51 @@ -Simple URL load caching service that allows to directly cache responses without having to rely on predetermined URLSession caching strategies. -- expiration policies support -- disk and memory storage +`NetworkCacheService` is a simple URL load caching service that allows to directly cache responses without having to rely on predetermined URLSession caching strategies. + +##Features + +- Expiration policies support +- Disk and memory storage +- Full thread safety + +##Usage + +###Caching of network response models: + +```swift +URLSession.shared.dataTask(with: url) { [cacheService] data, response, error in + guard let response, let data else { + return + } + + do { + let networkModel = try JSONDecoder().decode(ResponseNetworkModel.self, from: data) + + cacheService.cacheItem(networkModel, with: response, for: url, policies: .nonExpirable) + } + catch { + } +}.resume() +``` + +###Reading of previously cached models: + +```swift +let modelOrNil = cacheService.readItem(of: ResponseNetworkModel.self, for: url) +``` + +## Integration + +### [Swift Package Manager](https://github.com/apple/swift-package-manager) + +Use Swift Package Manager and add dependency to `Package.swift` file. + +```swift + dependencies: [ + .package(url: "https://github.com/ivasilyeu/NetworkCacheService.git", .upToNextMajor(from: "1.0.0")) + ] +``` + +Alternatively, in Xcode select `File > Add Package Dependencies…` and add NetworkCacheService repository URL: + +``` +https://github.com/ivasilyeu/NetworkCacheService.git +``` diff --git a/Sources/NetworkCacheService/Dependencies/DependencyFactory.swift b/Tests/NetworkCacheServiceTests/MockDependencyFactory.swift similarity index 57% rename from Sources/NetworkCacheService/Dependencies/DependencyFactory.swift rename to Tests/NetworkCacheServiceTests/MockDependencyFactory.swift index d61bf2b..915ea60 100644 --- a/Sources/NetworkCacheService/Dependencies/DependencyFactory.swift +++ b/Tests/NetworkCacheServiceTests/MockDependencyFactory.swift @@ -1,12 +1,16 @@ // -// DependencyFactory.swift +// MockDependencyFactory.swift // -import Foundation +import NetworkCacheService -// MARK: - DependencyFactory +// MARK: - NetworkCacheServiceFactoryProtocol + +extension MockDependencyFactory: NetworkCacheServiceFactoryProtocol {} + +// MARK: - MockDependencyFactory -public final class DependencyFactory { +public final class MockDependencyFactory { public init() {} @@ -17,7 +21,3 @@ public final class DependencyFactory { public private(set) lazy var networkCacheService: NetworkCacheServiceProtocol = NetworkCacheService() } - -// MARK: - NetworkCacheServiceFactoryProtocol - -extension DependencyFactory: NetworkCacheServiceFactoryProtocol {} diff --git a/Tests/NetworkCacheServiceTests/NetworkCacheServiceTests.swift b/Tests/NetworkCacheServiceTests/NetworkCacheServiceTests.swift index 0c8c71b..74c4852 100644 --- a/Tests/NetworkCacheServiceTests/NetworkCacheServiceTests.swift +++ b/Tests/NetworkCacheServiceTests/NetworkCacheServiceTests.swift @@ -3,13 +3,13 @@ import XCTest final class NetworkCacheServiceTests: XCTestCase { - let dependencies: NetworkCacheServiceFactoryProtocol = DependencyFactory() + let dependencies: NetworkCacheServiceFactoryProtocol = MockDependencyFactory() lazy var cache = dependencies.networkCacheService func testThatStringCacheUncacheWorks() throws { let item = "test" - let url = URL(string: "https://google.com")! //URL(string: UUID().uuidString)! + let url = URL(string: "https://google.com")! let mockedResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)! cache.cacheItem(item, @@ -24,8 +24,3 @@ final class NetworkCacheServiceTests: XCTestCase { XCTAssertEqual(uncachedItem, item, "cached and uncached items must be equal") } } - - - - -