Redis Set Operation:
SADD key1 "value1 containing key1"
SADD key1 "value2 containing key1"
SADD key1 "value3 containing key1"
SMEMBERS
Result :
1) "value2 containing key1"
2) "value3 containing key1"
3) "value1 containing key1"
SREM key1 "value2 containing key1" --> it works
Now i want to delete "value2 containing key1" from set, without passing whole value e.g. "value2 containing key1". Want to send only "value2"
SREM key1 "value2"
is there any way to do this ? Or any other method?
Instead of inserting into SET the full value("value1 : verylongmessage"), you can just inserting value("value1") to the set "key1".
BUT you should use another data structure, Hash to store the full content of the value1:
hash['value1'] = "value1 : verylongmessage" (in Redis, you can use HSET?)
So, if you want to delete value1 from the SET, you just delete from both the SET and the HASH.
Hope this can help you ~~~
Related
I have a large set of ids (around 100000) which I want to store in redis.
I am looking for the most optimal way through which I can check if a given list of ids, what are the ids that are part of my set.
If I use a redis set, I can use SISMEMBER to check if a id is part of my set, but in this case I want to check if, given a list of ids, which one is part of my set.
Example:
redis> SADD myset "1"
(integer) 1
redis> SADD myset "2"
(integer) 2
redis> MYCOMMAND myset "[1,2,4,5]"
(list) 1, 2
Does anything of this sort exist already ?
thanks !
I am following BITOP command in redis.
http://redis.io/commands/bitop.
While using snippet command as bellow:
redis> SET key1 "foobar"
OK
redis> SET key2 "abcdef"
OK
redis> BITOP AND dest key1 key2
(integer) 6
redis> GET dest
"`bc`ab"
Why BITOP AND dest key1 key2 return (integer) 6 ? Is it the length of the key dest. And why result of key dest is "'bc'ab". Because in mathematic theory, I never seen AND operation between two strings. Can anyone explain me the way to AND two strings ? Thanks so much !
BITOP command does bitwise operations on binary representations of any strings you provide.
Why BITOP AND dest key1 key2 return (integer) 6 ? Is it the length of the key dest
Yes it is. From doc :
Return value
Integer reply
The size of the string stored in the destination key, that is equal to the size of the longest input string.
I never seen AND operation between two strings. Can anyone explain me the way to AND two strings ?
AND or any bitwise operation between two strings means same operation between binary representations of these strings, and if the strings are of unequal length in their binary representation, the shorter strings are padded with zeroes on the left to match the larger string ( done in redis BITOP too ).
Binary representations of "foobar" and "abcdef" are ( spaces for clarity):
"foobar" : 01100110 01101111 01101111 01100010 01100001 01110010
"abcdef" : 01100001 01100010 01100011 01100100 01100101 01100110
Now you can perform any bitwise operation on them and then convert the resulting bits to string representation.
I've saved a list into a hash but cannot figure out how to retrieve contents of the hash. Firstly, here is some code to create the list:
127.0.0.1:6379> LPUSH list1 'dc:39:79:ab:cd:ef'
(integer) 1
127.0.0.1:6379> LPUSH list1 '2014-07-21'
(integer) 2
127.0.0.1:6379> LPUSH list1 'Success'
(integer) 3
127.0.0.1:6379> LPUSH list1 'Miscellaneous notes about the install. Can be as long as you want'
(integer) 4
Now I create a hash and assign the value of one key to list1:
127.0.0.1:6379> hset hash 'RKT1234' list1
(integer) 1
How can I print the entire list saved inside hash['RKT1234'] ?
127.0.0.1:6379> hgetall hash
1) "RKT1234"
2) "list1"
127.0.0.1:6379> hvals hash
1) "list1"
thanks
It looks like you are attempting to store a list as a value in a hash. You are not doing what you think you are doing as Redis does not support nested data structures. In hset hash 'RKT1234' list1 you are not storing the list, just a string name: "list1".
In order to get the contents of list you need to first get the name of the list from the hash, then get the contents of the list in a second call.
So your sequence looks like this:
# returns "list1"
hvals hash
lrange list1 0 -1
# returns the contents of list1
Cheers
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 .
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.
I have a a list in redis containing a sequence of Ids. Each id is unique for a single object which I am storing as a JSON string on a separate key.
So I have something like:
redis> LRANGE mylist 0 -1
1) "one"
2) "two"
3) "three"
And I have separate keys mylist:one, mylist:two, mylist:three.
I am saving the ids to a list in order to build a simple FIFO queue on my application.
What is the most efficient way to get all the ids in mylist and their matching values from each individual key? Is there a better way to go about it?
The most efficient way is probably to use the SORT command:
# Populate list
rpush mylist one two three
set mylist:one 1
set mylist:two 2
set mylist:three 3
# Retrieve all items with their corresponding values
sort mylist by nosort get # get mylist:*
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"