is there any thing like get_next in redis? - redis

Is there any support in redis to walk keys ? some thing like get_next api ? I did some search but couldnt find any thing relevant. my requirement is to get a key from a value so i iterate over all the keys and try to match the value with that of the required.

AFAIK, there's no get_next type of api/command in Redis. You can do some trick with the key though. Something like 1:id:<key_name>, 2:id:<key_name>, ..., n:id:<key_name>. You can use INCR to have an incremental counter. Also consider looking at keys to find a bunch of keys with a particular pattern.

Related

Is it possible to get and remove something from a hash in Redis in one operation?

I'm currently using Redis and doing an HGET followed by an HDEL to remove the field (and its value) from the hash once I've retrieved the value I need (which only needs to be used once).
Is there any way to do this in one operation (i.e. get a field and remove it from the hash)? I skimmed through the docs and couldn't find the right command.
Transactions will do this. Use the MULTI command to start a transaction, within the transaction HGET what you want, HDEL it, and then EXEC.
You could also write a Lua script to do this.

Can I override a list value with a new list, natively in Redis?

I'd like to use the list data type in Redis, but I'd like to simply override/set the list with a value instead of interacting with it in push/pop fashion.
For my business case, I need the queryable nature of a list, but need to set/override the entire list.
Is this possible with native Redis commands?
(Not thread safe, but I can delete the key and then use LPUSH). Perhaps this could be wrapped in a Lua Script if needed.
I don't think that's possible. Your best bet for a secure and nice solution would be to use a Lua script, or a transaction, to make sure it's executed as an atomic operation. One possible option could be:
MULTI
DEL my_list
RPUSH my_list "A" "B" "C"
EXEC
If you're using expiration for the key, before removing it you could read the remaining expiry time with TTL my_list.

Alternative to Redis' SCAN for auto-suggestions feature in a search bar?

I have a simple redis DB where the key value is like so:
symptom_name: symptom_id
In my search bar, I'd like to implement an auto suggestions feature that will display any symptoms that are similar.
For example, typing 'pain' may also bring up 'stomach pain', 'knee pain', etc.
I thought about using the SCAN command matching with patterns to quickly fetch all the symptom names that are similar, but the problem is that the scan command can't fetch all the keys that match the pattern in one go.
What's my alternative here?
Here is autocomplete with Redis explained by the author of Redis:
Auto Complete with Redis

How to list keys without serial number in REDIS?

I am trying to list keys with specific pattern like below:
KEYS "*Team*"
and I am getting resultset with serial number like below:
1) "TeamMetricSummary_google_bps_app_google wfep
league_chambersc2016:04-03-2016_06-04-2016"
2) "\xac\xed\x00\x05t\x00TTeamMetricSummary_google_bps_app_google wfep
league_malini.gto:12-06-2015_04-02-2016"
My problem is that I want to avoid serial number in result set.
Is that possible?
That is not possible. Redis will return the whole key. You can use regex or string operations like split in your application logic to achieve this. For this you must know your input. For example if your key is in a pattern like xTeamNamey. where x and y are some constraints (serial number) you want to avoid, you can insert your key like x:TeamName:y. In retrieval you can use string.split(":")[1] to get the TeamName.
To answer your specific question:
Redis supports Lua scripting. If you're on a version of Redis that is bundled with Lua version 5.0 or above, you Lua script can use regular expressions. Write a Lua script that does redis.call of KEYS command and then does pattern matching to remove serial numbers for you.
Alternative:
By the way, assuming above is part of software that runs in production, here is what I would suggest: Don't use KEYS command on production! As it's documentation says, Redis has to go through all the keys to get keys matching your pattern. Alternatively, you may consider doing shadow writes to a Redis set that's trimmed of serial number every time you add a key. When you need list of keys, you can read the whole set. However, you'll have to manually handle the expiration of keys.

How do I get list of keys/values using Booksleeve?

I'm trying to get a list of values, where key name starts with let's say "monkey".
I really couldn't find a doc on this. :(
How can I do this? What API should I use? Keys, Sets, Strings? What method?
Or it is not available yet, but a workaround?
Thanks
Redis does not have a "get all keys like {x} along with their values" command, but it does have:
get all keys like {x}
get value of key / keys
Whether your approach is sensible in the first place depends a bit on which server version you are using. If you are on a recent version then the library will use SCAN, which is not terrible. On older server versions it will use KEYS, which is to be avoided at all costs. I am not at a PC so this is pseudo-code only, but:
foreach(var batch in db.GetKeys("monkey*")
.Batchify(100))
{
list.AddRange(await db.Strings.GetString(batch));
}
Note that this isn't optimized - the batches could be fun much more concurrently than the above - but I would need a keyboard and compiler to demonstrate that!