Installing Redis

Download the latest Redis version from https://redis.io/download. Unzip the tar.gz file, enter the redis directory, and compile Redis using the make command, as follows:

cd redis-4.0.9
make

After installing it, use the following shell command to initialize the Redis server:

src/redis-server

You should see an output that ends with the following lines:

# Server initialized
* Ready to accept connections

By default, Redis runs on port 6379. You can specify a custom port using the --port flag, for example, redis-server --port 6655.

Keep the Redis server running and open another shell. Start the Redis client with the following command:

src/redis-cli

You will see the Redis client shell prompt like this:

127.0.0.1:6379>

The Redis client allows you to execute Redis commands directly from the shell. Let's try some commands. Enter the SET command in the Redis shell to store a value in a key:

127.0.0.1:6379> SET name "Peter"
OK

The preceding command creates a name key with the string value "Peter" in the Redis database. The OK output indicates that the key has been saved successfully. Then, retrieve the value using the GET command, as follows:

127.0.0.1:6379> GET name
"Peter"

You can also check whether a key exists using the EXISTS command. This command returns 1 if the given key exists, 0 otherwise:

127.0.0.1:6379> EXISTS name
(integer) 1

You can set the time for a key to expire using the EXPIRE command, which allows you to set time to live in seconds. Another option is using the EXPIREAT command that expects a Unix timestamp. Key expiration is useful to use Redis as a cache or to store volatile data:

127.0.0.1:6379> GET name
"Peter"
127.0.0.1:6379> EXPIRE name 2
(integer) 1

Wait for two seconds and try to get the same key again:

127.0.0.1:6379> GET name
(nil)

The (nil) response is a null response and means that no key has been found. You can also delete any key using the DEL command, as follows:

127.0.0.1:6379> SET total 1
OK
127.0.0.1:6379> DEL total
(integer) 1
127.0.0.1:6379> GET total
(nil)

These are just basic commands for key operations. Redis includes a large set of commands for other data types, such as strings, hashes, sets, and ordered sets. You can take a look at all Redis commands at https://redis.io/commands and all Redis data types at https://redis.io/topics/data-types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.94.153