Redis is a high performance database that keeps all data in memory. Can be used as a database server, a cache server queues and message store. The program is written in C programming language and has libraries for use in different programming languages, including PHP.
In this article we will discuss how to install Redis Ubuntu 18.04 how to use the program and how to connect and use a library for PHP.
To install Redis in Ubuntu 18.04
The program is in the official repositories of the distribution, so to install it simply run the command:
sudo apt install redis-server
Then start the redis services and add it to the startup:
sudo systemctl start redis-server
sudo systemctl enable redis-server
Redis will start and will work fine with the default configuration, but it is recommended to set the amount of memory available for the program. To do this, open the file /etc/redis/redis.conf and add the following lines:
sudo vi /etc/redis/redis.conf
Here we specify that the maximum the program can use up to 512 megabytes. Maxmemory_policy parameter specifies what to do when you reach the limit of memory. Here the available values:
- volatile-lru – (Least Recently Used) to remove a key that has been used all a long and has a label expire;
- allkeys-lru – remove the key that was used long all;
- volatile-lfu – (Least Frequently Used) to remove the key that was used least of all and has the label expire;
- allkeys-lfu – delete the key that was used least of all;
- volatile-random – remove a random key with the label expire;
- allkeys-random – remove a random key;
- volatile-ttl – delete the key with the nearest time of destruction;
- noeviction – do not accept new write requests.
In this file you can configure the logging level port and address on which to expect connections program, as well as other parameters. But now let’s move on to use. After changing settings be sure to restart the service.
If you are going to use Redis with php, then you also need to install the library for that language:
sudo apt install php-redis
To access the Redis console, you can use the redis-cli. Run it in the terminal and you will get to a command prompt from where you will be able to send commands to redis. First check if a server:
To add a key you can use the set command:
set test value
The utility will offer tips so you know what you need to enter. To view the key values, use the get command:
You can also display all the keys that are stored in the database:
The utility doesn’t need to use any pattern for output, so you can display the keys that fit a certain pattern:
To delete a key you can use the del command:
To get out of the redis client, use the exit command:
This article is more about the installation of redis than about his “real” use, so we won’t go into the details of data structures and their applications. But now you can more or less navigate that is in the database.
In this article we discussed how to install Redis Ubuntu 18.04, as well as the basics of using the program. Redis could be very useful for caching, but note that the program stores all data in RAM, so when you restart you will lose everything. There should not be storing important data, there is a special database.