Table of contents
2. Redis installation and configuration
3.1 Operate Redis through commands
3.5 Set ordered set (sorted set)
1. Introduction to Redis
Redis is an open source memorydata structure storage system, which can be used as database and cache and message middleware. Redis supports a variety of data structures, includingstrings, hash tables, lists, sets, and ordered sets, making it very flexible and versatile.
The main role of Redis is to provide high-performance data storage and access. Because data is stored in memory, Redis is capable of very fast read and write operations, making it ideal for handling high-concurrency and low-latency applications. In addition, Redis also has the following important features and application scenarios:
-
Caching: Redis is often used to cache popular data to reduce the load on the back-end database. By storing frequently accessed data in Redis, the response speed and throughput of your application can be greatly improved.
-
Distributed locks: Redis provides support for atomic operations and distributed locks, which can be used to implement concurrency control and resource competition solutions in distributed systems.
-
Publish/Subscribe: Redis’ publish/subscribe feature allows real-time messaging between different applications. This is useful in real-time chat, real-time data updates, and event-driven applications.
-
Rankings and counters: Redis’ ordered collection and counter functions can be used to implement rankings, counters and statistical functions. This is useful for applications that require real-time statistics and rankings.
-
Geolocation and geosearch: Redis's geolocation function can be used to store and query geolocation data, such as nearby people, location recommendations, and geosearch.
-
Session storage: Since Redis supports persistence, it can be used as a session store to replace traditional cookie-based session storage. This provides higher performance and scalability.
In summary, Redis is a powerful and flexible storage system suitable for a variety of different application scenarios, including caching, distributed locks, real-time messaging, leaderboards and counters, geolocation and session storage, etc. As software engineers, we can reasonably select and use Redis to optimize application performance and user experience based on specific needs and performance requirements.
2. Redis installation and configuration
Redis official website download path—— https://redis.io/download/
2.1 Linux version
1. Upload the redis.tar.gz file to the virtual machine and decompress it.
tar -zxvf redis-5.0.0.tar.gz
2. Install gcc
yum install gcc
3. Compile redis
make && make install
4. Check the installation status
make install
5. Modify the redis.conf file
Order:
vim redis.conf
General content:daemonize no change daemonize yes
6. Start redis
./src/redis-server redis.conf
7. redis startup test
Port test:
lsof -i:6379
Test whether you can connect to the local client:
./src/redis-cli
8. End the process
kill -9 xxxx (PID)
9. Change password and external connection
The above is a password-less link. You need to set a password for security, andconfigure external link access.
Modify the redis.conf directive:
vim redis.conf
1. Comment bind 127.0.0.1
2. Modify requirepass 123456
10. Open the Redis port number
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
reconnect
./src/redis-server redis.conf
11. Install client tool RedisDesktopManager connection test
After the connection is successful, you can see the default database of the server in the list on the left.
2.2 Windows version
1. Download the Windows version of redis and decompress it.
2. Write a file and put the following code into it. Change the suffix to batfile
cd Redis-x64-3.2.100 #进入解压文件
redis-server redis.windows.conf #安装
Run the file and the picture will appear, indicating that the installation is successful.
3. Modify the redis.windows.conf file
The operation is as above: Step 9 of Linux version --> Change password and external connection
4. Connection test
3. Redis command
3.1 Operate Redis through commands
First you need to start the Redis service and connect to the local client
Because we have joined the daemon process, the previous direct connection method cannot be used. You must bring a password and user port.
-h (host ip) -p (port) -a (password)
Redis supports five data types. Next, you can use Redis through commands.
- string
- hash
- list
- set
- zset(sorted set: ordered set)
3.2 String string
# set key //Save
3.3 Hash Hash
Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.
# hset key field1 value1 [field2 value2] #Set multiple field-values into the hash table key at the same time
# hget key field #Get the specified field value # hlen key
# hdel key field . a i=4> # hexists key field Query whether the field in the specified key exists
3.4 List list
A Redis list is a simple list of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list
# hset key field1 value1 [field2 value2] #Set multiple field-values into the hash table key at the same time
# hget key field #Get the specified field value # hlen key
# hdel key field . a i=4> # hexists key field Query whether the field in the specified key exists
3.5 Set ordered set (sorted set)
Redis Set is an unordered collection of String type. Set members are unique, which means that duplicate data cannot appear in the set.
# sadd key value1 [value2] #Add one or more elements to the set
# scard key #Get the number of elements in the set
# sscan key cursor [MATCH pattern] [COUNT count] #Iterate elements in the collection
# exists key