Skip to content

Commit 14bb69e

Browse files
authored
Refactoring SwiftMemcache (#38)
* init clean up * lifecycle in seperate PR * remove Service * Update README.md * closes #39 * removed use of Foundation * Update README.md * example rename * not needed * not needed * not needed * README update
1 parent ed50014 commit 14bb69e

21 files changed

+411
-349
lines changed

Package.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ let package = Package(
2525
],
2626
products: [
2727
.library(
28-
name: "SwiftMemcache",
29-
targets: ["SwiftMemcache"]
28+
name: "Memcache",
29+
targets: ["Memcache"]
3030
),
3131
],
3232
dependencies: [
@@ -36,7 +36,7 @@ let package = Package(
3636
],
3737
targets: [
3838
.target(
39-
name: "SwiftMemcache",
39+
name: "Memcache",
4040
dependencies: [
4141
.product(name: "NIOCore", package: "swift-nio"),
4242
.product(name: "NIOPosix", package: "swift-nio"),
@@ -47,12 +47,12 @@ let package = Package(
4747
),
4848
.testTarget(
4949
name: "SwiftMemcacheTests",
50-
dependencies: ["SwiftMemcache"]
50+
dependencies: ["Memcache"]
5151
),
5252
.executableTarget(
53-
name: "swift-memcache-gsoc-example",
53+
name: "MemcacheExample",
5454
dependencies: [
55-
.target(name: "SwiftMemcache"),
55+
.target(name: "Memcache"),
5656
]
5757
),
5858
]

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,61 @@
33
SwiftMemcache is a Swift Package in development that provides a convenient way to communicate with [Memcached](https://github.com/memcached/memcached) servers.
44

55
## Getting Started
6+
7+
## Overview
8+
9+
### Memcache Connection API
10+
11+
Our `MemcacheConnection` allows for communicate with a Memcached server. This actor takes care of establishing a connection, creating a request stream and handling asynchronous execution of commands.
12+
13+
Here's an example of how you can use `MemcachedConnection` in a program.
14+
15+
```swift
16+
@main
17+
struct Program {
18+
// Use the shared singleton instance of MultiThreadedEventLoopGroup
19+
static let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
20+
// Initialize the logger
21+
static let logger = Logger(label: "memcache")
22+
23+
static func main() async throws {
24+
// Instantiate a new MemcacheConnection actor with host, port, and event loop group
25+
let memcacheConnection = MemcacheConnection(host: "127.0.0.1", port: 11211, eventLoopGroup: eventLoopGroup)
26+
27+
// Initialize the service group
28+
let serviceGroup = ServiceGroup(services: [memcacheConnection], logger: self.logger)
29+
30+
try await withThrowingTaskGroup(of: Void.self) { group in
31+
// Add the connection actor's run function to the task group
32+
// This opens the connection and handles requests until the task is cancelled or the connection is closed
33+
group.addTask { try await serviceGroup.run() }
34+
35+
// Set a value for a key.
36+
let setValue = "bar"
37+
try await memcacheConnection.set("foo", value: setValue)
38+
39+
// Get the value for a key.
40+
// Specify the expected type for the value returned from Memcache.
41+
let getValue: String? = try await memcacheConnection.get("foo")
42+
43+
// Assert that the get operation was successful by comparing the value set and the value returned from Memcache.
44+
// If they are not equal, this will throw an error.
45+
assert(getValue == setValue, "Value retrieved from Memcache does not match the set value")
46+
47+
// Cancel all tasks in the task group.
48+
// This also results in the connection to Memcache being closed.
49+
group.cancelAll()
50+
}
51+
}
52+
}
53+
```
54+
55+
## Contributing
56+
57+
### Docker
58+
59+
We provide a Docker environment for this package. This will automatically start a local Memcached server and run the package tests.
60+
61+
```bash
62+
docker-compose -f docker/docker-compose.yaml run test
63+
```

Sources/SwiftMemcache/Extensions/ByteBuffer+SwiftMemcache.swift renamed to Sources/Memcache/Extensions/ByteBuffer+Memcache.swift

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Foundation
15+
#if os(Linux)
16+
import Glibc
17+
#else
18+
import Darwin
19+
#endif
1620
import NIOCore
1721

1822
extension ByteBuffer {
@@ -44,15 +48,15 @@ extension ByteBuffer {
4448
}
4549

4650
extension ByteBuffer {
47-
/// Serialize and writes MemcachedFlags to the ByteBuffer.
51+
/// Serialize and writes MemcacheFlags to the ByteBuffer.
4852
///
49-
/// This method runs a loop over the flags contained in a MemcachedFlags instance.
53+
/// This method runs a loop over the flags contained in a MemcacheFlags instance.
5054
/// For each flag that is set to true, its corresponding byte value,
5155
/// followed by a whitespace character, is added to the ByteBuffer.
5256
///
5357
/// - parameters:
54-
/// - flags: An instance of MemcachedFlags that holds the flags intended to be serialized and written to the ByteBuffer.
55-
mutating func writeMemcachedFlags(flags: MemcachedFlags) {
58+
/// - flags: An instance of MemcacheFlags that holds the flags intended to be serialized and written to the ByteBuffer.
59+
mutating func writeMemcacheFlags(flags: MemcacheFlags) {
5660
// Ensure that both storageMode and arithmeticMode aren't set at the same time.
5761
precondition(!(flags.storageMode != nil && flags.arithmeticMode != nil), "Cannot specify both a storage and arithmetic mode.")
5862

@@ -127,9 +131,9 @@ extension ByteBuffer {
127131
extension ByteBuffer {
128132
/// Parses flags from this `ByteBuffer`, advancing the reader index accordingly.
129133
///
130-
/// - returns: A `MemcachedFlags` instance populated with the flags read from the buffer.
131-
mutating func readMemcachedFlags() -> MemcachedFlags {
132-
let flags = MemcachedFlags()
134+
/// - returns: A `MemcacheFlags` instance populated with the flags read from the buffer.
135+
mutating func readMemcacheFlags() -> MemcacheFlags {
136+
let flags = MemcacheFlags()
133137
while let nextByte = self.getInteger(at: self.readerIndex, as: UInt8.self) {
134138
switch nextByte {
135139
case UInt8.whitespace:

0 commit comments

Comments
 (0)