Skip to content

Commit 74c2751

Browse files
heckjadam-fowler
andauthored
documentation updates (#160)
* documentation updates - breaks out the getting started pieces into its own article, and adds detail on how to add the package to your project - a pass through the top-level types to make their descriptions more consisent, and full sentences. - loosely curate the new (public) geographical types into the current structure Signed-off-by: Joe Heck <[email protected]> * shifting based on feedback Signed-off-by: Joe Heck <[email protected]> * updating the RESPRenderable protocol descriptions Signed-off-by: Joe Heck <[email protected]> * grammar fix Signed-off-by: Joe Heck <[email protected]> * hiding the geo response types Signed-off-by: Joe Heck <[email protected]> * Update Sources/Valkey/RESP/RESPStringRenderable.swift Co-authored-by: Adam Fowler <[email protected]> Signed-off-by: Joseph Heck <[email protected]> * Update Sources/Valkey/RESP/RESPRenderable.swift Co-authored-by: Adam Fowler <[email protected]> Signed-off-by: Joseph Heck <[email protected]> * Update Sources/Valkey/Documentation.docc/getting-started.md Co-authored-by: Adam Fowler <[email protected]> Signed-off-by: Joseph Heck <[email protected]> * Update Sources/Valkey/RESP/RESPError.swift Co-authored-by: Adam Fowler <[email protected]> Signed-off-by: Joseph Heck <[email protected]> --------- Signed-off-by: Joe Heck <[email protected]> Signed-off-by: Joseph Heck <[email protected]> Co-authored-by: Adam Fowler <[email protected]>
1 parent 5106ebf commit 74c2751

19 files changed

+132
-58
lines changed

Sources/Valkey/Cluster/ValkeyNodeDiscovery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
/// Allows the cluster client to initially find at least one node in the cluster or find the
15+
/// A type that allows the cluster client to initially find at least one node in the cluster, or find the
1616
/// nodes again if connection to them has been lost.
1717
public protocol ValkeyNodeDiscovery: Sendable {
1818
/// A type that describes a single node in a valkey cluster

Sources/Valkey/Commands/Custom/GeoCustomCommands.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
/// A type that represents geographic coordinates.
16+
@_documentation(visibility: internal)
1517
public struct GeoCoordinates: RESPTokenDecodable, Sendable {
1618
public let longitude: Double
1719
public let latitude: Double
@@ -21,6 +23,8 @@ public struct GeoCoordinates: RESPTokenDecodable, Sendable {
2123
}
2224
}
2325

26+
/// A type that represents a coordinate value for a geographic location.
27+
@_documentation(visibility: internal)
2428
public typealias GEODISTResponse = Double?
2529
extension GEODIST {
2630
public typealias Response = GEODISTResponse

Sources/Valkey/Connection/ValkeyConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Network
2323
import NIOTransportServices
2424
#endif
2525

26-
/// Single connection to a Valkey database
26+
/// A single connection to a Valkey database.
2727
@available(valkeySwift 1.0, *)
2828
public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
2929
nonisolated public let unownedExecutor: UnownedSerialExecutor

Sources/Valkey/Connection/ValkeyConnectionProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
/// Protocol for a Valkey connection that can send a command and get a response
15+
/// A type that provides the ability to send a Valkey command and get a response.
1616
public protocol ValkeyClientProtocol {
1717
/// Send RESP command to Valkey connection
1818
/// - Parameter command: ValkeyCommand structure

Sources/Valkey/Connection/ValkeyServerAddress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import NIOCore
1616

17-
/// Server address to connect to
17+
/// A Valkey server address to connect to.
1818
public struct ValkeyServerAddress: Sendable, Equatable {
1919
enum _Internal: Equatable {
2020
case hostname(_ host: String, port: Int)

Sources/Valkey/Documentation.docc/Pipelining.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Pipelining
22

3-
Send multiple commands at once without waiting for the response of each command.
3+
Sending multiple commands at once without waiting for the response of each command.
44

55
## Overview
6+
=======
67

78
Valkey pipelining is a technique for improving performance by issuing multiple commands at once without waiting for the response to each individual command. Pipelining not only reduces the latency cost of waiting for the result of each command it also reduces the cost to the server as it reduces I/O costs. Multiple commands can be read with a single syscall, and multiple results are delivered with a single syscall.
89

@@ -48,7 +49,6 @@ async let asyncResult = connection.lpush("fooList", elements: ["bar"])
4849
let result = try await asyncResult
4950
```
5051

51-
Be careful when using a single connection across multiple Tasks though. The result of a command only becomes available when the server makes available the result of the command previously queued. Because of this, a command that either blocks the connection or takes a long time can affect the response time of commands that follow it.
52-
53-
You can find out more about pipelining of commands in the [Valkey documentation](https://valkey.io/topics/pipelining/).
54-
52+
Be careful when using a single connection across multiple Tasks though.
53+
The result of a command will only become available when the result of any previous command queued has been made available.
54+
So a command that either blocks the connection or takes a long time could affect the response time of commands that follow it.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Getting started using Valkey
2+
3+
Add Valkey to your project, manage the connections, and send commands.
4+
5+
## Overview
6+
7+
### Adding Valkey as a dependency
8+
9+
Add Valkey-Swift as a dependency to your project and targets that use it.
10+
11+
You can use the `add-dependency` command:
12+
13+
```bash
14+
swift package add-dependency \
15+
https://github.com/valkey-io/valkey-swift --branch main
16+
```
17+
<!-- switch above to `--from: 0.1.0` after tagging an initial release -->
18+
19+
or edit Package.swift directly:
20+
```swift
21+
dependencies: [
22+
.package(url: "https://github.com/valkey-io/valkey-swift",
23+
branch: "main"),
24+
]
25+
```
26+
<!-- switch above to `from: "0.1.0"` after tagging an initial release -->
27+
28+
And for the relevant target or targets.
29+
The following example shows how to add to Valkey as a dependency to the target `MyApp`:
30+
31+
```bash
32+
swift package add-target-dependency \
33+
Valkey MyApp --package valkey-swift
34+
```
35+
36+
You can also edit the dependencies for that target directly in Package.swift:
37+
```swift
38+
dependencies: [
39+
.product(name: "Valkey", package: "valkey-swift"),
40+
]
41+
```
42+
43+
> Note: If you are building an executable for macOS, Valkey has a minimum platform dependency you need to accomodate.
44+
> For example, you may want to add a minimum platform requirement to your project:
45+
>
46+
> ```swift
47+
> platforms: [.macOS(.v15)],
48+
> ```
49+
50+
Import Valkey in the swift files to use it:
51+
52+
```swift
53+
import Valkey
54+
```
55+
56+
### Enabling connections to a Valkey server
57+
58+
``ValkeyClient`` and ``ValkeyClusterClient`` use a connection pool that requires a background root task to run all the maintenance work required to establish connections and maintain the cluster state.
59+
You can either run them using a Task group, for example:
60+
61+
```swift
62+
let valkeyClient = ValkeyClient(.hostname("localhost", port: 6379), logger: logger)
63+
try await withThrowingTaskgroup(of: Void.self) { group in
64+
group.addTask {
65+
// run connection pool in the background
66+
try await valkeyClient.run()
67+
}
68+
// use valkey client
69+
}
70+
```
71+
72+
Or you can use [swift-service-lifecycle](https://github.com/swift-server/swift-service-lifecycle) to manage the connection manager.
73+
74+
```swift
75+
let valkeyClient = ValkeyClient(.hostname("localhost", port: 6379), logger: logger)
76+
77+
let services: [Service] = [valkeyClient, webserver, other-service]
78+
let serviceGroup = ServiceGroup(
79+
services: services,
80+
gracefulShutdownSignals: [.sigint],
81+
cancellationSignals: [.sigterm],
82+
logger: logger
83+
)
84+
try await serviceGroup.run()
85+
```
86+
87+
### Sending commands
88+
89+
Once you have your connection pool up and running the client is ready to use, you can call commands on the client.
90+
Valkey-client uses a connection from the connection pool for each call:
91+
92+
```swift
93+
try await valkeyClient.set(key: "foo", value: "bar")
94+
let value = try await valkeyClient.get(key: "foo")
95+
```
96+
97+
You can ask for a single connection and run multiple commands using it:
98+
99+
```swift
100+
try await valkeyClient.withConnection { connection in
101+
try await connection.set(key: "foo", value: "bar")
102+
let value = try await connection.get(key: "foo")
103+
}
104+
```

Sources/Valkey/Documentation.docc/index.md

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,11 @@ A Swift client library for Valkey.
66

77
Valkey-swift is a swift based client for Valkey, the high-performance key/value datastore. It supports all the Valkey commands, pipelining, transactions, subscriptions and Valkey clusters.
88

9-
### Setup
10-
11-
``ValkeyClient`` and ``ValkeyClusterClient`` use a connection pool that requires a background root task to run all the maintenance work required to establish connections and maintain the cluster state. You can either run them using a Task group
12-
13-
```swift
14-
let valkeyClient = ValkeyClient(.hostname("localhost", port: 6379), logger: logger)
15-
try await withThrowingTaskgroup(of: Void.self) { group in
16-
group.addTask {
17-
// run connection pool in the background
18-
try await valkeyClient.run()
19-
}
20-
// use valkey client
21-
}
22-
```
23-
24-
Or you can use [swift-service-lifecycle](https://github.com/swift-server/swift-service-lifecycle) to manage the connection manager.
25-
26-
### Sending commands
27-
28-
Once you have your connection pool up and running the client is ready to use. Commands can be called straight from the client. Each call will ask for a connection from the connection pool
29-
30-
```swift
31-
try await valkeyClient.set(key: "foo", value: "bar")
32-
let value = try await valkeyClient.get(key: "foo")
33-
```
34-
35-
Or you can ask for a single connection and run multiple commands using that one connection
36-
```swift
37-
try await valkeyClient.withConnection { connection in
38-
try await connection.set(key: "foo", value: "bar")
39-
let value = try await connection.get(key: "foo")
40-
}
41-
```
42-
439
## Topics
4410

4511
### Articles
4612

13+
- <doc:getting-started>
4714
- <doc:Pipelining>
4815
- <doc:Pubsub>
4916
- <doc:Transactions>

Sources/Valkey/RESP/RESPError.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
import NIOCore
1616

17-
/// This error is thrown if a RESP3 package could not be decoded.
17+
/// An error that Valkey client throws if a RESP3 packet can't be decoded.
1818
///
1919
/// If you see this error, there a two potential reasons this might happen:
2020
///
21-
/// 1. The Swift RESP3 implementation is wrong
22-
/// 2. You are contacting an untrusted backend
21+
/// 1. The Swift RESP3 implementation is wrong.
22+
/// 2. You are contacting an untrusted backend.
2323
///
2424
public struct RESPParsingError: Error {
2525
/// The error code associated with an error parsing a response.

Sources/Valkey/RESP/RESPRenderable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import NIOCore
1616

17-
/// A type that can be rendered into a RESP buffer.
17+
/// A type that the command encoder can render as part of a Valkey command.
1818
public protocol RESPRenderable {
1919
var respEntries: Int { get }
2020

0 commit comments

Comments
 (0)