-
Notifications
You must be signed in to change notification settings - Fork 6
Implement an Arithmetic Request supporting increment and decrement operations #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement an Arithmetic Request supporting increment and decrement operations #29
Conversation
@@ -101,6 +101,22 @@ extension ByteBuffer { | |||
self.writeInteger(UInt8.R) | |||
} | |||
} | |||
|
|||
if let arithmeticMode = flags.arithmeticMode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can't specify a storage mode and arithmetic mode. Can we add a precondition somewhere to check for this
/// - key: The key for the value to increment. | ||
/// - amount: The `UInt64` amount to increment the value by. | ||
/// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down. | ||
public func increment(_ key: String, amount: UInt64) async throws { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use Int here and precondition that it is larger than 0
/// - key: The key for the value to decrement. | ||
/// - amount: The `UInt64` amount to decrement the value by. | ||
/// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down. | ||
public func decrement(_ key: String, amount: UInt64) async throws { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here and adjust the doc comment as well please
/// Flag 'D' for the 'ma' (meta arithmetic) command. | ||
/// | ||
/// Represents the delta to apply to the 'ma' command. The default value is 1. | ||
var arithmeticDelta: UInt64? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this an associated value of the arithmetic mode enum please
@@ -33,14 +33,15 @@ extension MemcachedValue where Self: FixedWidthInteger { | |||
/// | |||
/// - Parameter buffer: The ByteBuffer to which the integer should be written. | |||
public func writeToBuffer(_ buffer: inout ByteBuffer) { | |||
buffer.writeInteger(self) | |||
buffer.writeString(String(self)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems wrong. If you want to store an integer we shouldn't convert it to a string. Is this the problem you encountered where you set a key and then want to increment it but the type in memcache was wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, for example when I called set with a value of say 100 the request that was being written looked like ms decrement 8 ...
when it should look something like ms decrement 3 ...
when I went to increment the value I would get this error CLIENT_ERROR cannot increment or decrement non-numeric value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also make use of writeIntegerAsASCII
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the root problem here is that memcache is operating on the values interpreted as ASCII and not the raw byte values right? So decrement expects that the value for a key is an ASCII string representing an integer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is correct, since the protocol is text based its wanting to receive the values as ASCII strings. we can fall back on making use of writeIntegerAsASCII
and then implement one of the possible solutions we outlined here #9 to improve the performance.
increment
and decrement
operations
increment
and decrement
operations
This PR extends our Memcached functionality by introducing
increment
anddecrement
operations. This addition allows users to perform essential arithmetic task, crucial for diverse caching scenarios and furnishes a more varsity interface.Motivation:
To enhance interactions with Memcached servers, ensuring both versatilely and dynamics. The introduction of
increment
anddecrement
operations address a vital need in caching scenarios, facilitating direct arithmetic adjustments and expanding our API's capabilities.Modifications:
ArithmeticCommand
to ourMemcachedRequest
struct enablingarithmetic
operations in upcoming requests.MemcachedRequestEncoder
to handlearithmetic
operation request.MemcachedConnection
now boastsincrement
anddecrement
methods, providing developers with a straightforward way to interact with the Memcached server forarithmetic
processes.ArithmeticMode
enum to clearly differential betweenincrement
anddecrement
operations.arithmeticDelta
property toMemcachedFlags
which designates the magnitude by which a value should be incremented or decremented.Results:
With the addition of the
increment
anddecrement
operation support, our API now allows users to directly manipulate numerical values stored within Memcached servers. This fosters more dynamic caching strategies and enhances overall data management efficiency.