@@ -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}
0 commit comments