Lecture
Sets in Redis – this is an unordered collection of strings. A set
can contain only unique values.
The sadd command adds a value to the set stored at the key.
With the Smembers command you can view all the values
of a given set. We can see that all the values are arranged in
random order.
sismember checks whether a given value is a member of the set.
spop removes a value from the set
127.0.0.1:6379> sadd nameset Kolya
(integer) 1
127.0.0.1:6379> sadd nameset Oleg
(integer)
127.0.0.1:6379> sadd nameset And
(integer)
127.0.0.1:6379> smembers nameset
In sorted sets, the ordering is done by the
added score value. So, with the zadd command we create
a sorted set and add a value to it.
> zadd peoples 2053 "Richard Stallman"
> zadd peoples 2040 "Alan Kay"
> zadd peoples 2065 "Yukihiro Matsumoto"
> zadd peoples 2016 "Claude Shannon"
The zrange command returns the specified range of elements in
the sorted set stored at the key. In this case
sorted by year of birth in ascending order.
Zrevrange sorts in descending order.
zrangebyscore returns all the elements in the sorted set at the
key with a score between min and max. The elements are considered
to be ordered in ascending order.
zremrangebyscore removes all elements in the sorted set
stored at the key with a score between min and max (inclusive).
Zrevrange sorts in descending order.
zrangebyscore returns all the elements in the sorted set at the
key with a score between min and max. The elements are considered
to be ordered in ascending order.
zremrangebyscore removes all elements in the sorted set
stored at the key with a score between min and max (inclusive).
127.0.0.1:6379» zrange peoples 0 -1
1) "Claude Shannon"
2) "Alan Kay"
3) "Richard Stallman"
4) "Yukihiro Matsumoto"
127.0.0.1:6379> zrevrange peoples 0 -1
1) "Yukihiro Matsumoto"
"Richard Stallman"
"Alan Kay"
"Claude Shannon"
127.0.0.1:6379> zrangebyscore peoples -inf 2050
1) "Claude Shannon"
2) "Alan Kay"
127.0.0.1:6379> zremrangebyscore peoples 2046 2060
(integer) 2
127.0.0.1:6379> zrange peoples 0 -1
1) "Claude Shannon"
2) "Yukihiro Matsumoto"
127.0.0.1:6379>
With the rpush command we create a list and add
certain values to it. Rpush adds a value to the end of the list, lpush to the
beginning.
127.0.0.1:6379> rpush surnames 'Petrenko'
(integer) 1
127.0.0.1:6379> rpush surnames ‘Ivanov’
(integer)
127.0.0.1:6379> rpush surnames ‘Holanko'
integer
With the lrange command we output this list within a certain
interval.
127.0.0.1:6379> lrange surnames 0 1
1) "Petrenko"
2) "Ivanov"
127.0.0.1:6379> lrange surnames 0 2
1) "Petrenko"
2) "Ivanov"
3) "Intelectov"
127.0.0.1:6379 >
The llen command shows how many elements are contained in the list.
lpop removes and returns the first element of the list. Rpop – the last one.
127.0.0.1:6379> llen surnames (integer) 3 127.0.0.1:6379> lpop surnames "Petrenko” 127.0.0.1:6379> llen surnames (integer) 2 127.0.0.1:6379> lrange surnames © 1 1) "Ivanov" 2) "Intelectov" 127.0.0.1:6379> lrange surnames 2 2 1) "Ivanov" 2) "Intelectov" 127.0.0.1:6379>
A hash table – a data structure that implements the interface
of an associative array; in particular, it allows storing (key,
value) pairs and performing three operations: the operation of adding a new
pair, the lookup operation, and the operation of deletion by key.
In a hash table, instead of directly using the key as
the array index, the index is computed from the key value.
To create or add a value and a key to a hash table,
we use the hset command.
To output the keys of a hash table, we use the hkeys command.
To output the values – hvals.
To create several keys at once in a single operation,
hmset is used
To output both the values and the keys – hgetall
127.0.0.1:6379 > hset user1 name 'Kolya'
(integer) 1
127.0.0.1:6379 > hset user1 email 'kolya@mai.com'
(integer) 1
127.0.0.1:6379> hkeys user1
1) "name"
2) "email"
127.0.0.1:6379> hvals user1
1) "Kolya"
2) "kolya@mai.com
127.0.0.1:6379> hgetall user1
1) "name"
"Kolya"
"email"
4) "kolya@mai.com"
127.0.0.1:6379>
With the hincrby command you can add a certain value to some field.
In this case we increased the value of age by 2.
To delete a specific key you can use the hdel command.
(integer) 1
127.0.0.1:6379> hincrby user1 age 2
(integer) 20
127.0.0.1:6379> hvals user1
1) "Kolya"
2) "kolya@gmai.com"
127.0.0.1:6379> hdel user1 age
(integer) 1
127.0.0.1:6379> hvals user1
Comments