Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
- dependency management tweaks
- tests
- readme update
  • Loading branch information
ivasilyeu committed Jul 21, 2024
1 parent f7bf43b commit af60b3f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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() {}

Expand All @@ -17,7 +21,3 @@ public final class DependencyFactory {

public private(set) lazy var networkCacheService: NetworkCacheServiceProtocol = NetworkCacheService()
}

// MARK: - NetworkCacheServiceFactoryProtocol

extension DependencyFactory: NetworkCacheServiceFactoryProtocol {}
9 changes: 2 additions & 7 deletions Tests/NetworkCacheServiceTests/NetworkCacheServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,8 +24,3 @@ final class NetworkCacheServiceTests: XCTestCase {
XCTAssertEqual(uncachedItem, item, "cached and uncached items must be equal")
}
}





0 comments on commit af60b3f

Please sign in to comment.