I have a redis list I have created, I am using it as a queue at the moment that reverses once in a while. My problem is that I would like to be able to get the index of an i
As you may tell by now, Redis don't support such operation (sad face).
Although someone made some pretty good remarks on why such operation would make sense, looks like Salvatore won't implemented it any time soon.
There are basically two workarounds (as pointed out by the other answers):
ZRANK for the index.Since the first is O(N) and the latter just O(log(N)) you can probably tell which one outperforms the other.
Anyway I decided to put to the test*:
╔════════════════╦═══════════════════════╦══════════════════════╗
║ ║ Sorted Set with ZRANK ║ List with lua script ║
╠════════════════╬═══════════════════════╬══════════════════════╣
║ 1000 elements ║ 0.0638264 seconds ║ 0.2723238 seconds ║
╠════════════════╬═══════════════════════╬══════════════════════╣
║ 10000 elements ║ 00.4484714 seconds ║ 41.0661683 seconds ║
╚════════════════╩═══════════════════════╩══════════════════════╝
Yup, that's staggering 41 seconds for just ten thousand elements.
* On Windows 7, Redis 2.8 (MSOpenTech port), .NET 4 with compiler optimizations turned on and StackExchange.Redis 1.0.488.