using the results of "KEYS" when a `keyPrefix` option is being used
See original GitHub issuehi there,
when a keyPrefix
option is used is becomes a little tricky to work with the KEYS command:
- you have to manually apply the prefix in the argument to “KEYS” in order to reasonably find keys that match
- you have to manually remove the prefix from values in the returned array of in order to use them
the following example shows searching for keys and then attempting to deleting them (nothing is deleted because redis.del()
will actually attempt to delete “foo:foo:bar:baz”)
const Promise = require('bluebird');
const Redis = require('ioredis');
const redis = new Redis({keyPrefix: 'foo:'});
redis.set('bar:baz', 1)
.then(() => redis.keys('foo:*'))
.then(keys => Promise.map(keys, k => redis.del(k)))
;
I think it would be nice if redis.keys()
supported a second argument of options so that you could do something like this (I’m hoping that the suggested keysOptions
are self explanatory):
const Promise = require('bluebird');
const Redis = require('ioredis');
const redis = new Redis({keyPrefix: 'foo:'});
const keysOptions = {
applyKeyPrefix : true,
stripKeyPrefixFromResults : true,
};
redis.set('bar:baz', 1)
.then(() => redis.keys('*', keysOptions)) // performs KEYS with "foo:*"
.then(keys => Promise.map(keys, k => redis.del(k))) // returns ["bar:baz"]
;
Granted you may not agree with this idea, regardless I’m interested to know if you have any suggestions/advice for using the results of “KEYS” when a keyPrefix
option is being used.
Currently I’m using something akin to the following for stripping the keyPrefix
value from the returned keys (bare in mind that the keyPrefix
may contain a regex meta-character that needs to be escaped):
// "k" is a item from the arrya returned by "redis.keys()"
k.replace(RegExp('^' + redis.options.keyPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')), '')
IMO this is a bit inelegant (and error prone) to have to always do this in consuming code.
rgds, Jochem
PS - enjoying using your lib, especially the defineCommand
functionality! nice work!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
There’re already several related issues posted: #254 #359 #325. A warning is added to the “Transparent Key Prefixing” section in README for this matter.
Currently I’m not planning to add support for prefixing patterns and replies given it’s not a good practice to use key prefix as namespace to allow multiple applications share a same redis database.
PS - thank you for using ioredis 😆
Name spacing is recommended only when it’s used in the same application. For example, it’s useful to store users and posts details in the same database by prefixing them with
users:
&posts:
:keyPrefix
is designed for this use case: you may create two Redis instances with different prefixes, one for users and the other for posts:In the above case,
keys
(andscan
) is rarely needed to list all keys that have the same prefixes since, in the production, you may have a SET for indexing them.However, it’s not recommended to have multiple application sharing the same database:
You should setup a single Redis database for each application.