@@ -424,4 +424,62 @@ public actor MemcachedConnection {
424
424
throw MemcachedConnectionError . connectionShutdown
425
425
}
426
426
}
427
+
428
+ // MARK: - Increment a Value
429
+
430
+ /// Increment the value for an existing key in the Memcache server by a specified amount.
431
+ ///
432
+ /// - Parameters:
433
+ /// - key: The key for the value to increment.
434
+ /// - amount: The `Int` amount to increment the value by. Must be larger than 0.
435
+ /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
436
+ public func increment( _ key: String , amount: Int ) async throws {
437
+ // Ensure the amount is greater than 0
438
+ precondition ( amount > 0 , " Amount to increment should be larger than 0 " )
439
+
440
+ switch self . state {
441
+ case . initial( _, _, _, _) ,
442
+ . running:
443
+
444
+ var flags = MemcachedFlags ( )
445
+ flags. arithmeticMode = . increment( amount)
446
+
447
+ let command = MemcachedRequest . ArithmeticCommand ( key: key, flags: flags)
448
+ let request = MemcachedRequest . arithmetic ( command)
449
+
450
+ _ = try await self . sendRequest ( request)
451
+
452
+ case . finished:
453
+ throw MemcachedConnectionError . connectionShutdown
454
+ }
455
+ }
456
+
457
+ // MARK: - Decrement a Value
458
+
459
+ /// Decrement the value for an existing key in the Memcache server by a specified amount.
460
+ ///
461
+ /// - Parameters:
462
+ /// - key: The key for the value to decrement.
463
+ /// - amount: The `Int` amount to decrement the value by. Must be larger than 0.
464
+ /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
465
+ public func decrement( _ key: String , amount: Int ) async throws {
466
+ // Ensure the amount is greater than 0
467
+ precondition ( amount > 0 , " Amount to decrement should be larger than 0 " )
468
+
469
+ switch self . state {
470
+ case . initial( _, _, _, _) ,
471
+ . running:
472
+
473
+ var flags = MemcachedFlags ( )
474
+ flags. arithmeticMode = . decrement( amount)
475
+
476
+ let command = MemcachedRequest . ArithmeticCommand ( key: key, flags: flags)
477
+ let request = MemcachedRequest . arithmetic ( command)
478
+
479
+ _ = try await self . sendRequest ( request)
480
+
481
+ case . finished:
482
+ throw MemcachedConnectionError . connectionShutdown
483
+ }
484
+ }
427
485
}
0 commit comments