Getting Started with Redis

Redis is an open source log-type, key-value database written in ANSI C language, supports network, can be memory-based and persistent, and provides APIs in multiple languages. Since March 15, 2010, the development of Redis has been hosted by VMware. Since May 2013, the development of Redis has been sponsored by Pivotal.

Redis Getting Started Guide Redis Getting Started Guide

1. Install Redis

Redis configuration file: /etc/redis.conf

Redis main program: /usr/bin/redis-server

Client Tools: /usr/bin/redis-cli

Default listening port: 6379

Data directory: /var/lib/redis

Service script: /usr/lib/systemd/system/redis.service

yum install redis # epel source based on CentOS 7

2. Connect to Redis

The client command tool for Redis is redis-cli, which connects to the local Redis service by default.

If you need to connect remotely, use redis-cli -h HOST -p PORT -a PASSWD

[root@cache1 ~]# redis-cli
127.0.0.1:6379> ping # Use the ping command to test connectivity to the service
PONG # The server replies to pong, indicating that there is no problem with the network connection

3. Redis data type

String (string)

String is the most basic type of redis, you can understand it as the exact same type as Memcached, a key corresponds to a value.

The string type is binary safe. Means redis string can contain any data. Such as jpg images or serialized objects.

The string type is the most basic data type of Redis, and a key can store a maximum of 512MB.

Hash

Redis hash is a set of key-value (key=>value) pairs.

Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.

List

Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.

Set

Redis's Set is an unordered collection of string type.

Sets are implemented through hash tables, so adding, deleting, and searching are all O(1).

zset (sorted set: sorted set)

Redis zset, like set, is also a collection of elements of type string, and does not allow duplicate members.

The difference is that each element is associated with a fraction of type double. Redis sorts the members of the set from small to large through scores.

The members of zset are unique, but the score can be repeated.

4. Get help

help @ + double-click the tab # Double-click the tab will appear help for various subcommands

help @string # String related configuration
help @list # list related configuration; lists are like arrays
help @set
help @hash
......

Five, List related commands

Several other data types are relatively simple, and the use of commands can be obtained through help. The easiest way is Baidu. Since it is difficult to understand some operations of List-related commands just by looking at the help, I will take them out and talk about the most commonly used commands separately.
Redis Getting Started Guide Redis Getting Started Guide

Example:

127.0.0.1:6379> LPUSH stu aubin # LPUSH, add elements to the left of the list, the list does not exist and is automatically created
(integer) 1
127.0.0.1:6379> RPUSH stu 22 # RPUSH, add elements to the right of the list, if the list does not exist, it will be created automatically
(integer) 2
127.0.0.1:6379> RPUSHX stu nan # RPUSHX, if the list exists, add elements to the right
(integer) 3
127.0.0.1:6379> LPUSHX stu linux                # LPUSHX, add elements to the left if the list exists
(integer) 4
127.0.0.1:6379> LRANGE stu 0 10 # LRANGE, display 0-10 values ​​of a list named stu
1) "aubin"
2) "22"
3) "nan"
4) "linux"

LPOP stu # Left pop, delete the first element in the list named stu
RPOP stu # pop right, delete the last element in the list named stu
LREM stu 3 aubin # delete 3 elements whose value is aubin from the beginning to the end
LREM stu -3 aubin # delete 3 elements whose value is aubin from the end to the beginning
LREM stu 0 aubin # remove all elements whose value is aubin

 

This article is reproduced from: https://www.linuxprobe.com/redis-easy-entry.html

Provide the latest Linux technology tutorial books for free, and strive to do more and better for open source technology enthusiasts, open source site: https://www.linuxprobe.com/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172590&siteId=291194637