question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using the results of "KEYS" when a `keyPrefix` option is being used

See original GitHub issue

hi there,

when a keyPrefix option is used is becomes a little tricky to work with the KEYS command:

  1. you have to manually apply the prefix in the argument to “KEYS” in order to reasonably find keys that match
  2. 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
luincommented, Oct 13, 2016

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 😆

1reaction
luincommented, Jul 15, 2017

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::

SET users:178.name "Bob"
SET posts:201.name "Welcome to the WordPress"

keyPrefix is designed for this use case: you may create two Redis instances with different prefixes, one for users and the other for posts:

userRedis.set(`${id}.name`, 'bob')
postRedis.set(`${id}.name`, 'Welcome to the WordPress')

In the above case, keys (and scan) 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:

SET APP1:data.1 good
SET App2:data.1 bad

You should setup a single Redis database for each application.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Organizing objects using prefixes - AWS Documentation
You can use prefixes to organize the data that you store in Amazon S3 buckets. A prefix is a string of characters at...
Read more >
useTranslation (hook) - react-i18next documentation
Use the useTranslation hook inside your functional components to access the ... Do not use the keyPrefix option if you want to use...
Read more >
How to type check i18n dictionaries with TypeScript?
Here is an example making use of string interpolation. // retrieves all variable placeholder names as tuple type Keys<S extends string> = S ......
Read more >
How to Load Data From an Amazon S3 Bucket Into Redshift
Additionally, we'll discuss some options available with COPY that allow the ... Using the key prefix variable, we can load these files in ......
Read more >
Step 5: Run the COPY commands - Amazon Redshift
In this tutorial, you use the following COPY command options and features: Key prefix. For information on how to load from multiple files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found