0

I am having some data stored in redis with below given keys
I have stored some keys into redis like

key1 = https://abc.net/v/140225014843/css/
key2 = https://abc.net/v/153729007613/css/
key3 = https://abc.net/v/240125414249/css/
key4 = https://abc.net/v/140225014843/css/:tokens
key5 = https://abc.net/v/240125414249/css/:tokens

Now i am having data = 140225014843 i want to fetch key and it's value which is having that data inside it .
Example : key1 is having data inside it so i want to fetch key1 from redis. I am using django-redis.

Edit :
Key4 is also having data into it but i want to fetch only those keys which are of pattern like key1 .

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
  • i used red.keys("*140225014843*").but output here is coming ['https://abc.net/v/14022501484/css/:tokens', 'https://abc.net/v/14022501484/css/'] .... i want only value of 'https://abc.net/v/14022501484/css/ – Prashant Gaur Feb 25 '14 at 07:17
  • How you name your keys is very important in redis. In above structure i dont think key is of any use for the operations you are trying to perform. What you are trying to do is GET key from pattern matching values which dissolve all purpose of key. What are these numbers (e.g.140225014843) in URL? What the size of data? Can you change Data Structure? – Jack Daniel's Feb 25 '14 at 08:45
  • data structure is already better .. here in one operation i am getting value of data . like data = 140225014843 . data is mapped with other key in redis . like redis.hget('https://abc.net', data)... here i can fetch value of data so from data i want to fetch that versioned url(key1) which is stored into redis. – Prashant Gaur Feb 25 '14 at 08:49
  • Exactly !!! thts my point is. Shouldnt it would have been better if your key should have been 140225014843 and SET contain all the urls which has 140225014843. By doing this you would have got O(1) performance – Jack Daniel's Feb 25 '14 at 09:00
  • 1
    thanks for your suggestion .it is much better way – Prashant Gaur Feb 25 '14 at 09:08

1 Answers1

1

You should rethink the way you name your keys, as it is an important decision.

You could use a List for each data value you have, with that value being the key and "paths" of that data value being the members of the list.

For example in your case you could do:

redis> RPUSH 140225014843 "css/"
redis> RPUSH 153729007613 "css/"
redis> RPUSH 240125414249 "css/"
redis> RPUSH 140225014843 "css/:tokens"
redis> RPUSH 240125414249 "css/:tokens"

Depending on what is the variable part in your data, you could adjust this approach. For example if "css/" is always present then you could omit it.

Also you may not want duplicates in your lists, in which case you should use a Set instead.

Agis
  • 32,639
  • 3
  • 73
  • 81