-1

Using sort one can sort a set and get external keys using results from the sort component of query.

By way of example:

  • If the external key/value are defined as various keys using the pattern:itemkey:<somestring>
  • And a sorted list has list of the members then issuing command sort <lists key> by nosort get itemkey:* would get the values of the referenced keys.

I would like to be able to sort through a sorted list and delete these individual keys but it appears that sort <key> by nosort del itemkey:* is not supported.

Any suggestions on how to get list of values stored in a set and then delete the external keys?

Obviously I can do this with two commands, first getting the list of values and then by iterating through list call the delete function - but this is not desirable as I requite atomic operation.

colinbes
  • 417
  • 6
  • 14

1 Answers1

0

To ensure atomic operation one can use either transactions or redis' lua scripts. For efficiency I decided to go with using script. This way the entire script is completed before next redis action/request is processed.

In code snippet below. I used loadScript in order to store script redis side reducing traffic with every call, the response from loadScript is then used as identifier to Jedis's evalsha command.

Using Scala (Note Jedis is a Java library, hence the .asJava):

val scriptClearIndexRecipe = """local names = redis.call('SORT', KEYS[1]);
   | for i, k in ipairs(names) do
   |   redis.call('DEL', "index:recipe:"..k)
   | end;
   | redis.call('DEL', KEYS[1]);
   | return 0;""".stripMargin

def loadScript(script: String): String = client.scriptLoad(script)

def eval(luaSHA: String, keys: List[String], args: List[String]): AnyRef = {
  client.evalsha(luaSHA, keys.asJava, args.asJava)
}
colinbes
  • 417
  • 6
  • 14