I am writing a REST API that exposes a method that allows the caller to delete a resource from the REDIS database. Because I’m using a SET, when you try to do an SREM, it will just return the number of records that were deleted.
Please consider the following use cases and the REDIS results:
Delete a member that doesn't exist: 127.0.0.1:6379> srem 123455 firstname.lastname@example.org (integer) 0 Delete multiple members at once: 127.0.0.1:6379> srem 123455 email@example.com firstname.lastname@example.org (integer) 2 127.0.0.1:6379> Delete multiple but it only finds one match: 127.0.0.1:6379> srem 123455 email@example.com firstname.lastname@example.org (integer) 1 127.0.0.1:6379> Delete email for an account that doesn't even exists 127.0.0.1:6379> srem bogus email@example.com (integer) 0 127.0.0.1:6379>
I’m wondering how I should interpret a return value from REDIS when I can the SREM command, as far as the REST API that I’m writing is concerned. (and ultimately what kind of a return value I send back to the caller)
For example, when the caller asks me to delete a list of email addresses, if no matches are found, REDIS returns 0. This could mean it failed because it didn’t do anything but from the business point of view, this is a successful request because I know those emails don’t exist.
For now, I always return a successful result, unless there is some unexpected behavior.
def delete_db_email(mailbox, profiledata): try: if DEBUG: logging.info("delete_db_email debug started:") count = len(profiledata) if DEBUG: logging.info("the number of emails to process is: " + str(count)) r = redis.Redis(connection_pool=POOL) mailbox = str(mailbox) keyname = mailbox + "-emails" response = r.srem(keyname, *set(profiledata)) return True, 200 except Exception as ex: logging.error("delete_db_email() failed for mailbox:" + mailbox) logging.error(ex) return False, 500
From a design / behavior point of view, can you give me some constructive criticism re: what is good / not good about this approach?
One thing I could do is an explicit check for the mailbox number in question so I can return a 404 if the mailbox doesn’t exist, but that is an extra database call … which I’m not sure if it’s worth it.