1

How to get all keys that not starts from "enduser"

My List

"enduser_tom"
"master_1"
"enduser_jack"
"slave_1"
"slave_2"
"enduser_harry"
"enduser_panther"

Result should be :

"master_1"
"slave_1"
"slave_2"

This is what i tried

  • keys * ->list all keys
  • keys *enduser -> list all keys that end with enduser
  • keys enduser* -> list all keys that start with enduser

So need assistance in getting above result.

Mr punch
  • 1,756
  • 4
  • 16
  • 23
  • I don't believe there is a native exclusion function for this. Depending on the size of your lists, you may want to handle the exclusions in your application. – DvideBy0 Dec 09 '15 at 09:13
  • depending on the needs of your data, you could also try storing these values differently to meet your needs. – DvideBy0 Dec 09 '15 at 09:14
  • @DvideBy0 its a very big list.Just for example i have shortened it.In redis if there is starts with , ends with , functions why can't i write not in it.So i wrote this post in stack – Mr punch Dec 09 '15 at 09:21

1 Answers1

1

AFAIK the KEYS command is incapable of doing this.

Use a Lua script:

local keys, filtered = redis.call('KEYS', '*'), {};
for i=1,#keys do
    if string.sub(keys[i], 1, string.len(ARGV[1])) ~= ARGV[1] then
        filtered[#filtered + 1] = keys[i];
    end
end
return filtered

SCRIPT LOAD registers the script, you can later execute it using EVALSHA:

127.0.0.1:6379> SCRIPT LOAD "local keys, filtered = redis.call('KEYS', '*'), {};\nfor i=1,#keys do\n    if string.sub(keys[i], 1, string.len(ARGV[1])) ~= ARGV[1] then\n        filtered[#filtered + 1] = keys[i];\n    end\nend\nreturn filtered"
"fca86e91217c20c90b268539603bdb16c7fdbc32"
127.0.0.1:6379> EVALSHA fca86e91217c20c90b268539603bdb16c7fdbc32 0 enduser

You could also save it to a file and execute it using redis-cli --eval:

redis-cli --eval not_startswith.lua , enduser
vaultah
  • 44,105
  • 12
  • 114
  • 143