Lecture
Now we can move on from theory to practice.
To communicate with Redis, the redis-
cli client program is used.
The set command is used to set the value of a key.
In this case, the key name is assigned the value
Kolya.
The get command is used to retrieve a value by its key.
The getset command sets the value of a key and outputs
the previous value of that key. The mset and mget commands allow you to
set or retrieve the values of several keys respectively.
This command helps you check whether a value is stored under the given
key. In this case, checking the key name
returns 1, while checking the key surname returns 0.
127.0.0.1:6
(integer) 1
127.0.0.1:6
(integer) 0
127.0.0.1:6379>
379> exists name
379»
With the type command you can find out what data type
is stored under a given key.
127.0.0.1:6379> get name
"Oleg"
127.0.0.1:6379> get age
"19"
127.0.0.1:6379> type name
string
127.0.0.1:6379> type age
string
127.0.0.1:6379>
The keys command can find keys. In this case, the key name
was found using the pattern na?
127.0.0.1:6379> keys
(error) ERR wrong number of arguments for 'keys' command
127.0.0.1:6379> keys n
(empty list or set)
127.0.0.1:6379> keys na*"
1) "name"
127.0.0.1:6379> _
The del command deletes a value by its key.
127.0.0.1:6379» set name:my:friend "Andrey"
OK
127.0.0.1:6379> del name:my:friend
(integer) 1
127.0.0.1:6379> exists name:my:friend
(integer) 0
127.0.0.1:
LIFETIME OF A KEY'S VALUE.
When creating a key, you can specify an additional parameter EX, which
indicates after how many seconds the key will be deleted. The ttl command lets you
see how much longer an object will "live".
If you do not provide the EX parameter, then entering the ttl command outputs
the value -1, and if the object's lifetime has already expired, it outputs
the value –2.
127.0.0.1:6379> set test1 "1"
OK
127.0.0.1:6379> ttl test1
(integer) -1
127.0.0.1:6379> set test2 '2' EX 20
OK
127.0.0.1:6379> ttl test2
(integer) 8
127.0.0.1:6379> ttl test2
(integer) -2
127.0.0.1:6379> exists test2
(integer) 0
127.0.0.1:6379>
Allows you to change the name of a key. In this case, we change the name of
the key test to the name space.
127.0.0.1:6379> set test "space"
127.0.0.1:6379> rename test space
127.0.0.1:6379>
The incr and decr commands add one to and
subtract one from the value of a key respectively.
The incrby and decrby commands increase the value by a specified number.
127.0.0.1:6379> set value1 10
OK
127.0.0.1:6379> incr value1
11
Comments