command suggestion: string "get and delete"
Possible suggestion for rxstrings - for context see https://github.com/StackExchange/StackExchange.Redis/issues/622
So I created this command and called it STRPOP. Is that a decent name?
Should it accept multiple keys?
Name-wise: sounds like a good solid name Multi-wise: I prefer to do these in a MULTI/EXEC block or Lua... multi-key commands are notoriously and obviously harder to support in a cluster, which leads to these becoming unsupported in it, which is kind of a shame.
Suggestion: what do you think about popping ranges? I.e. if ungiven to STRPOP, the range default to all (i.e. 0 -1), but I can just trim the end (i.e. -1 -1) the beginning (0 0) or anything else I want, LTRIM-like.
I like your range idea. I see two ways of going with it:
-
Go very general/flexible. In a way,
CHOPis a subset of that range pop, except it doesn't return the value. [I'm chopping 10MB long strings and don't need the result so don't waste time copying/transmitting it.] We could have a command modifier which selects whether to return the value or an integer count. Also you can't CHOP the middle of a string. So we could create one uber-command that can do CHOP, STRPOP, TRIMS, etc. -
Go very specific. Have a
STRPOP,CHOP,STRPOPRANGE,whatever else. I do like some of the fine-grained aspects.. makes it easy to write stuff without the documentation and code that uses these commands are read clearly.
How about something like this:
STRPOP key [start [end]] [NODELETE] [LENGTH]
Removes the substring of the string value stored at key and returns it. If the resulting string value is empty, then the key is deleted. This can be overridden with the optional NODELETE argument, which will leave an empty string at key.
The range determined by the offsets start and end (both are inclusive). Negative offsets can be used in order to provide an offset starting from the end of the string. So -1 means the last character, -2 the penultimate and so forth. The function handles out of range requests by limiting the resulting range to the actual length of the string.
start and end are optional and default to 0 and -1. When used with the other default arguments, STRPOP is equivalent to an atomic GET and DEL of key.
Normally, the substring is returned as a reply. If the optional LENGTH argument is specified, the length of the remaining string value is returned instead. This is useful when one doesn't need the results, saving on copies and transfers.
An error is returned if the key exists and does not hold a string.
Reply: String, the substring of key or NULL if key didn't exist. If LENGTH is specified, it is an Integer length of the key's value after the STRPOP operation.