Pergunta

Say I have a namespaced key for user + id:

  • lastMessages
  • isNice attribute

So - it goes like this :

>lpush user:111:lastMessages a
>lpush user:111:lastMessages b
>lpush user:111:lastMessages c

ok

let's add the isNice prop:

>set user:111:isNice 1

so : let's see all keys for 111 :

> keys user:111*

result :

1) "user:111:isNice"
2) "user:111:lastMessages"

Ok , BUT

I want to expire the namespaced entry at it's whole ! ( so when timeout - all the keys should go at once. I don't want start managing each namespaced key and time left because not all props are added at the same time - but i want all props to be dead at the same time...)

Question :

Does it mean that I have to set expire for each namespaced key entry ?

if not , what is the correct way of doing it ?

Foi útil?

Solução

Yes, the way you have it set up, these are all just separate keys. You can think of the namespace as an understanding you have with all the people who will access the Redis store

Okay guys, here's the deal. We're all going to use keys that look like this:

user:{user_id}:lastMessages

That way, we all understand where to look to get user number 325's last messages.

But really, there's nothing shared between user:111:lastMessages and user:111:isNice.

The fix

A way you can do what you're describing is using a hash. You will create a hash whose key is user:111 and then add fields lastMessages and isNice.

> hset user:111 lastMessages "you are my friend!"
> hset user:111 isNice true
> expire user:111 1000

Or, all at once,

> hmset user:111 lastMessages "you are my friend!" isNice true
> expire user:111 1000

Here is a page describing redis' data types. Scroll down to where it says 'Hashes' for more information.

Edit

Ah, I hadn't noticed you were using a list.

If you don't have too many messages (under 20, say), you could serialize them into JSON and store them as one string. That's not a very good solution though.

The cleanest way might just be to set two expires.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top