Skip to content
This repository was archived by the owner on Apr 22, 2026. It is now read-only.

Commit dc1fa42

Browse files
committed
merge main
2 parents ea8a16a + 942dcf4 commit dc1fa42

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

Sources/SwiftMemcache/MemcachedConnection.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,62 @@ public actor MemcachedConnection {
576576
)
577577
}
578578
}
579+
580+
// MARK: - Increment a Value
581+
582+
/// Increment the value for an existing key in the Memcache server by a specified amount.
583+
///
584+
/// - Parameters:
585+
/// - key: The key for the value to increment.
586+
/// - amount: The `Int` amount to increment the value by. Must be larger than 0.
587+
/// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
588+
public func increment(_ key: String, amount: Int) async throws {
589+
// Ensure the amount is greater than 0
590+
precondition(amount > 0, "Amount to increment should be larger than 0")
591+
592+
switch self.state {
593+
case .initial(_, _, _, _),
594+
.running:
595+
596+
var flags = MemcachedFlags()
597+
flags.arithmeticMode = .increment(amount)
598+
599+
let command = MemcachedRequest.ArithmeticCommand(key: key, flags: flags)
600+
let request = MemcachedRequest.arithmetic(command)
601+
602+
_ = try await self.sendRequest(request)
603+
604+
case .finished:
605+
throw MemcachedConnectionError.connectionShutdown
606+
}
607+
}
608+
609+
// MARK: - Decrement a Value
610+
611+
/// Decrement the value for an existing key in the Memcache server by a specified amount.
612+
///
613+
/// - Parameters:
614+
/// - key: The key for the value to decrement.
615+
/// - amount: The `Int` amount to decrement the value by. Must be larger than 0.
616+
/// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
617+
public func decrement(_ key: String, amount: Int) async throws {
618+
// Ensure the amount is greater than 0
619+
precondition(amount > 0, "Amount to decrement should be larger than 0")
620+
621+
switch self.state {
622+
case .initial(_, _, _, _),
623+
.running:
624+
625+
var flags = MemcachedFlags()
626+
flags.arithmeticMode = .decrement(amount)
627+
628+
let command = MemcachedRequest.ArithmeticCommand(key: key, flags: flags)
629+
let request = MemcachedRequest.arithmetic(command)
630+
631+
_ = try await self.sendRequest(request)
632+
633+
case .finished:
634+
throw MemcachedConnectionError.connectionShutdown
635+
}
636+
}
579637
}

Sources/SwiftMemcache/MemcachedValue.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ extension MemcachedValue where Self: FixedWidthInteger {
4040
///
4141
/// - Parameter buffer: The ByteBuffer from which the value should be read.
4242
public static func readFromBuffer(_ buffer: inout ByteBuffer) -> Self? {
43-
guard let string = buffer.readString(length: buffer.readableBytes)?.trimmingCharacters(in: .whitespacesAndNewlines) else { return nil }
44-
return Self(string)
43+
return buffer.readIntegerFromASCII()
4544
}
4645
}
4746

0 commit comments

Comments
 (0)