Redis key naming conventions?

2019-01-12 15:39发布

What are the normal naming convention for keys in redis? I've seen values separated by : but I'm not sure what the normal convention is, or why.

For a user would you do something like...

user:00

if the user's id was 00

Are you able to query for just the beginning of the key to return all users?

I'm mainly just hoping to avoid any future problems by researching the ways that work for people and why they chose them.

4条回答
Animai°情兽
2楼-- · 2019-01-12 15:57

I don't know if there really are widespread "best practices" for Redis key naming yet.

I've experimented with using ASCII NUL characters as my separators (since Redis and Python are both 8-bit clean). It looks a little ugly if you're looking at raw keys, but the idea is to hide it behind an abstraction layer. Colon and pipe symbols are obvious alternatives so long as the components of your name space are either guaranteed not to use them or you're willing to encode each component as necessary. However, if you'd be encoding them then you'd want develop the abstraction layer and avoiding viewing raw keys anyway ... which brought me right back to just using \0 in my reasoning.

I'll be interesting in seeing if any other opinions are articulated on this.

查看更多
3楼-- · 2019-01-12 16:06

We use a colon (:) as namespace separator and a hash (#) for id-parts of keys, e.g.:

logistics:building#23
查看更多
在下西门庆
4楼-- · 2019-01-12 16:10

A convention seems to be colon (:) but I'm a web developer so I personally prefer slash (/) for the separator. Slash is already so important separator within URLs which are meant to be Uniform Resource Locators so kind of keys for resources. Why to take a different approach with colon (:)? Does it help anything?

Consider this example:

We have an RESTful API for toy objects. There is a one:

http://example.com/api/toy/234 

Where we have it stored? We use Redis and slashes so the key is obvious:

toy/234

This is the unique key for the toy. The key can now be used also on client side:

{
    key: "toy/234",
    color: "red",
    url: function () {
        return API_BASE_URL + this.key;
    }
}

An user requests an object with key toy/666. How to get it from Redis? A Node.js related example:

redis.get(key, function reply_callback(error, toystring) {
    var toy = JSON.parse(toystring);
    ...
}

No need to convert slashes to colons and vice versa. Convenient, don't you think?

Note: always ensure that user is able to access only things you intended. The raw URL-to-key approach above is able to fetch user/1/password as well, as noted by commentators. This should not be a problem if you use Redis as a public read-only cache.

查看更多
Melony?
5楼-- · 2019-01-12 16:19

What are the normal naming convention for keys in redis? I've seen values separated by : but I'm not sure what the normal convention is, or why.

Yes, colon sign : is a convention when naming keys. In this tutorial on redis website is stated: Try to stick with a schema. For instance "object-type:id:field" can be a nice idea, like in "user:1000:password". I like to use dots for multi-words fields, like in "comment:1234:reply.to".

Are you able to query for just the beginning of the key to return all users?

If you mean someting like directly querying for all keys which starts with user: there is a keys command for that. This command should be however used only for debugging purpose since it's O(N) because it's searching through all keys strored in database.

More appropriate solution for this problem is to create dedicated key, let's name is users, which will store all the users keys, for example, in list or set data structure.

查看更多
登录 后发表回答