Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Hummingbird.docc/Articles/PersistentData.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ let persist = MemoryPersistDriver()
```
If you use the memory based driver the key/value pairs you store will be lost if your server goes down, also you will not be able to share values between server processes.

### Redis
### Valkey/Redis

You can use Redis to store the `persists` key/value pairs with ``HummingbirdRedis/RedisPersistDriver`` from the `HummingbirdRedis` library. You would setup `persist` to use Redis as follows.
You can use Valkey/Redis to store the `persists` key/value pairs with ``HummingbirdValkey/ValkeyPersistDriver`` from the `HummingbirdValkey` library. You would setup `persist` to use Valkey as follows.
```swift
let redis = RedisConnectionPoolService(
.init(hostname: redisHostname, port: 6379),
logger: Logger(label: "Redis")
let valkeyClient = ValkeyClient(
.hostname(valkeyHostname, port: 6379),
logger: Logger(label: "Valkey")
)
let persist = RedisPersistDriver(redisConnectionPoolService: redis)
let persist = ValkeyPersistDriver(client: valkeyClient)
```

### Fluent
Expand All @@ -93,5 +93,5 @@ if shouldMigrate {
- ``PersistDriver``
- ``MemoryPersistDriver``
- ``HummingbirdFluent/FluentPersistDriver``
- ``HummingbirdRedis/RedisPersistDriver``
- ``HummingbirdValkey/ValkeyPersistDriver``
- ``HummingbirdPostgres/PostgresPersistDriver``
2 changes: 1 addition & 1 deletion Hummingbird.docc/HummingbirdAuth/Sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Sessions allow you to persist state eg user authentication status between multip

The ``HummingbirdAuth/SessionMiddleware`` is used to extract and save session state from the RequestContext. To use it, your `RequestContext` must conform to ``HummingbirdAuth/SessionRequestContext``. Adding the `SessionMiddleware` to your middleware stack will mean any middleware or routes after will have read/write access to session state via the member ``HummingbirdAuth/SessionRequestContext/sessions``.

The `SessionMiddleware` needs a persist key value store to save its state. You can find out more about the persist framework here <doc:PersistentData>. In the example below we are using an in memory key value store, but ``HummingbirdFluent/FluentPersistDriver`` and ``HummingbirdRedis/RedisPersistDriver`` provide solutions that stores the session data in a database or redis database respectively.
The `SessionMiddleware` needs a persist key value store to save its state. You can find out more about the persist framework here <doc:PersistentData>. In the example below we are using an in memory key value store, but ``HummingbirdFluent/FluentPersistDriver`` and ``HummingbirdValkey/ValkeyPersistDriver`` provide solutions that stores the session data in a database or valkey/redis database respectively.

```swift
router.add(
Expand Down
71 changes: 0 additions & 71 deletions Hummingbird.docc/HummingbirdRedis/HummingbirdRedis.md

This file was deleted.

24 changes: 0 additions & 24 deletions Hummingbird.docc/HummingbirdRedis/RedisConnectionPoolService.md

This file was deleted.

45 changes: 45 additions & 0 deletions Hummingbird.docc/HummingbirdValkey/HummingbirdValkey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ``HummingbirdValkey``

@Metadata {
@PageImage(purpose: icon, source: "logo")
}

Add Valkey/Redis support to Hummingbird server with valkey-swift.

## Overview

HummingbirdValkey provides a driver for the Hummingbird persist framework to store key, value pairs between requests.

```swift
let valkeyClient = ValkeyClient(
.hostname(Self.valkeyHostname, port: 6379),
logger: Logger(label: "Valkey")
)
let persist = ValkeyPersistDriver(client: valkeyClient)
let router = Router()
// return value from valkey database
router.get("{id}") { request, context -> String? in
let id = try context.parameters.require("id")
try await persist.get(key: id, as: String.self)
}
// set value in valkey database
router.put("{id}") { request, context -> String? in
let id = try context.parameters.require("id")
let value = try request.uri.queryParameters.require("value")
try await persist.set(key: id, value: value)
}
var app = Application(router: router)
// add Valkey client as service to manage its lifecycle
app.addServices(valkeyClient)
try await app.runService()
```

## Topics

### Storage

- ``ValkeyPersistDriver``

## See Also

- ``JobsRedis``
34 changes: 1 addition & 33 deletions Hummingbird.docc/JobsRedis/JobsRedis.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,7 @@ Hummingbird Jobs Queue driver using [RediStack](https://github.com/swift-server/

### Setup

Currently `RediStack` is not setup to use `ServiceLifecycle`. So to ensure clean shutdown of `RediStack` you either need to use the ``HummingbirdRedis/RedisConnectionPoolService`` that is part of ``HummingbirdRedis`` or write your own `Service` type that will manage the shutdown of a `RedisConnectionPool`.

#### Using HummingbirdRedis

If you choose to use `HummingbirdRedis` you can setup a JobQueue using `RediStack` as follows

```swift
let redisService = try RedisConnectionPoolService(
.init(hostname: redisHost, port: 6379),
logger: logger
)
let jobQueue = JobQueue(
.redis(
redisService.pool,
configuration: .init(
queueKey: "MyJobQueue",
pollTime: .milliseconds(50)
)
),
logger: logger
)
let serviceGroup = ServiceGroup(
configuration: .init(
services: [redisService, jobQueue],
gracefulShutdownSignals: [.sigterm, .sigint],
logger: logger
)
)
try await serviceGroup.run()
```
The Redis job queue configuration includes two values.
- `queueKey`: Prefix to all the Redis keys used to store queues.
- `pollTime`: This is the amount of time between the last time the queue was empty and the next time the driver starts looking for pending jobs.
Currently `RediStack` is not setup to use `ServiceLifecycle`. So to ensure clean shutdown of `RediStack` you need to create your own `Service` type that will manage the shutdown of a `RedisConnectionPool`.

#### Write RedisConnectionPool Service

Expand Down
2 changes: 1 addition & 1 deletion Hummingbird.docc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Below is a list of guides and tutorials to help you get started with building yo
- ``/HummingbirdFluent``
- ``/HummingbirdLambda``
- ``/HummingbirdPostgres``
- ``/HummingbirdRedis``
- ``/HummingbirdValkey``
- ``/HummingbirdWebSocket``
- ``/Jobs``
- ``/Mustache``
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-postgres.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/postgres-migrations.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/hummingbird-redis.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-valkey.git", from: "0.1.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.2.0"),
.package(url: "https://github.com/hummingbird-project/swift-websocket.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Expand Down Expand Up @@ -54,7 +54,7 @@ let package = Package(
.product(name: "Mustache", package: "swift-mustache"),
.product(name: "HummingbirdPostgres", package: "hummingbird-postgres"),
.product(name: "PostgresMigrations", package: "postgres-migrations"),
.product(name: "HummingbirdRedis", package: "hummingbird-redis"),
.product(name: "HummingbirdValkey", package: "hummingbird-valkey"),
.product(name: "HummingbirdWebSocket", package: "hummingbird-websocket"),
.product(name: "WSClient", package: "swift-websocket"),
.product(name: "WSCompression", package: "swift-websocket"),
Expand Down