|
12 | 12 | //
|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
| 15 | +import Logging |
15 | 16 | import NIOCore
|
16 | 17 | import NIOPosix
|
| 18 | +import ServiceLifecycle |
17 | 19 | import SwiftMemcache
|
18 | 20 |
|
19 | 21 | @main
|
20 | 22 | struct Program {
|
21 |
| - // Create an event loop group with a single thread |
22 |
| - static let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 23 | + // Use the shared singleton instance of MultiThreadedEventLoopGroup |
| 24 | + static let eventLoopGroup = MultiThreadedEventLoopGroup.singleton |
| 25 | + // Initialize the logger |
| 26 | + static let logger = Logger(label: "memcache") |
23 | 27 |
|
24 | 28 | static func main() async throws {
|
25 |
| - // Instantiate a new MemcachedConnection actor with host, port, and event loop group |
26 |
| - let memcachedConnection = MemcachedConnection(host: "127.0.0.1", port: 11211, eventLoopGroup: eventLoopGroup) |
| 29 | + // Instantiate a new MemcacheConnection actor with host, port, and event loop group |
| 30 | + let memcacheConnection = MemcachedConnection(host: "127.0.0.1", port: 11211, eventLoopGroup: eventLoopGroup) |
| 31 | + |
| 32 | + // Initialize the service group |
| 33 | + let serviceGroup = ServiceGroup(services: [memcacheConnection], logger: self.logger) |
27 | 34 |
|
28 | 35 | try await withThrowingTaskGroup(of: Void.self) { group in
|
29 | 36 | // Add the connection actor's run function to the task group
|
30 | 37 | // This opens the connection and handles requests until the task is cancelled or the connection is closed
|
31 |
| - group.addTask { try await memcachedConnection.run() } |
| 38 | + group.addTask { try await serviceGroup.run() } |
32 | 39 |
|
33 | 40 | // Set a value for a key.
|
34 | 41 | let setValue = "bar"
|
35 |
| - try await memcachedConnection.set("foo", value: setValue) |
| 42 | + try await memcacheConnection.set("foo", value: setValue) |
36 | 43 |
|
37 | 44 | // Get the value for a key.
|
38 | 45 | // Specify the expected type for the value returned from Memcache.
|
39 |
| - let getValue: String? = try await memcachedConnection.get("foo") |
| 46 | + let getValue: String? = try await memcacheConnection.get("foo") |
40 | 47 |
|
41 | 48 | // Assert that the get operation was successful by comparing the value set and the value returned from Memcache.
|
42 | 49 | // If they are not equal, this will throw an error.
|
|
0 commit comments