Home  >  Article  >  Operation and Maintenance  >  How to use Redis cache in Linux

How to use Redis cache in Linux

WBOY
WBOYOriginal
2023-06-18 08:01:391742browse

Redis is a high-performance key-value database that is widely used in various application scenarios such as data caching, session management, and message queues. In the Linux operating system, we can use Redis as a cache to improve system performance and scalability. This article will introduce how to use Redis cache in Linux, mainly including the following aspects:

  1. Install Redis
  2. Configure Redis
  3. Use Redis for data caching
  4. Installing Redis

It is very easy to install Redis in Linux. You only need to use the apt-get command or the yum command to install it. The specific operations are as follows:

Ubuntu/Debian system:
$ sudo apt-get update
$ sudo apt-get install redis-server

CentOS system:
$ sudo yum install redis

After the installation is complete, you can use the following command to check whether Redis has been installed successfully:

$ redis-cli ping
PONG

If "PONG" is displayed It means that Redis has been installed successfully.

  1. Configuring Redis

Redis uses port 6379 for communication by default. We can modify the Redis configuration in the /etc/redis/redis.conf configuration file. For example, we can change the listening address, modify the number of databases, etc. The following are some commonly used configurations:

Bind IP address

bind 127.0.0.1

Listening port number

port 6379

Specify the log file location

logfile "/var/log/redis/redis.log"

Specify the password

requirepass mypassword

Specify the number of databases

databases 16

After modifying the configuration file, use the following command to restart the Redis service:

$ sudo systemctl restart redis

  1. Use Redis for data caching

The easiest way to use Redis for caching in Linux is to use the Redis client program redis-cli. The following are some commonly used commands:

Connect to Redis server

$ redis-cli -h -p

Set value

$ set

Get the value

$ get

Set the expiration time

$ expire < ;key>

Delete value

$ del

For example, the following command sets a key-value pair and caches it for 10 seconds:

$ set mykey "Hello World"
$ expire mykey 10

It is also very easy to use Redis for caching in the program. We can use the official client library of Redis or the Redis client library in other languages, such as the redis-py library in the Python language. The following is an example of a Python program using the redis-py library for caching:

import redis

Connect to the Redis server

r = redis.StrictRedis(host='< host>', port=, password='', db=0)

Cache data

r.set('mykey', 'Hello World' )
r.expire('mykey', 10)

Read data from cache

value = r.get('mykey')

Summary

Using Redis for caching in Linux can greatly improve the performance and scalability of the system. This article introduces how to install Redis, configure Redis, and use Redis for data caching. If you are developing a high-performance application, then Redis cache is definitely a very useful tool.

The above is the detailed content of How to use Redis cache in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn